HDU-1198 Farm Irrigation

来源:互联网 发布:知乎粉红 编辑:程序博客网 时间:2024/05/06 16:15

Farm Irrigation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)


Problem Description
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
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 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.
 

Input
There 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 2DKHF3 3ADCFJKIHE-1 -1
 

Sample Output
23
 
————————————————————尴尬的分割线——————————————————————
前言:又遇到疑似忘记发博客的题目了。
思路:很水的DFS题,只不过是需要把每个点存四个值,表示管道的方向。采用in和out来对应水流流入和流出的方向。
代码如下:
/*ID: j.sure.1PROG: LANG: C++*//****************************************/#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>#include <stack>#include <queue>#include <vector>#include <map>#include <string>#include <iostream>using namespace std;/****************************************/#define LIM nx >= 0 && nx < n && ny >= 0 && ny < m#define M mat[i][j]const int N = 55;const int dir[][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};bool mat[N][N][4];bool vis[N][N];int n, m;inline void read_G(int i, int j){char w;scanf("%c", &w);switch(w){case 'A': M[0] = M[3] = true; break;case 'B': M[0] = M[1] = true; break;case 'C': M[2] = M[3] = true; break;case 'D': M[2] = M[1] = true; break;case 'E': M[0] = M[2] = true; break;case 'F': M[1] = M[3] = true; break;case 'G': M[0] = M[1] = M[3] = true; break;case 'H': M[0] = M[2] = M[3] = true; break;case 'I': M[1] = M[2] = M[3] = true; break;case 'J': M[0] = M[1] = M[2] = true; break;case 'K': M[0] = M[1] = M[2] = M[3] = true; break;}}void dfs(int x, int y, int out){mat[x][y][out] = false;int nx = x + dir[out][0], ny = y + dir[out][1];if(LIM) {int in = (out+2)%4;if(mat[nx][ny][in]) {//上个点流出之后能否流入该点mat[nx][ny][in] = false;for(int nout = 0; nout < 4; nout++) if(mat[nx][ny][nout]) {//选择流出方向dfs(nx, ny, nout);}}}}int main(){//freopen("1198.in", "r", stdin);//freopen("1198.out", "w", stdout);while(scanf("%d%d", &n, &m)!=EOF) {getchar();if(n == -1&&m == -1) break;memset(mat, false, sizeof(mat));memset(vis, 0, sizeof(vis));for(int i = 0; i < n; i++) {for(int j = 0; j < m; j++) {read_G(i, j);}getchar();}int ans = 0;for(int i = 0; i < n; i++) {for(int j = 0; j < m; j++) {for(int out = 0; out < 4; out++) if(mat[i][j][out]) {dfs(i, j, out);if(!vis[i][j]) {ans++;vis[i][j] = true;}}}}printf("%d\n", ans);}return 0;}


0 0
原创粉丝点击