Topic Links:ah haha, choose me to choose meThe idea is: first, according to the image abstraction of the unicom relationship. First determine the relationship between each graph, with a value of 01 to indicate the non-connected and non-connected ... The DFS search is then performed from the 1th graph. If the two fast fields can be connected, then mark. Note that the process of processing yourThe search order is the same as the order of connectivity for each of your graphs ... And then the last look up and down. Whether or not to match ... Look at the end there are a few different fast, this is the answer, feel the same as the check set. And check set should also be done.
Title:Farm IrrigationTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 5696 Accepted Submission (s): 2474
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, Lu
Sourcezhejiang University Local Contest 2005
RECOMMENDIGNATIUS.L | We have carefully selected several similar problems for you:1232 1213 1856 1811 1829
The code is:
#include <cstdio> #include <cstring> #include <iostream>using namespace std;int n,m,ans;const int MAXN =50+10;int map[maxn][maxn];int dx[]={-1,0,1,0};int dy[]={0,1,0,-1};int pipe[11][4]={{1,0,0,1},{1,1,0,0},{0,0,1,1}, {0,1,1,0},{1,0,1,0},{0,1,0,1},{1,1,0,1},{1,0,1,1},{0,1,1,1},{1,1,1,0},{1,1,1,1}};int Hash[maxn][maxn];bool check (int row,int col) {if (Row>=0&&row<n&&col>=0&&col<m&&!hash[row][col]) return true; return false;} void Dfs (int row,int col) {hash[row][col]=1; for (int k=0;k<4;k++) {int dx=row+dx[k]; int dy=col+dy[k]; if (check (dx,dy) &&pipe[map[row][col]][k]==1&&pipe[map[dx][dy]][(k+2)%4]==1) DFS (DX,DY); }}void solve () {memset (hash,0,sizeof (hash)); for (int i=0;i<n;i++) for (int j=0;j<m;j++) {if (!hash[i][j]) {DFS (I,J); ans++; }}}int Main () {char STR[MAXN]; while (~SCANF ("%d%d",&N,&m)) {ans=0; if (n<=0| | M<=0) return 0; for (int i=0;i<n;i++) {scanf ("%s", str); for (int j=0;j<m;j++) map[i][j]=str[j]-' A '; } solve (); printf ("%d\n", ans); } return 0;}