zoj 2412 Farm Irrigation

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1412

解题思路:DFS搜索

  1 ///////////////////////////////////////////////////////////////////////////
  2 //problem_id: zoj 2412
  3 //user_id: SCNU20102200088
  4 ///////////////////////////////////////////////////////////////////////////
  5 
  6 #include <algorithm>
  7 #include <iostream>
  8 #include <iterator>
  9 #include <iomanip>
 10 #include <cstring>
 11 #include <cstdlib>
 12 #include <string>
 13 #include <vector>
 14 #include <cstdio>
 15 #include <cctype>
 16 #include <cmath>
 17 #include <queue>
 18 #include <stack>
 19 #include <list>
 20 #include <set>
 21 #include <map>
 22 using namespace std;
 23 
 24 ///////////////////////////////////////////////////////////////////////////
 25 typedef long long LL;
 26 const double PI=acos(-1.0);
 27 ///////////////////////////////////////////////////////////////////////////
 28 
 29 ///////////////////////////////////////////////////////////////////////////
 30 //Add Code:
 31 int m,n,cnt;
 32 char land[55][55];
 33 bool flag[55][55];
 34 //顺时针:上 -> 右 -> 下 -> 左
 35 const int x[]={-1,0,1,0};
 36 const int y[]={0,1,0,-1};
 37 const int tube[11][4]={
 38     {1,0,0,1},  //A
 39     {1,1,0,0},  //B
 40     {0,0,1,1},  //C
 41     {0,1,1,0},  //D
 42     {1,0,1,0},  //E
 43     {0,1,0,1},  //F
 44     {1,1,0,1},  //G
 45     {1,0,1,1},  //H
 46     {0,1,1,1},  //I
 47     {1,1,1,0},  //J
 48     {1,1,1,1}   //K
 49 };
 50 
 51 void DFS(int i,int j){
 52     int num=land[i][j]-'A';
 53     for(int p=0;p<4;p++){
 54         if(tube[num][p]==1){
 55             int xx=i+x[p],yy=j+y[p];
 56             if(xx<1 || xx>m || yy<1 || yy>n) goto out;
 57             if(flag[xx][yy]){
 58                 int temp=land[xx][yy]-'A';
 59                 int zz=(p+2)%4;
 60                 if(tube[temp][zz]==1){
 61                     flag[xx][yy]=0;
 62                     cnt--;
 63                     DFS(xx,yy);
 64                 }
 65             }
 66         }
 67         out:;
 68     }
 69     return ;
 70 }
 71 ///////////////////////////////////////////////////////////////////////////
 72 
 73 int main(){
 74     ///////////////////////////////////////////////////////////////////////
 75     //Add code:
 76     while(scanf("%d%d",&m,&n)!=EOF){
 77         if(m<0 || n<0) break;
 78         int i,j;
 79         char ch;
 80         scanf("%c",&ch);
 81         for(i=1;i<=m;i++){
 82             for(j=1;j<=n;j++){
 83                 scanf("%c",&land[i][j]);
 84                 flag[i][j]=1;
 85             }
 86             scanf("%c",&ch);
 87         }
 88         cnt=m*n;
 89         int ans=0;
 90         while(cnt){
 91             for(i=1;i<=m;i++){
 92                 for(j=1;j<=n;j++){
 93                     if(flag[i][j]){
 94                         flag[i][j]=0;
 95                         cnt--;
 96                         DFS(i,j);
 97                         ans++;
 98                     }
 99                 }
100             }
101         }
102         printf("%d\n",ans);
103     }
104     ///////////////////////////////////////////////////////////////////////
105     return 0;
106 }
107 
108 ///////////////////////////////////////////////////////////////////////////
109 /*
110 Testcase:
111 Input:
112 2 2
113 DK
114 HF
115 
116 3 3
117 ADC
118 FJK
119 IHE
120 
121 -1 -1
122 Output:
123 2
124 3
125 */
126 ///////////////////////////////////////////////////////////////////////////

posted on 2013-08-17 17:29  SCNU20102200088  阅读(204)  评论(0编辑  收藏  举报

导航