(DFS) Hdoj1198-farm irrigation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.