ZOJ 2412 Farm Irrigation

来源:互联网 发布:ios放置类游戏 知乎 编辑:程序博客网 时间:2024/06/08 05:29

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

ADCFJKIHE
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 2DKHF3 3ADCFJKIHE-1 -1

Sample Output

23

限定条件的BFS 略坑 WA好多发  细节掌握得不行

#include <stdio.h>#include <string.h>#include <iostream>#include <queue>using namespace std;char map[55][55];int use[55][55];int n,m,d[5][2]={{0,0},{-1,0},{0,1},{1,0},{0,-1}};struct pp{int x,y;};queue<pp>q;int pd(char c,int b){if(c=='A' && (b==2||b==3))return 0;else if(c=='B'&&(b==3||b==4))return 0;else if(c=='C'&&(b==1||b==2))return 0;else if(c=='D'&&(b==1||b==4))return 0;else if(c=='E'&&(b==2||b==4))return 0;else if(c=='F'&&(b==1||b==3))return 0;else if(c=='G'&&b==3)return 0;else if(c=='H'&&b==2)return 0;else if(c=='I'&&b==1)return 0;else if( (b==4 && c=='J'))return 0;return 1;}int pd1(char e,char a,int b){if(b==1 && pd(e,b) && (a=='C'||a=='D'||a=='H'||a=='I'||a=='E'||a=='J'||a=='K'))return 1;if(b==2 && pd(e,b) && (a=='A'||a=='C'||a=='F'||a=='H'||a=='I'||a=='K'||a=='G'))return 1;if(b==3 && pd(e,b) && (a=='A'||a=='B'||a=='E'||a=='G'||a=='H'||a=='J'||a=='K'))return 1;if(b==4 && pd(e,b) && (a=='B'||a=='D'||a=='F'||a=='G'||a=='I'||a=='J'||a=='K'))return 1;return 0;}void bfs(int i,int j){pp a,now,next;a.x=i;a.y=j;q.push(a);while(!q.empty()){now=q.front();q.pop();for(i=1;i<=4;i++){next.x=now.x+d[i][0];next.y=now.y+d[i][1];if( next.x<1|| next.x>n ||next.y<1|| next.y>m || !use[next.x][next.y] )  //没用过continue;if(!pd1(map[now.x][now.y],map[next.x][next.y],i))//能连通continue;use[next.x][next.y]=0;q.push(next);}}}int main(){int i,j,sum;while(~scanf("%d %d",&n,&m)){if(n<=0||m<=0)break;sum=0;for(i=1;i<=n;i++)cin>>map[i]+1;memset(use,1,sizeof use);for(i=1;i<=n;i++){for(j=1;j<=m;j++){if(use[i][j]){use[i][j]=0;bfs(i,j);sum++;}}}printf("%d\n",sum);}return 0;}




0 0