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. 开启otl的64位长整数支持

    要开启OTL的64位长整数支持,必须先定义宏 #define OTL_BIGINT __int64 // VC++, Borland C++ 或者 #define OTL_BIGINT long lo ...

  2. OTL翻译(3) -- OTL的主要类

    相比于传统的C++类库而言,OTL更像是一个代码容器,里面复杂,但对外的接口简单.OTL在处理程序方面受到了STL的影响. OTL有一个模板框架,它实现了otl_stream的概念.该框架由模板类和内 ...

  3. Reverse Nodes in k-Group leetcode java

    题目: Given a linked list, reverse the nodes of a linked list k at a time and return its modified list ...

  4. javascript+JQuery实现返回顶部功能

    很多网站上都有返回顶部的效果,本文阐述如何使用jquery实现返回顶部按钮. 首先需要在顶部添加如下html元素: <p id="back-to-top"><a ...

  5. android 地址控件概述

    最近,公司做项目,需要一个地址控件,本来是想androidcopy开源的android的地址控件,但是了,找来找去.都没有找到一个真正满足我的需求的,普通的地址控件只是精确到市县区三级,但是我们的需求 ...

  6. GridControl 获取某分组的第一个孩子

    int iGroupRowHandle = this.gridControlView.FocusedRowHandle; ) { int iChildCount = this.gridControl. ...

  7. 5. How to set up a Activity

    1. Create a new xml in "layout" folder "splah.xml" <?xml version="1.0&qu ...

  8. 一段遍历4X4表格,取出每个单元格内容组合成文本的JS代码

    遍历表格的JS容易忘,留个随笔以备忘. var tableData="";    var table=document.getElementById("XXTableId ...

  9. HTML5游戏,五子棋

    在线演示 本地下载 最近html5的游戏还真是不少,这种在线游戏既简单又有趣.收藏几个在午休时间娱乐一下.何乐而不为呢?喜欢研究的可以下载代码看看.超级推荐!

  10. iOS中重用UITableView单元格时,千万别忘了这个

    不多说,看截图