Farm Irrigation

**Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)**

Total submission (s): 5820 accepted submission (s): 2523

Problem descriptionbenny 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 could wellsprings shocould 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.

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

Sample Input

2 2DKHF3 3ADCFJKIHE-1 -1

Sample output

23. The input is given as a letter. Therefore, for each letter, you must determine whether it is connected to the upper and lower left letters. If it is connected, it will be merged, the number of the last set is the answer. I had to find out all the connected letters on the paper and store them in the array.#include <iostream>#include <cstdio>#include <cstring>using namespace std;int m,n,fa[10010],hash[10010];char ma[60][60];void Make_set(int n){for(int i=1;i<=n;i++)fa[i]=i;}int Find(int x){ if(x!=fa[x])fa[x]=Find(fa[x]);return fa[x];}void Union(int x,int y){int fx=Find(x);int fy=Find(y);if(fx==fy)return ;fa[fy]=fx;}int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};char ok[11][4][12]={{"CDEHIJK","","BDFGIJK",""},{"CDEHIJK","","","ACFGHIK"},{"","ABEGHJK","BDFGIJK",""},{"","ABEGHJK","","ACFGHIK"},{"CDEHIJK","ABEGHJK","",""},{"","","BDFGIJK","ACFGHIK"},{"CDEHIJK","","BDFGIJK","ACFGHIK"},{"CDEHIJK","ABEGHJK","BDFGIJK",""},{"","ABEGHJK","BDFGIJK","ACFGHIK"},{"CDEHIJK","ABEGHJK","","ACFGHIK"},{"CDEHIJK","ABEGHJK","BDFGIJK","ACFGHIK"}};void bfs(int x,int y){ int i,tx=x,ty=y; for(i=0;i<4;i++){tx=x+dir[i][0];ty=y+dir[i][1];if(0<=tx&&tx<m&&0<=ty&&ty<n){int flag=0,len=strlen(ok[ma[x][y]-'A'][i]); for(int j=0;j<len;j++){if(ok[ma[x][y]-'A'][i][j]==ma[tx][ty]){flag=1;break;}}if(flag)Union(x*n+y+1,tx*n+ty+1);}}}int main(){int i,j;while(~scanf("%d%d",&m,&n)){memset(hash,0,sizeof(hash));if(m==-1&&n==-1) break;for(i=0;i<m;i++)scanf("%s",ma[i]);Make_set(m*n);for(i=0;i<m;i++)for(j=0;j<n;j++)bfs(i,j);for(i=1;i<=m*n;i++){int f=Find(i);hash[f]++;}int cnt=0;for(i=1;i<=m*n;i++)if(hash[i]) cnt++;printf("%d\n",cnt);}return 0;}