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.
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
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
一道深搜题,我抱着试试看的心态居然做出来了,其实没什么算法方面的技巧,就是将问题转化成数学问题是比较困难,看我的调试代码就知道调试了很久
#include<iostream> #include<stdio.h> #include<string.h> using namespace std; struct Cell{ char name; int right,up,left,down; }; Cell map[55][55]; int flag[55][55]; int count; int m,n; int d[4][2]={{0,-1},{-1,0},{0,1},{1,0}}; bool canConnect(Cell &a,Cell &b,int dir) { /*cout<<"====in canConnect===="; cout<<dir<<":"<<endl; cout<<a.name<<' '<<b.name<<endl; cout<<a.left<<' '<<b.left<<endl; cout<<a.right<<' '<<b.right<<endl; cout<<a.up<<' '<<b.up<<endl; cout<<a.down<<' '<<b.down<<endl; cout<<"====in canConnect===="<<endl; */ if(dir==1) { if(a.left==b.right&&a.left!=0) return true; } else if(dir==2) { if(a.up==b.down&&a.up!=0) return true; } else if(dir==3) { if(a.right==b.left&&a.right!=0) return true; } else if(dir==4) { if(a.down==b.up&&a.down!=0) return true; } return false; } void Convert(Cell &a) { switch(a.name){ case 'A':a.left=1;a.up=1;a.right=0;a.down=0;break; case 'B':a.left=0;a.up=1;a.right=1;a.down=0;break; case 'C':a.left=1;a.up=0;a.right=0;a.down=1;break; case 'D':a.left=0;a.up=0;a.right=1;a.down=1;break; case 'E':a.left=0;a.up=1;a.right=0;a.down=1;break; case 'F':a.left=1;a.up=0;a.right=1;a.down=0;break; case 'G':a.left=1;a.up=1;a.right=1;a.down=0;break; case 'H':a.left=1;a.up=1;a.right=0;a.down=1;break; case 'I':a.left=1;a.up=0;a.right=1;a.down=1;break; case 'J':a.left=0;a.up=1;a.right=1;a.down=1;break; case 'K':a.left=1;a.up=1;a.right=1;a.down=1;break; } } void dfs(int r,int c,int count) { //cout<<"r: "<<r<<" c: "<<c<<endl; if(r<=0||c<=0||r>m||c>n) { //cout<<"out of boundary"<<endl; return; } else if(flag[r][c]!=0) { //cout<<"flag not zero"<<endl; return; } else { flag[r][c]=count; for(int i=0;i<=3;i++) { if(canConnect(map[r][c],map[r+d[i][0]][c+d[i][1]],i+1)&&!flag[r+d[i][0]][c+d[i][1]]) { //cout<<"can connect r: "<<r+d[i][0]<<" c: "<<c+d[i][1]<<endl; dfs(r+d[i][0],c+d[i][1],count); } } } } int main() { while(cin>>m>>n) { if(m==-1&&n==-1)break; memset(flag,0,sizeof(flag)); memset(map,0,sizeof(map)); for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++) { cin>>map[i][j].name; Convert(map[i][j]); } } count=0; for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++) { if(flag[i][j])continue; dfs(i,j,++count); } } //cout<<"=====flag====="<<endl; /*for(int i=1;i<=m;i++) { for(int j=1;j<=n;j++) { cout<<flag[i][j]<<' '; } cout<<endl; }*/ //cout<<"=====flag====="<<endl; cout<<count<<endl; } }