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 ;
}
 

最新文章

  1. Sqlite官方下载对应版本注意细节
  2. JSP 内置对象的四种属性范围
  3. Struts2 模型驱动及页面回显
  4. 一个tabBarController管理多个Storyboard
  5. linux下TUN/TAP虚拟网卡的使用
  6. UITabBarItem&#39;s appearance
  7. Linux下的有用命令
  8. springMVC系列之(四) spring+springMVC+hibernate 三大框架整合
  9. 在oracle表中增加字段,并调整字段的顺序
  10. assert()函数总结 (转)
  11. learning makefile 模式规则
  12. C#中,使用显式类型转换(int)和Math.Round方法,将浮点数转换为整数的区别
  13. 手动升级11.2.0.1的rac数据库到11.2.0.4
  14. Codeforces 670F - Restore a Number - [字符串]
  15. Webscoket
  16. 无法执行该操作,因为链接服务器 &quot;xxxxx&quot; 的 OLE DB 访问接口 &quot;SQLNCLI&quot; 无法启动分布式事务
  17. Centos7使用yum安装Mysql5.7.19的详细步骤(可用)
  18. UBIFS学习笔记
  19. java并发编程与高并发解决方案
  20. 超详细的Java面试题总结(三)之Java集合篇常见问题

热门文章

  1. 启明星会议室预定系统Outlook版开始支持Exchange2013与Office365版
  2. 第一个OC的类
  3. Objective-C:MRC(引用计数器)在OC内部的可变对象是适用的,不可变对象是不适用的(例如 NSString、NSArray等)
  4. 我所遭遇过的中间件--3D MAX SDK
  5. 交叉编译git
  6. python 两个队列进行对比
  7. Java Object Clone
  8. jmeter-The JVM should have exitted but did not
  9. 修改linux的文件时,如何快速找到要修改的内容并修改
  10. eclipse在search的时候,通过search打开的页面会覆盖之前打开的页面