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 }
- python数据分析师面试题选
- Spring+SpringMVC+MyBatis+easyUI整合优化篇(九)数据层优化-jdbc连接池简述、druid简介
- webpack配置别名alias出现的错误匹配
- 在Java程序中处理数据库超时与死锁
- 如何用TensorFlow和TF-Slim实现图像标注、分类与分割
- Spring+SpringMVC+MyBatis+easyUI整合优化篇(六)easyUI与富文本编辑器UEditor整合
- 使用shell脚本查看数据库负载情况(第二篇)(r3笔记第92天)
- tensorflow LSTM + CTC实现端到端OCR
- Spring+SpringMVC+MyBatis+easyUI整合优化篇(七)图片上传功能
- 黑客比程序员牛在哪?
- oracle工具集初探(r4笔记第8天)
- Spring+SpringMVC+MyBatis+easyUI整合优化篇(二)Log4j讲解与整合
- 京东JData算法大赛-高潜用户购买意向预测(github源码)
- 巧用linux命令做图片下载器(r4笔记第7天)
- 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 数组属性和方法
- Python中好用又高效的Collections 模块
- 正确创建Python二维数组
- 深入理解Python内存管理与垃圾回收,再也不怕问了(一)
- 走进面向“对象”编程的理想国(一)——深入理解Python中的一切皆对象
- 深入理解Python内存管理与垃圾回收,再也不怕问了(二)
- 如何利用Python实现二分查找(迭代和递归)
- 详解排序算法(Python实现)
- Python垃圾回收机制
- iOS 开发:『Crash 防护系统』(二)KVO 防护
- 学Java到底学什么
- Python 的魔法方法及用途
- 四件简单的事情,帮助改善部署过程
- 为什么 Python 程序中很少看到驼峰式的命名方式?
- GATK的FilterMutectCalls如何才能成功呢
- Python字典按键/值排序的几种方法