转载请注明出处:

viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1198

----------------------------------------------------------------------------------------------------------------------------------------------------------
欢迎光临天资小屋:http://user.qzone.qq.com/593830943/main

----------------------------------------------------------------------------------------------------------------------------------------------------------

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 1


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






Figure 2




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?



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

题意:有如上图11种土地块,块中的绿色线条为土地块中修好的水渠,如今一片土地由上述的各种土地块组成。须要浇水,问须要打多少口井。

代码一(dfs):

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define TM 117
//存储11中类型的土地。二维中的0 1 2 3分别代表这样的类型的土地的左上右下
//为1表示这个方向有接口,为0表示这个方向没有接口
int a[11][4]={{1,0,0,1},{1,1,0,0},{0,0,1,1},{0,1,1,0},{1,0,1,0},{0,1,0,1},
{1,1,0,1},{1,0,1,1},{0,1,1,1},{1,1,1,0},{1,1,1,1}};
int map[TM][TM];
char s[TM][TM];
bool vis[TM][TM];
int n, m, coun;
void dfs(int x, int y)
{
vis[x][y] = true;
for(int i = 0; i < 4; i++)
{
if(i == 0)//向上查找
{
if(a[map[x][y]][0]&&a[map[x-1][y]][2]&&x-1>=0&&!vis[x-1][y])
dfs(x-1,y);
}
else if(i == 1)//向右查找
{
if(a[map[x][y]][1]&&a[map[x][y+1]][3]&&y+1<m&&!vis[x][y+1])
dfs(x,y+1);
}
else if(i == 2)//向下查找
{
if(a[map[x][y]][2]&&a[map[x+1][y]][0]&&x+1<n&&!vis[x+1][y])
dfs(x+1,y);
}
else if(i == 3)//向左查找
{
if(a[map[x][y]][3]&&a[map[x][y-1]][1]&&y-1>=0&&!vis[x][y-1])
dfs(x,y-1);
}
}
return;
}
void init()//初始化
{
memset(vis,false,sizeof(vis));
coun = 0;
}
int main()
{
int i, j;
while(cin>>n>>m)
{
init();
if(n==-1 && m==-1)
break;
for(i = 0; i < n; i++)
{
cin>>s[i];
for(j = 0; j < m; j++)
{
map[i][j] = s[i][j]-'A';
}
}
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
{
if(!vis[i][j])
{
coun++;
dfs(i,j);
}
}
}
cout<<coun<<endl;
}
return 0;
}

代码二:(并查集)

#include <iostream>
#include <algorithm>
using namespace std;
#define TM 117
//存储11中类型的土地。二维中的0 1 2 3分别代表这样的类型的土地的左上右下
//为1表示这个方向有接口。为0表示这个方向没有接口
int a[11][4]={{1,0,0,1},{1,1,0,0},{0,0,1,1},{0,1,1,0},{1,0,1,0},{0,1,0,1},
{1,1,0,1},{1,0,1,1},{0,1,1,1},{1,1,1,0},{1,1,1,1}};
struct p
{
int f;//记录每块田地水管的分布
int c;//分别对每块田地编号
}map[TM][TM];
char s[TM][TM];
int father[TM*TM];
int n, m, coun, k;
int find(int x)
{
return x==father[x]?x:father[x]=find(father[x]);
} void init()//初始化
{
for(int i = 0; i <= n*m; i++)
{
father[i] = i;
}
coun = k = 0;
} void Union(int x, int y)
{
int f1 = find(x);
int f2 = find(y);
if(f1!=f2)
{
father[f2] = f1;
}
}
void F(int x, int y)
{
for(int i = 0; i < 4; i++)
{
if(i == 0)//向上查找
{
if(a[map[x][y].f][0]&&a[map[x-1][y].f][2]&&x-1>=0)
{
Union(map[x][y].c,map[x-1][y].c);
}
}
else if(i == 1)//向右查找
{
if(a[map[x][y].f][1]&&a[map[x][y+1].f][3]&&y+1<m)
{
Union(map[x][y].c,map[x][y+1].c);
}
}
else if(i == 2)//向下查找
{
if(a[map[x][y].f][2]&&a[map[x+1][y].f][0]&&x+1<n)
{
Union(map[x][y].c,map[x+1][y].c);
}
}
else if(i == 3)//向左查找
{
if(a[map[x][y].f][3]&&a[map[x][y-1].f][1]&&y-1>=0)
{
Union(map[x][y].c,map[x][y-1].c);
}
}
}
} int main()
{
int i, j;
while(cin>>n>>m)
{
init();
if(n==-1 && m==-1)
break;
for(i = 0; i < n; i++)
{
cin>>s[i];
for(j = 0; j < m; j++)
{
map[i][j].f = s[i][j]-'A';
map[i][j].c = k++;
}
}
for(i = 0; i < n; i++)//分别对每块田地查找是否和别的田地的水管联通
{
for(j = 0; j < m; j++)
{
F(i,j);
}
}
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)//查找有多少个独立的集合
{
if(father[map[i][j].c] == map[i][j].c)
coun++;
}
}
cout<<coun<<endl;
}
return 0;
}

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)T ...

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

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

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

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

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

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

  6. hdu 1198 Farm Irrigation

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

  7. hdu 1198 Farm Irrigation(并查集)

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

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

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

  9. HDU 2553 N皇后问题(深搜DFS)

    N皇后问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

