zoj2412 & hdu1198 - Farm Irrigation (DFS实现)
来源:互联网 发布:ubuntu gtx1080 驱动 编辑:程序博客网 时间:2024/04/30 11:55
Farm Irrigation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6029 Accepted Submission(s): 2608
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.
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
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
Author
ZHENG, Lu
Source
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 51int 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;}
0 0
- zoj2412 & hdu1198 - Farm Irrigation (DFS实现)
- ZOJ2412 Farm Irrigation (DFS)
- hdu1198 Farm Irrigation dfs
- hdu1198 Farm Irrigation DFS
- hdu1198 Farm Irrigation(DFS)
- hdu1198 Farm Irrigation (DFS)
- zoj2412 Farm Irrigation 搜索dfs
- zoj2412:Farm Irrigation
- zoj2412-Farm Irrigation
- HDU1198 Farm Irrigation
- HDU1198--Farm Irrigation
- HDU1198 Farm Irrigation
- Farm Irrigation(HDU1198)(幷查集)
- HDU1198 Farm Irrigation
- HDU1198-Farm Irrigation
- hdu1198 Farm Irrigation
- hdu1198 Farm Irrigation(DFS,并查集)
- hdu1198 Farm Irrigation —— dfs or 并查集
- 用 Python 脚本实现对 Linux 服务器的监控
- Android 学习笔记 第五季 7 在 Intellij 和 AndroidStudio 中查看源码
- vs2008 MFC 对话框 grid++ report 使用经验总结
- 题目1517:链表中倒数第k个结点
- [leetcode] Balanced Binary Tree
- zoj2412 & hdu1198 - Farm Irrigation (DFS实现)
- makefile用法
- grid(datagrid、treegrid)自适应高度和宽度解决方法
- 大数据时代对于数据的一些看法
- MYSQL中常见时间日期相关的表示方法与常用相关函数
- 【5】设计模式之Prototype模式
- Apache Shiro 使用手册(二)Shiro 认证
- Windows平台安装Django
- 数据文件迁移