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. linux安装Mac的默认Monaco字体

    Monaco字体是我最喜欢的编程字体,如果你想在linux上面安装,只需要在terminal中执行: curl -kL https://raw.github.com/cstrap/monaco-fon ...

  2. RBM Formula Deduction

    Energy based Model the probability distribution (softmax function): \[p(x)=\frac{\exp(-E(x))}{\sum\l ...

  3. 总结的一些网站利于搜索引擎优化的小常识及SEO优化

    网站利于搜索引擎优化的小常识 1. 尽量用独立IP和空间原因:同IP下其他网站受罚,可能会对你站有影响.如果你的站和很多垃圾.色情站同在一个服务器,搜索引擎会喜欢吗? 2. 做不同内容网站时,避免使用 ...

  4. HD2059龟兔赛跑(DP)

    题目链接 直接拿来当贪心做了=_=,然后就懵逼了 动态规划,本弱真没想到=_= #include <iostream> #include <cstdio> #include & ...

  5. VRRP协议详解

    今天做了lvs的负载均衡的实验,竟然成功了,出乎我意料......哈哈哈哈 http://blog.csdn.net/fanlu319/article/details/7013258

  6. Git删除tag

    git tag -d v2016062101 删除本地tag git push origin --delete tag v2016062101 删除远程tag

  7. 最简单的jQuery插件

    <script src="./jquery-1.7.1.min.js"></script><script>;(function($,undefi ...

  8. win7下如何建立ftp服务器

    前段时间正在做一个项目,需要上传东西到ftp服务器,纠结于如何建立ftp服务器.经过一番摸索.终于成功建立ftp服务器.现将我的经验跟大家分享一下.不足之处还望多多指点! 步骤/方法 首先在本地机器上 ...

  9. win7下exe提示无法正常启动(0xc0000906)

    本人遇见是 avast问题,卸了

  10. php基础语句2

    计算一个月有多少天 $count = date("t",strtotime("2014-09-01")); // 一个月有多少天