hdu.1198.Farm Irrigation(dfs +放大建图)
Farm Irrigation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6590 Accepted Submission(s): 2838

ADC FJK IHE
then the water pipes are distributed like

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.
DK
HF
3 3
ADC
FJK
IHE
-1 -1
3
#include<stdio.h>
#include<string.h>
const int M = ;
char map[M][M] ;
bool a[M * ][M * ] , vis[M * ][M * ];
int move[][] = {{,} , {-,} , {,} , {,-}} ;
int n , m ; void build ()
{
memset (a , , sizeof(a)) ;
memset (vis , , sizeof(vis)) ;
for (int i = ; i < n ; i ++) {
for (int j = ; j < m ; j ++) {
int x = i * + , y = j * + ;
// printf ("(%d,%d)\n" , x , y ) ;
a[x][y] = ;
if (map[i][j] == 'A') {
a[x][y - ] = ;
a[x - ][y] = ;
}
else if (map[i][j] == 'B') {
a[x - ][y] = ;
a[x][y + ] = ;
}
else if (map[i][j] == 'C') {
a[x + ][y] = ;
a[x][y - ] = ;
}
else if (map[i][j] == 'D') {
a[x + ][y] = ;
a[x][y + ] = ;
}
else if (map[i][j] == 'E') {
a[x + ][y] = ;
a[x - ][y] = ;
}
else if (map[i][j] == 'F') {
a[x][y + ] = ;
a[x][y - ] = ;
}
else if (map[i][j] == 'G') {
a[x][y - ] = ;
a[x][y + ] = ;
a[x - ][y] = ;
}
else if (map[i][j] == 'H') {
a[x][y - ] = ;
a[x - ][y] = ;
a[x + ][y] = ;
}
else if (map[i][j] == 'I') {
a[x][y - ] = ;
a[x][y + ] = ;
a[x + ][y] = ;
}
else if (map[i][j] == 'J') {
a[x - ][y] = ;
a[x + ][y] = ;
a[x][y + ] = ;
}
else if (map[i][j] == 'K') {
a[x - ][y] = ;
a[x + ][y] = ;
a[x][y - ] = ;
a[x][y + ] = ;
}
}
}
for (int i = ; i < n * ; i ++) {
for (int j = ; j < m * ; j ++) {
if (a[i][j] == )
vis[i][j] = ;
}
}
//printf ("n=%d,m=%d\n" , n , m );
/* for (int i = 0 ; i < n * 3 ; i ++) {
for (int j = 0 ; j < m * 3 ; j ++) {
printf ("%d " , a[i][j]) ;
} puts ("") ;
}*/
} void dfs (int sx , int sy)
{
vis[sx][sy] = ;
int x , y ;
for (int i = ; i < ; i ++) {
x = sx + move[i][] ; y = sy + move[i][] ;
if (a[x][y] && !vis[x][y]) {
dfs (x , y) ;
}
}
} int main ()
{
freopen ("a.txt" , "r" , stdin ) ;
while (~ scanf ("%d%d" , &n , &m ) ) {
if (n == - && m == -) break ;
for (int i = ; i < n; i ++) scanf ("%s" , map[i] ) ; // puts (map[i]);
build () ;
int cnt = ;
for (int i = ; i < * n ; i ++) {
for (int j = ; j < * m ; j ++) {
if ( !vis[i][j] && a[i][j]) {
dfs (i , j) ;
cnt ++;
}
}
}
printf ("%d\n" , cnt ) ;
}
return ;
}
把地图放大3倍,建成一个0,1图
hdu.1198.Farm Irrigation(dfs +放大建图)的更多相关文章
- HDU 1198 Farm Irrigation(状态压缩+DFS)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1198 题目: Farm Irrigation Time Limit: 2000/1000 MS (Ja ...
- HDU 1198 Farm Irrigation (并检查集合 和 dfs两种实现)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 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(并查集+位运算)
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
- hdu 1198 Farm Irrigation(深搜dfs || 并查集)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm ...
- hdu 1198 Farm Irrigation
令人蛋疼的并查集…… 我居然做了大量的枚举,居然过了,我越来越佩服自己了 这个题有些像一个叫做“水管工”的游戏.给你一个m*n的图,每个单位可以有11种选择,然后相邻两个图只有都和对方连接,才判断他们 ...
- HDU 1198 Farm Irrigation (并查集优化,构图)
本题和HDU畅通project类似.仅仅只是畅通project给出了数的连通关系, 而此题须要自己推断连通关系,即两个水管能否够连接到一起,也是本题的难点所在. 记录状态.不断combine(),注意 ...
- hdu 1198 Farm Irrigation(并查集)
题意: Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a ...
- ZOJ 2412 Farm Irrigation(DFS 条件通讯块)
意甲冠军 两个农田管内可直接连接到壳体 他们将能够共享一个水源 有11种农田 管道的位置高于一定 一个农田矩阵 问至少须要多少水源 DFS的连通块问题 两个相邻农田的管道能够直接连接的 ...
随机推荐
- python dict.get()和dict['key']的区别
先看代码: In [1]: a = {'name': 'wang'} In [2]: a.get('age') In [3]: a['age'] --------------------------- ...
- DropZone
JavaScript 文件拖拽上传插件 dropzone.js 介绍 February 19, 2014 / 编程指南 dropzone.js 是一个开源的 JavaScript 库,提供 AJAX ...
- python标准模块(二)
本文会涉及到的模块: json.pickle urllib.Requests xml.etree configparser shutil.zipfile.tarfile 1. json & p ...
- UVA1213Sum of Different Primes(素数打表 + DP)
题目链接 题意:选择k个素数,使得和为N(1120)的方案数: 筛选出 <= N 的素数,然后就背包 写的时候没初始dp[0][0] = 1;而且方案数也没相加,真是弱逼 #include &l ...
- liunx 的 grep命令(转载)
简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它 ...
- uC/OS-II内核的服务文件
/*************************************************************************************************** ...
- asp.net与Matlab类型转换(待补全)
上上篇的博客已经提到如何配置环境,即如何在asp.net中调用matlab生成的dll文件.这篇博客打算做个笔记,那就是matlab和C#数据类型如何转换.随着需求的增加,我会不断增加新的类型转换. ...
- .net4.0及Silverlight_Tools for vs2008sp1安装失败解决办法
安装.net framework 4.0失败,出现HRESULT 0xc8000222错误代码 1.开始-运行-输入cmd,运行命令 net stop WuAuServ 2.开始-运行-输入 ...
- 编译安装chkrootkit出现的问题
tar xf chkrootkit.tar.gz cd chkrootkit-* make sense的时候出现make: *** [strings-static] Error 1,解决办法:yum ...
- 快捷键_Mac
苹果Mac系统常用快捷键 Command+Tab 任意情况下切换应用程序 - 向前循环 Shift+Command+Tab 切换应用程序 - 向后循环 Command+L 当前程序是浏览器时,可以直接 ...