随机推荐

  1. C++设计模式实现--职责链(Chain of Responsibility)模式

    一. 概述 职责链模式: 使多个对象都有机会处理请求.从而避免请求的发送者和接收者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止. 二. 举个样例 员工要求加薪 ...

  2. PHP-php.ini中文版

    今天细看了下配置文件 有很多没用过的 就从网上搜了一篇 常看看 ;;;;;;;;;;;;;;;; 简介 ;;;;;;;;;;;;;;;;; 本文并非是对英文版 php.ini 的简单翻译,而是参考了众 ...

  3. QQ的未来在那里

    http://blog.sina.com.cn/s/blog_53bcb13e0100030g.html 早期的QQ非常清爽,没有广告,友好的界面,飞快的连接速度,为中国人量身订做的体贴功能,非常吸引 ...

  4. DotNet Core 2.0使用MySql实现Code First

    本教程使用vs2017 + dotnet core2.0 + MySql5.7.19 1.打开vs2017,文件>新建>项目,选择Asp.Net Core Web应用程序. 2.项目名称可 ...

  5. mosquitto配置文件详解

    安装完成之后,所有配置文件会被放置于/etc/mosquitto/目录下,其中最重要的就是Mosquitto的配置文件,即mosquitto.conf,以下是详细的配置参数说明. # Config f ...

  6. 类型转换运算符、*运算符重载、->运算符重载、operator new 和 operator delete

    一.类型转换运算符 必须是成员函数,不能是友元函数 没有参数 不能指定返回类型 函数原型:operator 类型名();  C++ Code  1 2 3 4 5 6 7 8 9 10 11 12 1 ...

  7. 初始化列表(const和引用成员)、拷贝构造函数

    一.构造函数初始化列表 推荐在构造函数初始化列表中进行初始化 构造函数的执行分为两个阶段 初始化段 普通计算段 (一).对象成员及其初始化  C++ Code  1 2 3 4 5 6 7 8 9 1 ...

  8. Python 算法(1) 快速排序

    快速排序(quickSort) 快排的思想:首先任意选取一个数据(通常选用数组的第一个数)作为关键数据,然后将所有比它小的数都放到它前面,所有比它大的数都放到它后面,这个过程称为一趟快速排序. 百度百 ...

  9. IIS目录禁止执行权限

    IIS6: IIS7:

  10. 【Material Design视觉设计语言】应用布局设计

    [Material Design视觉设计语言]章节列表 [Material Design视觉设计语言]开篇 [Material Design视觉设计语言]Material Design设计概述 [Ma ...