hdu1198Farm Irrigation(dfs找联通)
题目链接:
思路是:首先依据图像抽象出联通关系。。
首先确定每一种图形的联通关系。用01值表示不连通与不连通。。。
然后从第1个图形进行dfs搜索。假设碰到两快田地能够联通的话那么标记。。注意处理的过程中你的
搜索顺序要和你的每一个图形的连通性的顺序同样。
。然后就是最后看上下。
左右是否能匹配。。
。
看最后有几个不同的快,这就是答案,感觉跟并查集一样。。并查集应该也能够做。
。
题目:
Farm Irrigation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5696 Accepted Submission(s): 2474
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.
M or N denotes the end of input, else you can assume 1 <= M, N <= 50.
2 2
DK
HF 3 3
ADC
FJK
IHE -1 -1
2
3
pid=1856" target="_blank" style="color:rgb(26,92,200); text-decoration:none">1856
1811 1829#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int n,m,ans;
const int maxn=50+10;
int map[maxn][maxn];
int dX[]={-1,0,1,0};
int dY[]={0,1,0,-1};
int pipe[11][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 hash[maxn][maxn];
bool check(int row,int col)
{
if(row>=0&&row<n&&col>=0&&col<m&&!hash[row][col])
return true;
return false;
}
void dfs(int row,int col)
{
hash[row][col]=1;
for(int k=0;k<4;k++)
{
int dx=row+dX[k];
int dy=col+dY[k];
if(check(dx,dy)&&pipe[map[row][col]][k]==1&&pipe[map[dx][dy]][(k+2)%4]==1)
dfs(dx,dy);
}
}
void solve()
{
memset(hash,0,sizeof(hash));
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(!hash[i][j])
{
dfs(i,j);
ans++;
}
}
}
int main()
{
char str[maxn];
while(~scanf("%d%d",&n,&m))
{
ans=0;
if(n<=0||m<=0) return 0;
for(int i=0;i<n;i++)
{
scanf("%s",str);
for(int j=0;j<m;j++)
map[i][j]=str[j]-'A';
}
solve();
printf("%d\n",ans);
}
return 0;
}
#include<cstring>
#include<iostream>
using namespace std; int n,m,ans;
const int maxn=50+10;
int map[maxn][maxn]; int dX[]={-1,0,1,0};
int dY[]={0,1,0,-1}; int pipe[11][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 hash[maxn][maxn]; bool check(int row,int col)
{
if(row>=0&&row<n&&col>=0&&col<m&&!hash[row][col])
return true;
return false;
} void dfs(int row,int col)
{
hash[row][col]=1;
for(int k=0;k<4;k++)
{
int dx=row+dX[k];
int dy=col+dY[k];
if(check(dx,dy)&&pipe[map[row][col]][k]==1&&pipe[map[dx][dy]][(k+2)%4]==1)
dfs(dx,dy);
}
} void solve()
{
memset(hash,0,sizeof(hash));
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(!hash[i][j])
{
dfs(i,j);
ans++;
}
}
} int main()
{
char str[maxn];
while(~scanf("%d%d",&n,&m))
{
ans=0;
if(n<=0||m<=0) return 0;
for(int i=0;i<n;i++)
{
scanf("%s",str);
for(int j=0;j<m;j++)
map[i][j]=str[j]-'A';
}
solve();
printf("%d\n",ans);
}
return 0;
}
hdu1198Farm Irrigation(dfs找联通)的更多相关文章
- hdu1198Farm Irrigation (DFS)
Problem Description Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is ...
- POJ 3620 Avoid The Lakes【DFS找联通块】
Avoid The Lakes Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6826 Accepted: 3637 D ...
- 用dfs求联通块(UVa572)
一.题目 输入一个m行n列的字符矩阵,统计字符“@”组成多少个八连块.如果两个字符所在的格子相邻(横.竖.或者对角线方向),就说它们属于同一个八连块. 二.解题思路 和前面的二叉树遍历类似,图也有DF ...
- 用dfs遍历联通块(优化)
一.题目(CF 598D) 输入一个n x m的字符矩阵,求从某个空点出发,能碰到多少面墙壁,总共询问k次.(3 ≤m,n ≤1000,1 ≤ k ≤ min(nm,100 000)) 二.解题思路 ...
- 利用DFS求联通块个数
/*572 - Oil Deposits ---DFS求联通块个数:从每个@出发遍历它周围的@.每次访问一个格子就给它一个联通编号,在访问之前,先检查他是否 ---已有编号,从而避免了一个格子重复访问 ...
- CodeForces - 103B(思维+dfs找环)
题意 https://vjudge.net/problem/CodeForces-103B 很久很久以前的一天,一位美男子来到海边,海上狂风大作.美男子希望在海中找到美人鱼 ,但是很不幸他只找到了章鱼 ...
- # 「银联初赛第一场」自学图论的码队弟弟(dfs找环+巧解n个二元一次方程)
「银联初赛第一场」自学图论的码队弟弟(dfs找环+巧解n个二元一次方程) 题链 题意:n条边n个节点的连通图,边权为两个节点的权值之和,没有「自环」或「重边」,给出的图中有且只有一个包括奇数个结点的环 ...
- hdu.1198.Farm Irrigation(dfs +放大建图)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- ZOJ 2412 Farm Irrigation(DFS 条件通讯块)
意甲冠军 两个农田管内可直接连接到壳体 他们将能够共享一个水源 有11种农田 管道的位置高于一定 一个农田矩阵 问至少须要多少水源 DFS的连通块问题 两个相邻农田的管道能够直接连接的 ...
随机推荐
- Struts2 改变语言状态
只要在请求中增加 request_locale=en_US 参数,就可以实现语言的切换,内部由拦截器实现
- Proteus中常用元器件名字
运放 OPAMP 稳压管 zener MOS管 IRC 串口 COMPIM
- [BZOJ1433][luogu_P2055][ZJOI2009]假期的宿舍
[BZOJ1433][luogu_P2055][ZJOI2009]假期的宿舍 试题描述 输入 输出 输入示例 1 3 1 1 0 0 1 0 0 1 1 1 0 0 1 0 0 输出示例 ^_^ 数据 ...
- BZOJ4540 [Hnoi2016]序列 【莫队 + ST表 + 单调栈】
题目 给定长度为n的序列:a1,a2,-,an,记为a[1:n].类似地,a[l:r](1≤l≤r≤N)是指序列:al,al+1,-,ar- 1,ar.若1≤l≤s≤t≤r≤n,则称a[s:t]是a[ ...
- 0-Android使用Ashmem机制进行跨进程共享内存
Android使用Ashmem机制进行跨进程共享内存 来源: http://blog.csdn.net/luoshengyang/article/details/6651971 导语: 在Androi ...
- pat 甲级 1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- 洛谷 P1072 Hankson 的趣味题
题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫 Hankson.现 在,刚刚放学回家的 Hankson 正在思考一个有趣的问题. 今天在课堂上,老师讲 ...
- cookie中存储json
原文发布时间为:2009-12-14 -- 来源于本人的百度文章 [由搬家工具导入] http://www.denisdeng.com/?p=563 最近的一个项目需要在cookie中存储json对象 ...
- js scrollTop, 滚动条操作
设置页面加载时滚动条自动滚到底的方法: jQuery: 复制代码 代码如下: $(function(){ var h = $(document).height()-$(window).height() ...
- dva脚手架 dva-cli 配置roadhogrc,antd-mobile样式按需加载 不生效的问题
1.新安装dva-cli脚手架版本0.9.2,dva版本是2.4.1,roadhogrc版本是2.4.9 roadhogrc2 与1 的区别把roadhogrc 改成了webpackrc 所以配置an ...