zoj2412 & hdu1198 - Farm Irrigation (DFS实现)
2014-11-18 14:54
337 查看
Farm Irrigation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6029 Accepted Submission(s): 2608
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case, output in one line the least number of wellsprings needed.
[align=left]Sample Input[/align]
2 2
DK
HF
3 3
ADC
FJK
IHE
-1 -1
[align=left]Sample Output[/align]
2
3
[align=left]Author[/align]
ZHENG, Lu
[align=left]Source[/align]
Zhejiang University Local Contest 2005
这道题可以用并查集来做,下面的代码是用dfs实现的,比较尴尬的一点是画图有点费事,恶心死我了。
/********************************* * * acm: zoj-2412 & hdu-1198 * * title: Farm Irrigation * * time : 2014.11.18 * *********************************/ //考察深度优先搜索 #include <stdio.h> #include <stdlib.h> #define MAXVEX 51 int map[MAXVEX*3][MAXVEX*3]; // 1表示有路,0表示无路 /* 将数组合并比较好,否则还要有个switch判断 比较麻烦 int A[3][3] ={{0,1,0},{1,1,0},{0,0,0}}; int B[3][3] ={{0,1,0},{0,1,1},{0,0,0}}; int C[3][3] ={{0,0,0},{1,1,0},{0,1,0}}; int D[3][3] ={{0,0,0},{0,1,1},{0,1,0}}; int E[3][3] ={{0,1,0},{0,1,0},{0,1,0}}; int F[3][3] ={{0,0,0},{1,1,1},{0,0,0}}; int G[3][3] ={{0,1,0},{1,1,1},{0,0,0}}; int H[3][3] ={{0,1,0},{1,1,0},{0,1,0}}; int I[3][3] ={{0,0,0},{1,1,1},{0,1,0}}; int J[3][3] ={{0,1,0},{0,1,1},{0,1,0}}; int K[3][3] ={{0,1,0}, {1,1,1},{0,1,0}}; */ int square[11][3][3]={{{0,1,0},{1,1,0},{0,0,0}},{{0,1,0},{0,1,1},{0,0,0}},{{0,0,0},{1,1,0},{0,1,0}},{{0,0,0},{0,1,1},{0,1,0}}, {{0,1,0},{0,1,0},{0,1,0}},{{0,0,0},{1,1,1},{0,0,0}},{{0,1,0},{1,1,1},{0,0,0}},{{0,1,0},{1,1,0},{0,1,0}}, {{0,0,0},{1,1,1},{0,1,0}},{{0,1,0},{0,1,1},{0,1,0}},{{0,1,0}, {1,1,1},{0,1,0}}}; int dir[4][2] ={{0,1},{0,-1},{1,0},{-1,0}}; //4个方向 int M, N; //长 宽 void DFS(int x, int y) { int i; int xx, yy; for (i = 0; i < 4; i++) { xx = x + dir[i][0]; yy = y + dir[i][1]; if (xx < 0 || xx >= M*3 || yy < 0 || yy >= N*3) { continue; } if (map[xx][yy] == 1) { map[xx][yy] = 0; DFS(xx, yy); } } } int main() { int i, j; int k, z; char c; int count; while (scanf("%d%d", &M, &N), M > 0 && N > 0) { for (i = 0; i < M; i++) //画地图 { for (j = 0; j < N; j++) { scanf(" %c", &c); for (k =0; k < 3; k++) { for (z = 0; z < 3; z++) { map[k + i*3][z + j*3] = square[c-'A'][k][z]; } } } } count = 0; for (i = 0; i < M*3; i++) { for (j = 0; j < N*3; j++) { if (map[i][j] == 1) { map[i][j] = 0; DFS(i, j); count++; } } } printf("%d\n", count); } return 0; }
相关文章推荐
- hdu1198 Farm Irrigation(DFS)
- ZOJ2412 Farm Irrigation (DFS)
- hdu1198 Farm Irrigation DFS
- hdu1198 Farm Irrigation DFS
- zoj2412 Farm Irrigation 搜索dfs
- HDU 1198 Farm Irrigation(DFS)
- DFS&BFS(链式前向星实现)
- HDU 1198 Farm Irrigation(dfs)
- hdu 1848 sg——dfs&&打表双实现
- zoj 2412 Farm Irrigation(DFS~)
- 图的基本遍历算法的实现(BFS & DFS)复习
- ZOJ 2412 Farm Irrigation(DFS 条件连通块)
- 【HDU 1198】Farm Irrigation(字符串拼接/类油田问题/DFS)
- zoj Farm Irrigation dfs
- Hdu 1198 Farm Irrigation (DFS 异或的应用)
- Farm Irrigation+DFS
- zoj 2412 Farm Irrigation #DFS
- zoj 2412 Farm Irrigation ——DFS入门题
- 杭电1198--Farm Irrigation(BFS外…
- ZOJ 2412 Farm Irrigation (DFS+模拟)