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
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 like
Figure 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
Zhejiang University Local Contest 2005
Recommend
Ignatius.L
代码:
1 /*龚细军 宽度优先搜索bfs*/
2 #include<iostream>
3 #include<queue>
4 #include<cstdio>
5 #include<cstring>
6 using namespace std;
7 typedef struct G
8 {
9 /*true 代表此处有piper*/
10 bool up,down,left,right;
11 }gong;
12 struct point
13 {
14 int x;
15 int y;
16 }start;
17 //依次代表A~K的管道特性;
18 gong go[]={1,0,1,0,1,0,0,1,0,1,1,0,0,1,0,1,1,1,0,0,0,0,1,1,1,0,1,1,1,1,1,0,0,1,1,1,1,1,0,1,1,1,1,1};
19
20 /*前行的步奏*/
21 char map[51][51];
22 int n,ans,m;
23 void input()
24 {
25 memset(map,' ',sizeof(map));
26 for(int i=0;i<m;i++)
27 scanf("%s",map[i]);
28 }
29 void dfs()
30 {
31 /*判断是否联通,这样便于构造重位*/
32 bool isrr,isll,isuu,isdd;
33 queue<point> q;
34 point p1,p2;
35 q.push(start);
36 bool isbian=false;
37 while(!q.empty())
38 {
39 isrr=isll=isuu=isdd=false;
40 p1=q.front();
41 q.pop();
42 /*向下搜索*/
43 if(p1.x+1<m&&map[p1.x+1][p1.y]>='A'&&go[map[p1.x][p1.y]-'A'].down!=0&&go[map[p1.x+1][p1.y]-'A'].up!=0)
44 {
45 p2.x=p1.x+1;
46 p2.y=p1.y;
47 q.push(p2);
48 isdd=true; //说明经过这里;
49 }
50 /*向上搜索*/
51 if(p1.x>=1&&map[p1.x-1][p1.y]>='A'&&go[map[p1.x][p1.y]-'A'].up!=0&&go[map[p1.x-1][p1.y]-'A'].down!=0)
52 {
53 p2.x=p1.x-1;
54 p2.y=p1.y;
55 q.push(p2);
56 isuu=true;
57 }
58 /*向左搜索*/
59 if(p1.y>=1&&map[p1.x][p1.y-1]>='A'&&go[map[p1.x][p1.y]-'A'].left!=0&&go[map[p1.x][p1.y-1]-'A'].right!=0)
60 {
61 p2.x=p1.x;
62 p2.y=p1.y-1;
63 q.push(p2);
64 isll=true;
65 }
66 /*向右搜索*/
67 if(p1.y+1<n&&map[p1.x][p1.y+1]>='A'&&go[map[p1.x][p1.y]-'A'].right!=0&&go[map[p1.x][p1.y+1]-'A'].left!=0)
68 {
69 p2.x=p1.x;
70 p2.y=p1.y+1;
71 q.push(p2);
72 isrr=true;
73 }
74 if(isuu||isll||isrr||isdd)
75 {
76 map[p1.x][p1.y]='0';
77 }
78 else
79 if((p1.x>=1&&map[p1.x-1][p1.y]=='0')||(p1.x+1<m&&map[p1.x+1][p1.y]=='0')
80 ||(p1.y>=1&&map[p1.x][p1.y-1]=='0')||(p1.y+1<n&&map[p1.x][p1.y+1]=='0'))
81 {
82 if(map[p1.x][p1.y]!='0')
83 {
84 isbian=true;
85 map[p1.x][p1.y]='1';
86 }
87 }
88
89 else ans++;
90 }
91 if(isbian) ans++;
92 }
93 int main()
94 {
95 int i,j;
96
97 /* for(i=0;i<11;i++)
98 {
99 printf("%d %d %d %dn",go[i].up,go[i].down,go[i].left,go[i].right);
100 }*/
101
102 while(scanf("%d%d",&m,&n),(m!=-1||n!=-1))
103 {
104 ans=0;
105 input( );
106 for(i=0;i<m;i++)
107 {
108 for(j=0;j<n;j++)
109 {
110 if(map[i][j]>='A')
111 {
112 start.x=i;
113 start.y=j;
114 dfs();
115 }
116 }
117 }
118 printf("%dn",ans);
119 }
120 return 0;
121 }
- Windows Runtime(WinRT) 揭秘
- WeApp-Workflow: 基于Gulp 的微信小程序前端开发工作流
- 如何安装Windows Phone SDK 7.1 Release Candidate (RC)
- 微信与支付宝回应央行新规:认同央行规范!
- Gulp 工作流中Sass 增量编译功能的探索
- Sass与Compass——回顾
- 苹果就“降速门”致歉;央行批扫码支付不正当竞争;王健林旗下公司遭集体裁员
- 姚期智教授:量子计算是千亿万亿级别的产业,或成为科技创新的引擎
- Powershell中禁止执行脚本解决办法
- 使用AsyncTask异步更新UI界面及原理分析
- 商家为何要做小程序?
- Android中关于dip和px以及转换的总结
- Python介绍
- python案例-用户登录
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- ubuntu中python调用C/C++办法之动态链接库详解
- linux中使用boost.python调用c++动态库的办法
- 在Linux系统下上传项目到码云的办法
- PHP global全局变量经典应用与注意事项分析【附$GLOBALS用法对比】 原创
- Linux(Ubuntu 18.04)上安装Anaconda步骤详解
- php web环境和命令行环境下查找php.ini的位置
- PHP大文件分块上传功能实例详解
- Linux 命令行通配符及转义符的实现
- Python爬虫抓取指定网页图片代码实例
- PHP变量作用域(全局变量&局部变量)&global&static关键字用法实例分析
- CentOS 7 安装Chrome浏览器的方法
- PHP高级编程之消息队列原理与实现方法详解
- thinkphp5.1框架模板布局与模板继承用法分析
- Linux内核设备驱动之内存管理笔记整理
- Matplotlib 绘制饼图解决文字重叠的方法