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的连通块问题 两个相邻农田的管道能够直接连接的 ...
随机推荐
- 树链剖分I 原理
树链剖分(Heavy Light Decomposition, HLD)是一种将对[树上两点间的路径]上[边或点]的[修改与查询]转化到[序列]上来处理的方法. 目的:将树的边或点转化到一个线性结构( ...
- System.IO.Directory.Delete目录删除
在程序运行的时候,如果直接获取一个目录路径,然后执行删除(包括子目录及文件): System.IO.Directory.Delete(path,true); 或者 System.IO.Director ...
- K米测评
第一部分 调研,评测 K米APP使用体验 从小五音不全,所以KTV这类地方我是能不去就不去的,更别说为了点个歌去下个APP,所以K米是我第一个点歌类APP.说说第一次上手的体验吧,APP颜值一般吧,U ...
- iOS - 如何自动播放H5中的音频
场景:iOS端设备,App页面跳转到H5产品介绍,背景音乐无法播放.(为什么不能自动播放,因该是iPhone人性化设定吧~) 加载H5用UIWebView空间: 代码: CGRect rect = s ...
- 《Java疯狂讲义》(第3版)学习笔记 2 - Java语言的运行机制
内容 1.高级语言的运行机制 2.Java 语言的运行机制 1.高级语言的运行机制 高级语言主要分为编译型语言和解释型语言两类. 编译型语言是指使用专门的编译器.针对特定平台(操作系统)将高级语言源代 ...
- idea community 配置已有的scala工程
- Foundation框架—— 数组 (NSArray NSMutableArray )
基础知识回顾 1.在给可变数组添加元素时,要保证该数组已被初始化 2.在遍历可变数组时,不能对其进行增删改 3.NSMutableArray继承自NSArray,几乎拥有NSArray的一切方法. 4 ...
- 使用RawSocket进行网络抓包
aw socket,即原始套接字,可以接收本机网卡上的数据帧或者数据包,对与监听网络的流量和分析是很有作用的,一共可以有3种方式创建这种socket. 中文名 原始套接字 外文名 RAW SOCKET ...
- php 下载文件的函数
通过函数完成下载文件的PHP功能代码 function download($url, $filename) { // 获得文件大小, 防止超过2G的文件, 用sprintf来读 $filesize = ...
- Realm简单使用小记
一.项目环境:纯OC 载入Realm: pod 'Realm' 二.为了方便调用可以写一个Realm类的分类 #import <Foundation/Foundation.h> #impo ...