Farm Irrigation HDU
来源:互联网 发布:sql数据迁移解决方案 编辑:程序博客网 时间:2024/06/01 21:13
题目:
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 1
Benny 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 2
Several 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
题目大意:
有上述11种水管摆放方式,现给你一块n*m的农田,问你需要多少个出水点能灌溉到所有地方。
思路:
并查集求联通快.
代码:
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<string>#include<vector>#include<stack>#include<bitset>#include<cstdlib>#include<cmath>#include<cctype>#include<sstream>#include<set>#include<list>#include<deque>#include<map>#include<queue>using namespace std;typedef long long ll;const double PI = acos(-1.0);const double eps = 1e-6;const int INF = 0x3f3f3f3f;const int maxn = 123456;int T,n,m;int f[3100];char maps[55][55];//A-K向四个方向是否连通 左上右下int dir[11][5]={{1,1,0,0},{0,1,1,0},{1,0,0,1},{0,0,1,1},{0,1,0,1},{1,0,1,0},{1,1,1,0},{1,1,0,1},{1,0,1,1},{0,1,1,1},{1,1,1,1}};int Get_f(int x){ if(f[x]==-1) return x; return Get_f(f[x]);}void Merge(int x,int y){ int t1=Get_f(x); int t2=Get_f(y); if(t1!=t2) f[t1]=t2;}int main(){ while(scanf("%d%d",&n,&m)!=EOF!=EOF) { if(n==-1&&m==-1) break; for(int i=0;i<n;i++) scanf("%s",&maps[i]); memset(f,-1,sizeof(f)); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(i>0&&dir[maps[i][j]-'A'][1]==1&&dir[maps[i-1][j]-'A'][3]==1)//上方可以连接 Merge(i*m+j,(i-1)*m+j); if(j>0&&dir[maps[i][j]-'A'][0]==1&&dir[maps[i][j-1]-'A'][2]==1)//左方 Merge(i*m+j,i*m+j-1); if(i<n-1&&dir[maps[i][j]-'A'][3]==1&&dir[maps[i+1][j]-'A'][1]==1)//下方 Merge(i*m+j,(i+1)*m+j); if(j<m-1&&dir[maps[i][j]-'A'][2]==1&&dir[maps[i][j+1]-'A'][0]==1)//右方 Merge(i*m+j,i*m+j+1); } } int cnt=0; for(int i=0;i<n*m;i++) if(f[i]==-1) cnt++; printf("%d\n",cnt); } return 0;}
- HDU 1198 Farm Irrigation
- hdu 1198 Farm Irrigation
- hdu 1198 Farm Irrigation
- HDU--1198Farm Irrigation
- hdu 1198 Farm Irrigation
- HDU 1198-Farm Irrigation
- HDU 1198 Farm Irrigation
- hdu 1198 Farm Irrigation
- hdu 1198 Farm Irrigation
- HDU 1198 Farm Irrigation
- HDU 1198 Farm Irrigation
- HDU 1198 Farm Irrigation
- hdu 1198 Farm Irrigation
- hdu 1198 Farm Irrigation
- hdu 1198 Farm Irrigation
- hdu 1198 Farm Irrigation
- hdu(1198)Farm Irrigation
- hdu 1198 Farm Irrigation
- Linux-视频监控系统(12)-移植到树莓派中
- OpenCV文档翻译--相机标定
- hdu1087最长递增子序列dp
- 论文总结——In Situ Visualization at Extreme Scale Challenges and Opportunities
- 【MVC】走向MVC模式
- Farm Irrigation HDU
- Android opencv 调试
- 自定义网络传输数据加密与解密
- 1128:检查一个数是否为质数
- Hibernate中,mappedBy和注解@JoinColumn的对比
- LeetCode1—Two Sum
- Restful API利器——RestPack项目教程
- play framwork2.X jpa(hibernate)配置
- Json传对象数组到后台并转化成Java对象数组