HDU 1198 Farm Irrigation (并查集)
来源:互联网 发布:淘宝助理怎么下架 编辑:程序博客网 时间:2024/05/16 07:11
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.
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 2
DK
HF
3 3
ADC
FJK
IHE
-1 -1
Sample Output
2
3
泪奔,一个小BUG找了2小时 。。。
//#define Night_13#include <iostream>#include <cstring>using namespace std;int n, m, father[100000];char farm[55][55];int per[][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 Find(int x) { int fx = x; while (father[fx] != fx) { fx = father[fx]; } int i = x, j; while (father[i] != fx) { j = father[i]; father[i] = fx; i = j; } return fx;}void Merge(int a, int b) { int fa = Find(a), fb = Find(b); if (fa != fb) { father[fb] = fa; }}int main() {#ifdef Night_13 freopen("input.txt", "r", stdin);#endif while (scanf("%d%d%*c", &m, &n), n != -1 && m != -1) { for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { scanf("%c", &farm[i][j]); father[i*100 + j] = i*100 + j; } getchar(); } for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (i != m - 1 && per[farm[i][j] - 'A'][2] == 1 && per[farm[i + 1][j] - 'A'][0] == 1) { Merge(i*100 + j, (i + 1)*100 + j); } if (j != n - 1 && per[farm[i][j] - 'A'][1] == 1 && per[farm[i][j + 1] - 'A'][3] == 1) { Merge(i*100 + j, i*100 + j+1); } } } int ans = 0; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (Find(i*100 + j) == i*100 + j) ++ans; } } printf("%d\n", ans); } return 0;}
- hdu 1198 Farm Irrigation(并查集)
- hdu 1198 Farm Irrigation( 并查集)
- HDU 1198 Farm Irrigation(并查集)
- HDU 1198 Farm Irrigation 【并查集】
- HDU 1198Farm Irrigation(并查集)
- HDU 1198 Farm Irrigation(并查集)
- hdu 1198 Farm Irrigation(并查集)
- hdu 1198 Farm Irrigation(并查集)
- HDU 1198 Farm Irrigation 并查集
- HDU 1198 Farm Irrigation 并查集
- hdu-1198-Farm Irrigation(并查集)
- HDU 1198 Farm Irrigation (并查集)
- HDU 1198 Farm Irrigation 并查集
- hdu 1198 Farm Irrigation 并查集
- hdu 1198 Farm Irrigation(搜索+并查集)
- hdu 1198 Farm Irrigation (并查集)
- hdu 1198 Farm Irrigation(并查集)
- HDU 1198 Farm Irrigation(并查集)
- 【HTTP】Fiddler(一) - Fiddler简介
- 寻找水王·扩展
- iOS中Apple Pay接入详细教程
- java操作xml文件
- CentOS 7安装mysql
- HDU 1198 Farm Irrigation (并查集)
- 学而不思则罔,学而不思则殆-年终总结
- Java大牛养成记(1)——J2EE13规范
- VS启动调试传递命令行参数
- c++ gui qt4 chap02 find
- iOS 判断相机权限是否被限制,判断相机是否可以使用
- Linux内核分析实验一
- 使用networkx导出关系网络并用gephi进行简单分析
- 自定义 ActionBar ,创建复合控件