题目链接:

  http://acm.hdu.edu.cn/showproblem.php?pid=1198

题目大意:

  有一大块土地需要浇水,这块土地由很多的小块土地(有十一种)组成,小块土地上有水沟,问至少需要建几个井,才能灌溉这一大片土地?

11种土地,编号从A--K,

解题思路:

  这个题目可以用很多种方法,可以用并查集,找出一共有多少个集合,也可以用dfs求连通块,方法并不难,重在模型的转化,我用4个0/1数字代表一块土地,

并查集代码:

  

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 25010
 4 int a[N];
 5 int b[15][5] = {{1,1,0,0},{0,1,1,0},{1,0,0,1},{0,0,1,1,},{0,1,0,1},{1,0,1,0},{1,1,1,0},{1,1,0,1},{1,0,1,1},{0,1,1,1},{1,1,1,1,}};
 6
 7 int cha (int i)
 8 {
 9     if ( a[i] != i )
10         return cha ( a[i] );
11     return i;
12 }
13
14 int main ()
15 {
16     int i, j, n, m, num, x, y;
17     char map[55][55];
18     while ( scanf ("%d %d", &n, &m), m > 0 && n > 0 )
19     {
20         for (i=0; i<n; i++)
21         {
22             getchar ();
23             for (j=0; j<m; j++)
24                 scanf ("%c", &map[i][j]);
25         }
26         for (i=0; i<n*m; i++)
27             a[i] =i;
28         for (i=0; i<n; i++)
29             for (j=0; j<m; j++)
30             {
31                 if (i - 1 >= 0)
32                 {
33                     if ( b[ map[i-1][j]-'A' ][3] && b[ map[i][j]-'A' ][1] )
34                     {
35                         x =  cha ( (i-1)*m+j );
36                         y =  cha ( i*m+j );
37                         if (x != y)
38                             a[x] = a[y];
39                     }
40                 }
41                 if (j - 1 >= 0)
42                 {
43                     if ( b[ map[i][j-1]-'A' ][2] && b[ map[i][j]-'A' ][0] )
44                     {
45                         x = cha ( i*m+j-1 );
46                         y = cha ( i*m+j );
47                         if (x != y)
48                             a[x] = y;
49                     }
50                 }
51             }
52             num = 0;
53             for (i=0; i<n*m; i++)
54                 if ( a[i] == i )
55                     num ++;
56                 printf ("%d\n", num);
57     }
58     return 0;
59 }

bfs求连通块代码:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6
 7 #define maxn 105
 8
 9 int G[maxn][maxn], M, N;
10 int v[maxn][maxn];
11
12 int dir[4][2]={ {-1,0},{0,1},{1,0},{0,-1}};
13 int f[4] = {2, 3, 0, 1};
14 int dir1[11][4] =
15 {
16     {1,0,0,1},{1,1,0,0},{0,0,1,1},{0,1,1,0},
17     {1,0,1,0},{0,1,0,1},{1,1,0,1},{1,0,1,1},
18     {0,1,1,1},{1,1,1,0},{1,1,1,1}
19 };
20
21 void DFS(int i, int j)
22 {
23     int nx, ny, k;
24
25     v[i][j] = -1;
26
27     for(k=0; k<4; k++)
28     {
29         nx = dir[k][0] + i;
30         ny = dir[k][1] + j;
31         int p = f[k];
32         if( nx>=0&&nx<M && ny>=0&&ny<N && !v[nx][ny] && dir1[ G[i][j] ][k] && dir1[ G[nx][ny] ][p])
33         {
34             DFS(nx, ny);
35         }
36     }
37 }
38
39 int main()
40 {
41     while(scanf("%d%d", &M, &N), M != -1)
42     {
43         int i, j, ans = 0;
44         char ch;
45
46         memset(v, 0, sizeof(v));
47
48         for(i=0; i<M; i++)
49         for(j=0; j<N; j++)
50         {
51             cin >> ch;
52             G[i][j] = ch - 'A';
53         }
54
55         for(i=0; i<M; i++)
56         for(j=0; j<N; j++)
57         {
58             if(v[i][j] == 0)
59             {
60                 DFS(i, j);
61                 ans++;
62             }
63         }
64         cout << ans <<endl;
65     }
66
67     return 0;
68 }

  

