ZOJ2412 Farm Irrigation(农田灌溉) 搜索
Farm Irrigation
Time Limit: 2 Seconds Memory Limit: 65536 KB
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 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2412
简单的dfs搜索,主要就是构图,将水管状态变为数组状态,可以将水管类型字母转化成3*3的方阵。
0,1,0, 0,1,0, 0,0,0, 0,0,0, 0,1,0, 0,0,0, 0,1,0, 0,1,0, 0,0,0, 0,1,0, 0,1,0,
1,1,0, 0,1,1, 1,1,0, 0,1,1, 0,1,0, 1,1,1, 1,1,1, 1,1,0, 1,1,1, 0,1,1, 1,1,1,
0,0,0, 0,0,0, 0,1,0, 0,1,0, 0,1,0, 0,0,0, 0,0,0, 0,1,0, 0,1,0, 0,1,0, 0,1,0
A B C D E F G H I J K
构图成功后就是简单的dfs。类似题目有HDOJ1241,HDOJ1312,POJ1562,POJ2386,POJ1979
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m;
int farm[155][155];
int dir[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};
int x[11][9]=
{
0,1,0,1,1,0,0,0,0,
0,1,0,0,1,1,0,0,0,
0,0,0,1,1,0,0,1,0,
0,0,0,0,1,1,0,1,0,
0,1,0,0,1,0,0,1,0,
0,0,0,1,1,1,0,0,0,
0,1,0,1,1,1,0,0,0,
0,1,0,1,1,0,0,1,0,
0,0,0,1,1,1,0,1,0,
0,1,0,0,1,1,0,1,0,
0,1,0,1,1,1,0,1,0
};
void build(char c,int signi,int signj);
void dfs(int x,int y);
int main()
{
int i,j;
char c;
while(scanf("%d%d",&n,&m)&&n>=0&&m>=0)
{
getchar();
memset(farm,0,sizeof(farm));
for(i=1; i<n*3-1; i+=3)
{
for(j=1; j<m*3-1; j+=3)
{
scanf("%c",&c);
build(c,i,j);
//cout<<i<<" "<<j<<endl;
}
getchar();
}
/*
cout<<endl;
for(i=0; i<n*3; i++)
{
for(j=0; j<m*3; j++)
printf("%d",farm[i][j]);
printf("\n");
}
cout<<endl;
*/
int sum=0;
for(i=0; i<n*3; i++)
for(j=0; j<m*3; j++)
if(farm[i][j]==1)
{
farm[i][j]=0;
sum++;
dfs(i,j);
}
cout<<sum<<endl;
}
return 0;
}
void build(char c,int signi,int signj)
{
int i,j;
int k,t=0;
k=c-65;
for(i=signi-1; i<=signi+1; i++)
{
for(j=signj-1; j<=signj+1; j++)
{
farm[i][j]=x[k][t++];
//printf("%d",farm[i][j]);
}
//printf("\n");
}
}
void dfs(int x,int y)
{
int i,fx,fy;
for(i=0; i<4; i++)
{
fx=x+dir[i][0];
fy=y+dir[i][1];
if(fx>=0&&fx<n*3&&fy>=0&&fy<m*3&&farm[fx][fy]==1)
{
farm[fx][fy]=0;
dfs(fx,fy);
}
}
}
ZOJ2412 Farm Irrigation(农田灌溉) 搜索的更多相关文章
- ZOJ 2412 Farm Irrigation
Farm Irrigation Time Limit: 2 Seconds Memory Limit: 65536 KB Benny has a spacious farm land to ...
- ZOJ 2412 Farm Irrigation(DFS 条件通讯块)
意甲冠军 两个农田管内可直接连接到壳体 他们将能够共享一个水源 有11种农田 管道的位置高于一定 一个农田矩阵 问至少须要多少水源 DFS的连通块问题 两个相邻农田的管道能够直接连接的 ...
- HDUOJ--------(1198)Farm Irrigation
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1198 Farm Irrigation(并查集,自己构造连通条件或者dfs)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu.1198.Farm Irrigation(dfs +放大建图)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU1198水管并查集Farm Irrigation
Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot ...
- HDU 1198 Farm Irrigation (并检查集合 和 dfs两种实现)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 【简单并查集】Farm Irrigation
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
- Farm Irrigation
题目:Farm Irrigation 题目链接:http://210.34.193.66:8080/vj/Problem.jsp?pid=1494 题目思路:并查集 #include<stdio ...
随机推荐
- ajax 执行代码顺序
异步:ajax执行过程中,ajax后面的代码也执行了,程序没按顺序走 同步:ajax执行完毕后再执行后面的代码,程序顺序执行 在jq中ajax默认是异步的 当设置async:false表示的就是同步的 ...
- PowerDesigner导入sql脚本生成物理模型
https://www.cnblogs.com/zsswpb/p/5771623.html
- Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.0.2:resources 在我的myeclipse中新建maven工程总出现这个问题
只需在下图中更改代码 更改后是这样的: <plugin> <groupId>org.apache.maven.plugins</groupId> <artif ...
- java工程师基础笔试题(一)-参考答案
一.选择和填空 (不定项哦!) 1,如下是一份文件名为Test2.java的源文件,请问,编译该文件之后会生成几份字节码文件 class Test{ class Inner{} static cla ...
- win10 壁纸路径
C:\用户\用户名\AppData\Roaming\Microsoft\Windows\Themes\CachedFiles 原文: https://blog.csdn.net/qq_35040828 ...
- msf客户端渗透(四):关闭UAC,hash、防火墙、服务、磁盘、DEP、防病毒、远程桌面、桌面截图、获取tooken
关闭UAC 如果你获得一个system权限的session 进入到这个session 进入到shell 依次输入以下命令 cmd.exe /k %windir%\System32\reg.exe AD ...
- 解题6(OutputNMin)
题目描述 输入n个整数,输出其中最小的k个. 详细描述: 接口说明 原型: bool GetMinK(unsignedint uiInputNum, int * pInputArray, unsign ...
- 已知一个函数rand7()能够生成1-7的随机数,请给出一个函数rand10(),该函数能够生成1-10的随机数。
题目: 已知一个函数rand7()能够生成1-7的随机数,请给出一个函数,该函数能够生成1-10的随机数. 思路: 假如已知一个函数能够生成1-49的随机数,那么如何以此生成1-10的随机数呢? 解法 ...
- HDU5532 Almost Sorted Array(最长上升子序列 or 瞎搞个做差的数组)
题目链接:点我 题意:给定一个序列,询问是否能删除一个数让它成为非递减或者非递增的序列. 比如说 删除后的序列是1 3 3 5 或者5 3 3 1 或者1 3 5 或者5 3 1 都可以.只要满足删掉 ...
- roof
roof - 必应词典 美[ruf]英[ruːf] n.屋顶:车顶:顶部:有…顶的 v.给…盖顶:盖上屋顶 网络房顶:楼顶:屋脊 变形复数:roofs:过去分词:roofed:现在分词:roofing ...