Farm Irrigation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4491    Accepted Submission(s): 1947

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 likeFigure 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
 
Author
ZHENG, Lu
 
Source
 
Recommend
Ignatius.L
 
代码:

 /*龚细军 宽度优先搜索bfs*/
#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
typedef struct G
{
/*true 代表此处有piper*/
bool up,down,left,right;
}gong;
struct point
{
int x;
int y;
}start;
//依次代表A~K的管道特性;
gong go[]={,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,}; /*前行的步奏*/
char map[][];
int n,ans,m;
void input()
{
memset(map,'\0',sizeof(map));
for(int i=;i<m;i++)
scanf("%s",map[i]);
}
void dfs()
{
/*判断是否联通,这样便于构造重位*/
bool isrr,isll,isuu,isdd;
queue<point> q;
point p1,p2;
q.push(start);
bool isbian=false;
while(!q.empty())
{
isrr=isll=isuu=isdd=false;
p1=q.front();
q.pop();
/*向下搜索*/
if(p1.x+<m&&map[p1.x+][p1.y]>='A'&&go[map[p1.x][p1.y]-'A'].down!=&&go[map[p1.x+][p1.y]-'A'].up!=)
{
p2.x=p1.x+;
p2.y=p1.y;
q.push(p2);
isdd=true; //说明经过这里;
}
/*向上搜索*/
if(p1.x>=&&map[p1.x-][p1.y]>='A'&&go[map[p1.x][p1.y]-'A'].up!=&&go[map[p1.x-][p1.y]-'A'].down!=)
{
p2.x=p1.x-;
p2.y=p1.y;
q.push(p2);
isuu=true;
}
/*向左搜索*/
if(p1.y>=&&map[p1.x][p1.y-]>='A'&&go[map[p1.x][p1.y]-'A'].left!=&&go[map[p1.x][p1.y-]-'A'].right!=)
{
p2.x=p1.x;
p2.y=p1.y-;
q.push(p2);
isll=true;
}
/*向右搜索*/
if(p1.y+<n&&map[p1.x][p1.y+]>='A'&&go[map[p1.x][p1.y]-'A'].right!=&&go[map[p1.x][p1.y+]-'A'].left!=)
{
p2.x=p1.x;
p2.y=p1.y+;
q.push(p2);
isrr=true;
}
if(isuu||isll||isrr||isdd)
{
map[p1.x][p1.y]='';
}
else
if((p1.x>=&&map[p1.x-][p1.y]=='')||(p1.x+<m&&map[p1.x+][p1.y]=='')
||(p1.y>=&&map[p1.x][p1.y-]=='')||(p1.y+<n&&map[p1.x][p1.y+]==''))
{
if(map[p1.x][p1.y]!='')
{
isbian=true;
map[p1.x][p1.y]='';
}
} else ans++;
}
if(isbian) ans++;
}
int main()
{
int i,j; /* for(i=0;i<11;i++)
{
printf("%d %d %d %d\n",go[i].up,go[i].down,go[i].left,go[i].right);
}*/ while(scanf("%d%d",&m,&n),(m!=-||n!=-))
{
ans=;
input( );
for(i=;i<m;i++)
{
for(j=;j<n;j++)
{
if(map[i][j]>='A')
{
start.x=i;
start.y=j;
dfs();
}
}
}
printf("%d\n",ans);
}
return ;
}
 

HDUOJ--------(1198)Farm Irrigation的更多相关文章

  1. (DFS)hdoj1198-Farm Irrigation

    题目链接 DFS的简单应用,比较繁琐的是处理输入的英文字母.用并查集也可以做(可是笔者现在还没有掌握并查集,之前只用过一次,以后学会回来补上) #include<cstdio> #incl ...

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

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

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

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

  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(并查集,自己构造连通条件或者dfs)

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

  6. Farm Irrigation(非常有意思的并查集)

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

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

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

  8. Web Fram 2 for IIS7.X(Microsoft Web Farm Framework)

    Microsoft Web Farm Framework (WFF) 2.0 是微软开发的.基于IIS 7.x的小插件,能够帮助我们轻松实现Web网站的高性能.高可用性,用来在Web服务器群上提供和管 ...

  9. IIS负载均衡-Application Request Route详解第二篇:创建与配置Server Farm(转载)

    IIS负载均衡-Application Request Route详解第二篇:创建与配置Server Farm 自从本系列发布之后,收到了很多的朋友的回复!非常感谢,同时很多朋友问到了一些问题,有些问 ...

随机推荐

  1. 进程外Session保存和全局文件错误捕获

    Session深入学习,进程外的Session 当用户登入页面跳转时候,我们会将用户登录信息保存在服务端一个键值对的Session(Session池)中.那么Session池又是在哪里呢? 它最终默认 ...

  2. Asp.Net中自以为是的Encode

    Asp.Net 引擎可能是不错,但是它把程序员想的太笨,会自以为是做很多自动的 Encode 和 Decode,以下文举例: 如果客户端我们 post 了如下的数据, 但是你实际得到的是: 也就是说, ...

  3. eclipse svn 配置

    1.安装svn http://subversion.apache.org/ 到apache网站上下载svn 2.在eclipse中安装svn插件 在eclipse->help->eclip ...

  4. C# WCF 完整实例,winform 窗体作为 宿主

    上一次提到,我们的WCF程序宿主是发布到IIS上面的.虽然这样做未尝不可,不过不便于我们进行“开始”或“停止”WCF服务的操作.所以再次尝试了编写以窗体应用程序作为WCF服务宿主的方式,并取得了成功. ...

  5. java 解析 XML实例

    package com.hseact.fecp.servlet; import java.io.IOException; import javax.xml.parsers.DocumentBuilde ...

  6. STL sort 函数实现详解 ZZ

    前几天阿里电话一面,被问到STL中sort函数的实现.以前没有仔细探究过,听人说是快速排序,于是回答说用快速排序实现的,但听电话另一端面试官的声音,感觉不对劲,知道自己回答错了.这几天特意看了一下,在 ...

  7. android 创建通知栏Notification

    ///// 第一步:获取NotificationManager NotificationManager nm = (NotificationManager) getSystemService(Cont ...

  8. SELinux安全系统基础

    一.SELinux简介 SELinux(Secure Enhanced Linux)安全增强的Linux是由美国国家安全局NSA针对计算机基础结构安全开发的一个全新的Linux安全策略机制.SELin ...

  9. 2017.8.30 elasticsearch-sql的安装与使用

    参考来自: http://blog.csdn.net/u012307002/article/details/52837756 https://github.com/NLPchina/elasticse ...

  10. IStat Menus 5.02 5.03 的注册码

    1574-5977-7956-8062-0000 6015-5448-3282-4975-0000 9665-5955-6856-2071-0000 2447-9517-7939-5221-0000