题意:

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.

Benny 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

Several 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?

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.

思路:

并查集,看代码。

代码:

struct node{
bool up,down,left,right;
}
a[2505]; int m,n;
char graph[55][55];
int fa[2505]; int findFa(int x){
return fa[x]==x?fa[x]:fa[x]=findFa(fa[x]);
} void Union(int x,int y){
int fx=findFa(x);
int fy=findFa(y);
if(fx!=fy){
fa[fx]=fy;
}
} int main(){ while(scanf("%d%d",&m,&n),m>0||n>0){
rep(i,0,m-1) scanf("%s",graph[i]); rep(i,1,m*n) a[i].up=a[i].down=a[i].left=a[i].right=false; rep(i,0,m-1){
rep(j,0,n-1){
switch(graph[i][j]){
case 'A':a[i*n+j+1].up=true; a[i*n+j+1].left=true; break;
case 'B':a[i*n+j+1].up=true; a[i*n+j+1].right=true; break;
case 'C':a[i*n+j+1].left=true; a[i*n+j+1].down=true; break;
case 'D':a[i*n+j+1].right=true; a[i*n+j+1].down=true; break;
case 'E':a[i*n+j+1].up=true; a[i*n+j+1].down=true; break;
case 'F':a[i*n+j+1].left=true; a[i*n+j+1].right=true; break;
case 'G':a[i*n+j+1].left=true; a[i*n+j+1].right=true; a[i*n+j+1].up=true; break;
case 'H':a[i*n+j+1].up=true; a[i*n+j+1].down=true; a[i*n+j+1].left=true; break;
case 'I':a[i*n+j+1].right=true; a[i*n+j+1].left=true; a[i*n+j+1].down=true; break;
case 'J':a[i*n+j+1].up=true; a[i*n+j+1].down=true; a[i*n+j+1].right=true; break;
case 'K':a[i*n+j+1].up=true; a[i*n+j+1].left=true; a[i*n+j+1].down=true; a[i*n+j+1].right=true; break;
default: break;
}
}
} rep(i,1,m*n) fa[i]=i;
rep(i,1,m){
rep(j,1,n){
int now=(i-1)*n+j;
if(j!=1){
int last1=(i-1)*n+j-1;
if(a[last1].right && a[now].left){
Union(last1,now);
}
}
if(i!=1){
int last2=(i-2)*n+j;
if(a[last2].down && a[now].up){
Union(last2,now);
}
}
}
} map<int,int> mp;
mp.clear();
int ans=0;
rep(i,1,m*n){
int t=findFa(i);
if(mp[t]==0){
++ans;
mp[t]=1;
}
} printf("%d\n",ans); } return 0;
}

hdu 1198 Farm Irrigation(并查集)的更多相关文章

  1. 杭电OJ——1198 Farm Irrigation (并查集)

    畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可 ...

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

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

  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)

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

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

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

  7. hdu1198 Farm Irrigation 并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1198 简单并查集 分别合并竖直方向和水平方向即可 代码: #include<iostream&g ...

  8. hdu.1198.Farm Irrigation(dfs +放大建图)

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

  9. hdu 1198 Farm Irrigation

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

随机推荐

  1. 深度探索-Redis复制

    1.前言 本文介绍了Redis复制的主要流程和设计思想.通过本文的阅读,您大致能理解复制在软件架构方面的通用思想.在阅读本文之前,希望读者首先对Redis有一定的认识,对Redis的事件类型.和事件处 ...

  2. dede新增字段调用方法

    各位在使用{dede:channel }标签的时候,难免会遇到因为现在字段不能满足业务需求,需要新增的情况(具体怎么新增字段自行百度). 但是新增的字段通过DEDE的标签是不能直接使用的,现在博主介绍 ...

  3. javascript LinkedList js 双向循环链表 Circular Linked List

    javascript LinkedList: function Node(elem, prev, next) { this.elem = elem; this.prev = prev ? prev : ...

  4. struts2 中 form-action action-form 的传参方式

    1.  struts2 Action获取表单提交数据 主要有三种方式: 1.1 使用ActionContext类 //获取actionContext对象 ActionContext context = ...

  5. cmake入门:01 构建一个简单的可执行程序

    一.目录结构 CMakeLists.txt:cmake 工程入口文件,包含当前目录下的工程组织信息.cmake 指令根据此文件生成相应的 MakeFile 文件. Hello.c: 源代码文件 bui ...

  6. 定要过python二级 第三套

    第一模块   基础操作(共三道题) 1. 安装python 包  我在c  盘打开  但是它给我安装到了d盘得  anaconda3 下面  关键是  我在c盘  打开python .exe   创建 ...

  7. AT4353-[ARC101D]Robots and Exits【LIS】

    正题 题目链接:https://www.luogu.com.cn/problem/AT4353 题目大意 数轴上有\(n\)个球\(m\)个洞,每次可以将所有球左移或者右移,球到洞的位置会掉下去. 求 ...

  8. AT4502-[AGC029C]Lexicographic constraints【二分,栈】

    正题 题目链接:https://www.luogu.com.cn/problem/AT4502 题目大意 给出\(n\)个长度\(S\),求一个最小\(m\)表示用大小为\(m\)的字符集构造出\(n ...

  9. bootstrap inputfile 使用-上传,回显

    近期用bootstrap 做前端的上传,功能涉及到上传时就是召网上的教程随便弄一搜一大把,但是做到修改页面时候不知道页面该如何回显,折腾了一阵子才完成遂记录下来希望能给看到的小伙伴有点启发吧. 首先是 ...

  10. SSA

    wikipedia上关于SSA的定义如下: In compiler design, static single assignment form (often abbreviated as SSA fo ...