Farm Irrigation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6590    Accepted Submission(s): 2838

Problem 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.Figure 1Benny 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 2Several 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
 #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 +放大建图)的更多相关文章

  1. HDU 1198 Farm Irrigation(状态压缩+DFS)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1198 题目: Farm Irrigation Time Limit: 2000/1000 MS (Ja ...

  2. HDU 1198 Farm Irrigation (并检查集合 和 dfs两种实现)

    Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. HDU 1198 Farm Irrigation(并查集,自己构造连通条件或者dfs)

    Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  4. HDU 1198 Farm Irrigation(并查集+位运算)

    Farm Irrigation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tot ...

  5. hdu 1198 Farm Irrigation(深搜dfs || 并查集)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm ...

  6. hdu 1198 Farm Irrigation

    令人蛋疼的并查集…… 我居然做了大量的枚举,居然过了,我越来越佩服自己了 这个题有些像一个叫做“水管工”的游戏.给你一个m*n的图,每个单位可以有11种选择,然后相邻两个图只有都和对方连接,才判断他们 ...

  7. HDU 1198 Farm Irrigation (并查集优化,构图)

    本题和HDU畅通project类似.仅仅只是畅通project给出了数的连通关系, 而此题须要自己推断连通关系,即两个水管能否够连接到一起,也是本题的难点所在. 记录状态.不断combine(),注意 ...

  8. hdu 1198 Farm Irrigation(并查集)

    题意: Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a ...

  9. ZOJ 2412 Farm Irrigation(DFS 条件通讯块)

    意甲冠军  两个农田管内可直接连接到壳体  他们将能够共享一个水源   有11种农田  管道的位置高于一定  一个农田矩阵  问至少须要多少水源 DFS的连通块问题  两个相邻农田的管道能够直接连接的 ...

随机推荐

  1. androidstudio 之 svn配置 汇总

    http://www.cnblogs.com/shaocm/p/4182380.html https://www.zhihu.com/question/32298079 http://www.it16 ...

  2. Linux/UNIX 定时任务 cron 详解

    定时任务( job)被用于安排那些需要被周期性执行的命令.利用它,你可以配置某些命令或者脚本,让它们在某个设定的时间内周期性地运行.cron 是 Linux 或者类 Unix 系统中最为实用的工具之一 ...

  3. cobbler深入学习

    cobbler重要目录和cobbler各对象的关系 /var/www/cobbler/ks_mirror 存放操作系统镜像/var/www/cobbler/repo_mirror 存放仓库镜像/var ...

  4. ionic中获取坐标方法

    ionic中获取坐标的方法 1.首相需要执行命令: cordova plugin add cordova-plugin-geolocation2.然后块级注入配置bower文件引入ngCordova ...

  5. Windows系统自带工具的 cmd 命令

    目标 与计算机高手无关,只是为了减少鼠标点击的次数,提高效率. 适用范围 Windows XP,Windows 7,Window 8 (在Windows 7 下验证通过.) 使用方法 在 “运行“ 对 ...

  6. js021-Ajax与Comet

    js021-Ajax与Comet 本章内容: 使用XMLHttpRequet对象 使用XMLHttpRequet事件 跨域Ajax通信的限制 Ajax技术的核心是XMLHttpRequet对象 21. ...

  7. Centos系统查看CPU有关信息

    top命令按1,看到几个CPU就代表是几核的. 查看CPU有几颗逻辑cpu,4代表有4个逻辑CPU,同时CPU的型号也打印出了,服务器一般都是至强的CPU [root@svn ~]# cat /pro ...

  8. (转)JS Date格式化为yyyy-MM-dd类字符串

    Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, //month &quo ...

  9. ASP.NET Padding Oracle Attack EXP

    #!/usr/bin/perl## PadBuster v0.3 - Automated script for performing Padding Oracle attacks# Brian Hol ...

  10. CentOs图形界面的开启与关闭

    1.1 shell中运行 init 3  进入文本模式,同时会关闭相关的服务(Xserver 肯定关闭) 1.2 Alt+Ctrl+F1~F6到字符界面,root登陆,ps aux|grep /usr ...