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. java程序的运行方式

    1.JAR File JAR 文件用于压缩和发布,而且还用于部署和封装库.组件和插件程序,并可被像编译器和 JVM 这样的工具直接使用 有点类似于net中的dll 2.Runnable JAR Fil ...

  2. hue解决timed out(code THRIFTSOCKET):None

    报错栈: Traceback (most recent call last): File , in decorator return func(*args, **kwargs) File , in e ...

  3. 数学图形(1.2)Sin曲线

    相关软件参见:数学图形可视化工具,使用自己定义语法的脚本代码生成数学图形.该软件免费开源.QQ交流群: 367752815 Sin曲线 vertices = x = *PI) to (*PI) y = ...

  4. linux C 多线程/线程池编程 同步实例

    在多线程.线程池编程中经常会遇到同步的问题. 1.创建线程 函数原型:int pthread_create(pthread_t *thread, const pthread_attr_t *attr, ...

  5. Android实现图片轮显效果——自定义ViewPager控件

    一.问题概述 使用ViewPager控件实现可横向翻页.水平切换图片等效果,但ViewPager需要手动滑动才能切换页面,图片轮显效果的效果本质上就是在ViewPager控件的基础上让它能自动的进行切 ...

  6. Triangular numbers

    http://codeforces.com/problemset/problem/47/A Triangular numbers time limit per test 2 seconds memor ...

  7. (算法)Trapping Rain Water I

    题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...

  8. Linux动态库(.so)搜索路径

    主要内容: 1.Linux动态库.so搜索路径 编译目标代码时指定的动态库搜索路径: 环境变量LD_LIBRARY_PATH指定的动态库搜索路径: 配置文件/etc/ld.so.conf中指定的动态库 ...

  9. c语言訪问excel

    直接通过格式化读取文件就可实现,见附件

  10. Android 四大组件之 Service(一)

    Service是Android中四大组件之一,在Android开发中起到非常重要的作用,它运行在后台,不与用户进行交互. 1.Service的继承关系: java.lang.Object → andr ...