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 IHEthen 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.
InputThere 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 -1Sample Output
2 3
此题比较简单,主要的思想还是深搜,对于图中每一个连通分量就是一个泉眼,深搜一下就好,最烦人的是前面建立图的邻接矩阵,容易出错,下标容易控制错误,导致wa了很多次。晕死。最后一看有个地方+1写成-1.
代码主要是为了复习课本建立邻接矩阵的知识而写的,写的比较复杂吧,主要是复习知识所用。
#include<stdio.h> #include<string.h> int M,N; char map[50][50]; int table[2500][2500]; bool visited[2500]; typedef struct kind{ int top; int down; int right; int left; }kind; int Firstnextadj(int cur){ int i=0; for(i=0;i<N*M;i++) if(table[cur][i])return i; return -1; } int Nextadj(int cur,int i){ int j=i; for(j=i+1;j<N*M;j++) if(table[cur][j])return j; return -1; } void DFS(int i){ int j; visited[i]=true; for(j=Firstnextadj(i);j>=0;j=Nextadj(i,j)) if(!visited[j])DFS(j); } int main(){ int i,j; kind kind[11]; while(~scanf("%d%d",&M,&N)){ //首先图点初始化为0 if(M<=0||N<=0)break; int k=0; int count=0; for(i=0;i<M*N;i++) for(j=0;j<M*N;j++) table[i][j]=0; for(i=0;i<11;i++){ kind[i].top=0; kind[i].down=0; kind[i].left=0; kind[i].right=0; } memset(visited,0,sizeof(visited)); //输入map的值 getchar(); for(i=0;i<M;i++){ for(j=0;j<N;j++) scanf("%c",&map[i][j]); getchar(); } int temp=65; for(i=0;i<11;i++){ switch(temp){ case 'A':kind[k].top=1; kind[k].left=1; k++; break; case 'B':kind[k].top=1; kind[k].right=1; k++; break; case 'C':kind[k].left=1; kind[k].down=1; k++; break; case 'D':kind[k].down=1; kind[k].right=1; k++; break; case 'E':kind[k].down=1; kind[k].top=1; k++; break; case 'F':kind[k].left=1; kind[k].right=1; k++; break; case 'G':kind[k].top=1; kind[k].right=1; kind[k].left=1; k++; break; case 'H':kind[k].top=1; kind[k].down=1; kind[k].left=1; k++; break; case 'I':kind[k].left=1; kind[k].right=1; kind[k].down=1; k++; break; case 'J':kind[k].top=1; kind[k].down=1; kind[k].right=1; k++; break; case 'K':kind[k].top=1; kind[k].down=1; kind[k].right=1; kind[k].left=1; k++; break; } temp++; } for(i=0;i<M;i++) for(j=0;j<N;j++){ if(j+1<N){ if(kind[map[i][j]-65].right&& kind[map[i][j]-65].right==kind[map[i][j+1]-65].left){ table[i*N+j][i*N+j+1]=1; table[i*N+j+1][i*N+j]=1; } } if(j){ if(kind[map[i][j]-65].left&& kind[map[i][j]-65].left==kind[map[i][j-1]-65].right){ table[i*N+j][i*N+j-1]=1; table[i*N+j-1][i*N+j]=1; } } if(i){ if(kind[map[i][j]-65].top&& kind[map[i][j]-65].top==kind[map[i-1][j]-65].down){ table[i*N+j][(i-1)*N+j]=1; table[(i-1)*N+j][i*N+j]=1; } } if(i+1<M){ if(kind[map[i][j]-65].down&& kind[map[i][j]-65].down==kind[map[i+1][j]-65].top){ table[i*N+j][(i+1)*N+j]=1; table[(i+1)*N+j][i*N+j]=1; } } } /* for(i=0;i<M;i++) printf("%s\n",map[i]); for(k=0;k<11;k++){ printf("kind:%d top:%d down:%d right:%d left:%d\n",k,kind[k]); } for(i=0;i<M*N;i++){ for(j=0;j<M*N;j++) printf("%d ",table[i][j]); printf("\n"); } for(i=0;i<M*N;i++) { printf("第%d个Fistnext:%d\n",i,Firstnextadj(i)); for(j=Firstnextadj(i);j>=0;j=Nextadj(i,j)) printf("第%d个next:%d\n",i,Nextadj(i,j)); } */ for(i=0;i<M*N;i++){ if(!visited[i]){ DFS(i); count++; } } printf("%d\n",count); //system("pause"); } return 0; }