HDUOJ--------(1198)Farm Irrigation
Farm Irrigation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4491 Accepted Submission(s): 1947
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.
HF
/*龚细军 宽度优先搜索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的更多相关文章
- (DFS)hdoj1198-Farm Irrigation
题目链接 DFS的简单应用,比较繁琐的是处理输入的英文字母.用并查集也可以做(可是笔者现在还没有掌握并查集,之前只用过一次,以后学会回来补上) #include<cstdio> #incl ...
- hdu 1198 Farm Irrigation(深搜dfs || 并查集)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm ...
- HDU 1198 Farm Irrigation(并查集+位运算)
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
- HDU 1198 Farm Irrigation (并检查集合 和 dfs两种实现)
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 ...
- Farm Irrigation(非常有意思的并查集)
Farm Irrigation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tot ...
- hdu.1198.Farm Irrigation(dfs +放大建图)
Farm Irrigation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Web Fram 2 for IIS7.X(Microsoft Web Farm Framework)
Microsoft Web Farm Framework (WFF) 2.0 是微软开发的.基于IIS 7.x的小插件,能够帮助我们轻松实现Web网站的高性能.高可用性,用来在Web服务器群上提供和管 ...
- IIS负载均衡-Application Request Route详解第二篇:创建与配置Server Farm(转载)
IIS负载均衡-Application Request Route详解第二篇:创建与配置Server Farm 自从本系列发布之后,收到了很多的朋友的回复!非常感谢,同时很多朋友问到了一些问题,有些问 ...
随机推荐
- select case when if 的一些用法
概述:sql语句中的case语句与高级语言中的switch语句,是标准sql的语法,适用于一个条件判断有多种值的情况下分别执行不同的操作. 首先,让我们看一下CASE的语法.在一般的SELECT中,其 ...
- 用Spark查询HBase中的表数据
java代码如下: package db.query; import org.apache.commons.logging.Log; import org.apache.commons.logging ...
- go语言基础之copy的使用
1.copy的使用 示例: package main //必须有个main包 import "fmt" func main() { srcSlice := []int{1, 2} ...
- C#中图片切割,图片压缩,缩略图生成的代码
**//// <summary> /// 图片切割函数 /// </summary> /// <param name="sourceFile"> ...
- [Javascript] Funciton Expression
//This will load the code into the memory no matter //you call it or not function diffOfSquares(a,b) ...
- 8. 利用反射机制, ListArray,intent来实现多Activity的切换
package com.example.thenewboston; import android.app.ListActivity; import android.content.Intent; im ...
- Pinger
import java.io.IOException;import java.io.InputStreamReader;import java.io.LineNumberReader;import j ...
- MySQL数据源在Spring中的配置
干脆把MySQL的数据源配置也一起放这里,以备不时之需. MySQL的驱动包可以从这里 http://pan.baidu.com/s/1d02aZ 下载. 以下粗体部分是需要你根据实际情况调整的. & ...
- postgresql 内存分配
postgresql的内存分配主要由shared_buffers.temp_buffers.work_mem.maintenance_work_mem参数控制. shared_buffers又可以叫做 ...
- Appium Python 四:怎样获取APP的Package以及Activity
看到一篇很好的博客:[Android测试][随笔]获得App的包名和启动页Activity 除了博客上的方法,我还找到两种方法: 方法一:aapt 前提需要使用SDK Manager.exe 下载 A ...