Farm Irrigation(非常有意思的并查集)
Farm Irrigation
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 46 Accepted Submission(s) : 26
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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
Output
Sample Input
2 2
DK
HF 3 3
ADC
FJK
IHE -1 -1
Sample Output
2
3
Author
Source
#include <iostream> using namespace std;
char map[][];
int par[];
int find(int x)
{
while(x!=par[x])
x=par[x];
return x;
}
void unioni(int x,int y)
{
int xx=find(x);
int yy=find(y);
if(xx!=yy)
{
par[yy]=xx;
} }
int main()
{
int n,m;
while(cin>>n>>m)
{
if(n<||m<)
break;
for(int i=;i<n*m;i++)
par[i]=i;
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
cin>>map[i][j];
} for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
if(i>=&&(map[i][j]=='A'||map[i][j]=='B'||map[i][j]=='E'||map[i][j]=='G'||map[i][j]=='H'||map[i][j]=='J'||map[i][j]=='K'))
{
if(map[i-][j]=='C'||map[i-][j]=='D'||map[i-][j]=='E'||map[i-][j]=='H'||map[i-][j]=='I'||map[i-][j]=='J'||map[i-][j]=='K')
{
unioni((i-)*m+j,i*m+j);
}
}
if(j>=&&(map[i][j]=='A'||map[i][j]=='C'||map[i][j]=='F'||map[i][j]=='G'||map[i][j]=='H'||map[i][j]=='I'||map[i][j]=='K'))
if(map[i][j-]=='B'||map[i][j-]=='D'||map[i][j-]=='F'||map[i][j-]=='G'||map[i][j-]=='I'||map[i][j-]=='J'||map[i][j-]=='K')
{
unioni(i*m+j-,i*m+j);
}
}
int flag=;
for(int i=;i<n*m;i++)
{
if(par[i]==i)
flag++; }
cout<<flag<<endl; }
return ;
}
Farm Irrigation(非常有意思的并查集)的更多相关文章
- HDU1198水管并查集Farm Irrigation
Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot ...
- 【简单并查集】Farm Irrigation
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
- HDU 1198 Farm Irrigation(并查集,自己构造连通条件或者dfs)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu 1198 Farm Irrigation(深搜dfs || 并查集)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm ...
- hdu 1198 Farm Irrigation(并查集)
题意: Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a ...
- 杭电OJ——1198 Farm Irrigation (并查集)
畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可 ...
- hdu1198 Farm Irrigation 并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1198 简单并查集 分别合并竖直方向和水平方向即可 代码: #include<iostream&g ...
- hdu 1198 (并查集 or dfs) Farm Irrigation
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...
- HDU 1198 Farm Irrigation(并查集+位运算)
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
随机推荐
- MongoDB数据库的基本操作
非关系型数据库(json数据库) npm install mongoose --save 启动数据酷: mongod --config /usr/local/etc/mongod.conf 这里可以将 ...
- mariaDB中文乱码
cetos7 下 http://hongjun.blog.51cto.com/445761/400985 1 . copy 一个文件成 /etc/my.cnf cp /usr/share/mysql ...
- 关于oracle函数listagg的使用说明
做项目的过程中遇到过一个这样的需求,在“用户查询”前台加一个字段“用户角色”,要将用户的所有角色查询出来放到一个字段中,角色之间用“,”分隔. 发现一个办法是使用Oracle的listagg方法. W ...
- Spring的一个入门例子
例子参考于:Spring系列教材 以及<轻量级JavaEE企业应用实战> Axe.class package com.how2java.bean; public class Axe { p ...
- PowerDesign的简单使用方法
PowerDesigner是一款功能非常强大的建模工具软件,足以与Rose比肩,同样是当今最著名的建模软件之一.Rose是专攻UML对象模型的建模工具,之后才向数据库建模发展,而PowerDesign ...
- 查看GPU占用率以及指定GPU加速程序
GPU占用率查看: 方法一:任务管理器 如图,GPU0和GPU1的占用率如下显示. 方法二:GPU-Z软件 下面两个GPU,上面是GPU0,下面是GPU1 sensors会话框里的GPU ...
- nodejs + ts 配置
参考:https://github.com/nestjs/typescript-starter 和 How to get auto restart and breakpoint support wit ...
- Codeforces 374C - Inna and Dima
374C - Inna and Dima 思路:dfs+记忆化搜索 代码: #include<bits/stdc++.h> using namespace std; #define ll ...
- word2010怎么把白色方框变成黑色方框?
word2010怎么把白色方框变成黑色方框? 打开Word 2010文档,选中第四个白色方框. 切换到“插入”功能区,在符号选项组单击“符号”按钮,出来的窗口单击“其他符号”. 在“符号”选项卡单 ...
- 学了vue和webpack的笔记
首先把package.json贴出来,这里很多插件存在版本区别,因此要特别注意版本,不是所有的安装最新的都行 { "name": "life_manager", ...