Farm Irrigation ZOJ 2412(DFS连通图)

Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot of samll squares. Water pipes are placed in these squares. Different square has a different type of pipe. There are 11 types of pipes, which is marked from A to K, as Figure 1 shows.

Figure 1

Benny has a map of his farm, which is an array of marks denoting the distribution of water pipes over the whole farm. For example, if he has a map

ADC
FJK
IHE

then the water pipes are distributed like

Figure 2

Several wellsprings are found in the center of some squares, so water can flow along the pipes from one square to another. If water flow crosses one square, the whole farm land in this square is irrigated and will have a good harvest in autumn.

Now Benny wants to know at least how many wellsprings should be found to have the whole farm land irrigated. Can you help him?

Note: In the above example, at least 3 wellsprings are needed, as those red points in Figure 2 show.

Input

There are several test cases! In each test case, the first line contains 2 integers M and N, then M lines follow. In each of these lines, there are N characters, in the range of 'A' to 'K', denoting the type of water pipe over the corresponding square. A negative M or N denotes the end of input, else you can assume 1 <= M, N <= 50.

Output

For each test case, output in one line the least number of wellsprings needed.

Sample Input

2 2
DK
HF

3 3
ADC
FJK
IHE

-1 -1

Sample Output

2
3

题目意思:有11种正方形农田,每种正方形农田里面对应一种形状的水管,不同的的正方形一用A到K表示,给一个矩阵,问至少需要多少个水源可以使矩形中所有的地方都可以被灌溉,如果两个相邻的正方形的
水管正好对口,那么这两个正方形可以共用一个水源。

解题思路:明显的DFS求连通区域,不过难点在于对农田水管的处理,我们可以开一个结构体数组,里面存放每一块农田四方向的水管,1代表有,0代表无。在搜索的过程中,假如当前的农田有向上的水管,而当前
农田的上面一块农田恰好有向下的水管,那么就可以从当前的水管向上搜索了。

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 using namespace std;
  5 int n,m;
  6 int maps[60][60];
  7 int vis[60][60];
  8 struct node
  9 {
 10     int up;
 11     int down;
 12     int left;
 13     int right;
 14 };
 15 node num[11]= {{1,0,1,0},{1,0,0,1},{0,1,1,0},{0,1,0,1},{1,1,0,0},{0,0,1,1},
 16     {1,0,1,1},{1,1,1,0},{0,1,1,1},{1,1,0,1},{1,1,1,1}};
 17 void DFS(int x,int y)
 18 {
 19     int i;
 20     int a,b;
 21     if(x<=0||x>m||y<=0||y>n||vis[x][y])
 22     {
 23         return ;
 24     }
 25     vis[x][y]=1;
 26     for(i=0; i<4; i++)
 27     {
 28         if(i==0)///向上走
 29         {
 30             a=x-1;
 31             b=y+0;
 32             if(num[maps[x][y]].up&&num[maps[a][b]].down)
 33             {
 34                 DFS(a,b);
 35             }
 36         }
 37         else if(i==1)///向下走
 38         {
 39             a=x+1;
 40             b=y+0;
 41             if(num[maps[x][y]].down&&num[maps[a][b]].up)
 42             {
 43                 DFS(a,b);
 44             }
 45         }
 46         else if(i==2)///向左走
 47         {
 48             a=x+0;
 49             b=y-1;
 50             if(num[maps[x][y]].left&&num[maps[a][b]].right)
 51             {
 52                 DFS(a,b);
 53             }
 54         }
 55         else if(i==3)///向右走
 56         {
 57             a=x+0;
 58             b=y+1;
 59             if(num[maps[x][y]].right&&num[maps[a][b]].left)
 60             {
 61                 DFS(a,b);
 62             }
 63         }
 64     }
 65     return ;
 66 }
 67 int main()
 68 {
 69     char ch;
 70     int i,j;
 71     int counts;
 72     while(scanf("%d%d",&m,&n)!=EOF)
 73     {
 74         memset(vis,0,sizeof(vis));
 75         if(m==-1&&n==-1)
 76         {
 77             break;
 78         }
 79         getchar();
 80         counts=0;
 81         for(i=1; i<=m; i++)
 82         {
 83             for(j=1; j<=n; j++)
 84             {
 85                 scanf("%c",&ch);
 86                 maps[i][j]=ch-'A';
 87             }
 88             getchar();
 89         }
 90 
 91         for(i=1; i<=m; i++)
 92         {
 93             for(j=1; j<=n; j++)
 94             {
 95                 if(!vis[i][j])
 96                 {
 97                     counts++;
 98                     DFS(i,j);
 99                 }
100             }
101         }
102         printf("%d
",counts);
103     }
104     return 0;
105 }
原文地址:https://www.cnblogs.com/wkfvawl/p/9573870.html