Farm irrigationtime limit:2000/1000ms (java/other) Memory limit:65536/32768k (Java/other) total submission (s): 6 A ccepted Submission (s): 3font:times New Roman | Verdana | Georgiafont Size:←→problem Descriptionbenny have a spacious farm land to irrigate. The farm land was a rectangle, and is divided to a lot of SAMLL squares. Water pipes is placed in these squares. Different Square has a Different type of pipe. There is one 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 in the over the pipes farm. For example, if he has a map
ADC FJK IHE
Then the water pipes is distributed like Figure 2 Several Wellsprings is 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 would have a good harvest in autumn.
Now Benny wants to know on least how many wellsprings should is found to has the whole farm land irrigated. Can you help him?
Note:in The above example, at least 3 wellsprings is needed, as those red points in Figure 2 show. Inputthere is several test cases! The first line contains is 2 integers m and N, then M lines follow. In each of these lines, there is N characters, in the range of ' A ' to ' K ', denoting the type of water pipe over the Corre Sponding 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
Authorzheng, Lusource
The puzzle: Using and checking the set, traversing the direction of each lattice, merging the adjacent two squares that can be connected into a set. The output N*m-count is the number of independent sets,
Note that the array of the check set needs to open a large point, the result of the small open tle several times ...
Code: 2015/10/20
1#include <iostream>2#include <stdio.h>3 #defineMax 52004 using namespacestd;5 intdicx[4]={0,-1,0,1};6 intdicy[4]={-1,0,1,0};7 intsign[ the][4]={8 1,1,0,0,//A9 0,1,1,0,//BTen 1,0,0,1,//C One 0,0,1,1,//D A 0,1,0,1,//E - 1,0,1,0,//F - 1,1,1,0,//G the 1,1,0,1,//H - 1,0,1,1,//I - 0,1,1,1,//J - 1,1,1,1//K + }; - intId[max]; + CharStr[max][max]; A voidCread (intN) at { - for(intI=0; i<=n;i++) id[i]=i; - } - intFind (intx) - { - if(X!=id[x]) id[x]=Find (id[x]); in returnId[x]; - } to intMain () + { - intn,m,i,j,k,d; the while(SCANF ("%d%d", &n,&m)! =EOF) * { $ if(n<0|| m<0) Break;Panax Notoginseng for(i=0; i<n;i++){ -scanf"%s", Str[i]); the } + intCount=0; ACread (nM); the for(i=0; i<n;i++)//Traverse every lattice + { - for(j=0; j<m;j++) $ { $ for(k=0;k<4; k++)//traverse the Four directions of the current grid - { - intii=i+Dicx[k]; the intjj=j+Dicy[k]; - Wuyi if(ii<0|| jj<0|| ii>=n| | JJ>=M)Continue; the if(sign[str[i][j]-'A'][k]&&sign[str[ii][jj]-'A'[(k +2)%4]) -{//determines whether the four directions of the current lattice are connected to the adjacent lattice Wu intA=find (i*m+j); - intB=find (ii*m+JJ); About if(a!=b)//merging interconnected collections $ { -id[a]=B; -count++; - } A } + } the } - } $printf"%d\n", N*m-count);//number of output sets the } the return 0; the}
Simple and check-set
"Simple and check" Farm irrigation