转载于:https://www.cnblogs.com/alihenaixiao/p/4429280.html

hdu 1198 Farm Irrigation相关推荐

  1. HDU 1198 Farm Irrigation

    题目大意:给你地图,让你判断需要多少水才可以将农场灌满. 题解:显然用并查集比较容易,将可以连通的并起来,最后输出连通块的数目即可,一开始我用字母分类讨论发现很麻烦,于是参考别人的博客发现,直接自己写 ...

  2. hdu1198 Farm Irrigation —— dfs or 并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1198 dfs: 1 #include<cstdio>//hdu1198 dfs 2 #in ...

  3. hdu 1198农田灌溉

    通过这道题,我只想要说图论的题,建好了图,题就解了一大半了... 这道题建好图后,dfs暴搜就行了... View Code 1 #include<iostream> 2 #include ...

  4. 杭电OJ分类题目(4)-Graph

    原题出处:HDOJ Problem Index by Type,http://acm.hdu.edu.cn/typeclass.php 杭电OJ分类题目(4) HDU Graph Theory - U ...

  5. 杭电OJ分类题目(1)

    原题出处:HDOJ Problem Index by Type,http://acm.hdu.edu.cn/typeclass.php 杭电OJ分类题目(1) HDU Introduction HDU ...

  6. 并查集入门+初级专题训练

    介绍   摘自罗勇军,郭卫斌的<算法竞赛入门到进阶>上的说明:   并查集(Disjoint Set)是一种非常精巧而且食用的数据结构,它主要用于处理一些不相交集合的合并问题.经典的例子有 ...

  7. 【转载】图论 500题——主要为hdu/poj/zoj

    转自--http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  8. 【HDOJ图论题集】【转】

    1 =============================以下是最小生成树+并查集====================================== 2 [HDU] 3 1213 How ...

  9. 一系列图论问题[转]

    =============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...

最新文章

  1. Python for虚幻引擎编辑器工具脚本学习教程
  2. matlab统计矩阵元素的出现次数
  3. Longest X 贪心,滑动窗口,前缀和(400)
  4. mysql 层_mysql三层体系
  5. [新功能]Blog首页仅列出标题
  6. 深度学习算法_深度学习算法
  7. 泸州NGN属南气矿工程----N2000网管系统提示连接数据库失败
  8. 安装ruby1.9.3-p0及redmon来监控redis
  9. vi 的完整指令说明 -- YenYen 整理
  10. ] 爆笑囧人囧事2009大合集![
  11. How add nic driver to initrd
  12. php 小偷程序 图片,一个图片地址分解程序(用于PHP小偷程序)
  13. 51单片机学习——PWM
  14. vue插件开发、文档书写、github发布、npm包发布一波流
  15. iview DatePicker日期组件禁止选择今天之后的日期 不包括今天
  16. web作业—简历信息管理系统
  17. 要怎么做才能实现工厂智慧物流体系的建设
  18. MySql函数DATE_FORMAT( )基本用法
  19. 在江西景德镇,连垃圾桶都是青花瓷的。。。
  20. WC2015 酱油记

热门文章

  1. 在银行存100万,如果银行倒闭,超过50万部分是不是拿不回来?
  2. 外部 Storage Provider - 每天5分钟玩转 Docker 容器技术(149)
  3. 工程应用中的自相关操作
  4. mysql 8.0数据备份恢复_MySQL 8.0 增强逻辑备份恢复工具介绍-爱可生
  5. ajax post提交数据_详解Ajax异步加载
  6. 移动web前端开发框架_移动前端开发是Web前端开发吗?
  7. mysql查询并设置高亮_慢查询分析调优工具~mysqldumpslow
  8. python英文词云代码_使用python实现个性化词云的方法
  9. python 当前目录_virtualenvwrapper打造多版本Python环境
  10. xilinx c语言编程,使用Xilinx SDSoc在Xilinx zcu102开发板上编程HelloWorld