Topic links
The simple application of Dfs, the more cumbersome is to process the input of the English alphabet. Use and check set can also do (but the author has not mastered and check set, before only used once, later learned to back up)
1#include <cstdio>2#include <cstring>3 using namespacestd;4 intm,n,typ[ A][4]={{1,1,0,0},{1,0,1,0},{0,1,0,1},{0,0,1,1},{1,0,0,1},{0,1,1,0},{1,1,1,0},{1,1,0,1},{0,1,1,1},{1,0,1,1},{1,1,1,1},{0,0,0,0}};5 intdir[4][2]={{-1,0},{0,-1},{0,1},{1,0}},a[ -][ -],cnt;//the way to deal with this is to label different kinds of pipes as 0--3, maybe Unicom is 1, or 0 .6 Chartem[ -];7 voidDfsintQinintSJ)8 {9 intX1,y1,kind=a[si][sj],z;Tena[si][sj]= One;//The first step is to first initialize this lattice to the 11th type, that is, all directions are not interconnected lattice One for(intI=0;i<4; i++) A { -z=Typ[kind][i]; - if(z==1) the { -x1=si+dir[i][0]; -y1=sj+dir[i][1]; - if(x1<0|| x1>=m| | y1<0|| y1>=n| | typ[a[x1][y1]][3-i]==0)//continue if it is out of range or unreachable; + Continue; - Else + { A DFS (x1,y1); at } - } - } - return; - } - intMain () { in while(SCANF ("%d%d",&m,&N)) - { to inti,j; +Cnt=0; - if(m<0|| n<0) the Break; * Else $ {Panax Notoginseng for(i=0; i<m;i++) - { thescanf"%s", TEM); + for(j=0; j<n;j++) A { thea[i][j]=tem[j]-'A';//for each English alphabet, the array has been built for this + } - } $ for(i=0; i<m;i++) $ { - for(j=0; j<n;j++) - if(a[i][j]!= One) the { -cnt++;//each newly-seen non-11 lattice is a whole that may be connected to a large chunk. Wuyi DFS (I,J); the } - } Wu } -printf"%d\n", CNT); About } $}
(DFS) Hdoj1198-farm irrigation