杭电OJ——1198 Farm Irrigation (搜索)
来源:互联网 发布:知世与艾利欧结婚甜文 编辑:程序博客网 时间:2024/06/17 05:46
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1198
题目:
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.
![](http://acm.hdu.edu.cn/data/images/1121-1.gif)
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
![](http://acm.hdu.edu.cn/data/images/1121-2.gif)
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.
![](http://acm.hdu.edu.cn/data/images/1121-1.gif)
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
![](http://acm.hdu.edu.cn/data/images/1121-2.gif)
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 2DKHF3 3ADCFJKIHE-1 -1
Sample Output
23
题目描述:
有图中几种形状的水管,给出一个n*m的矩阵,问最少需要多少水源点可以使整个图有水流通。题目乍一看有点难啊,不要自己把自己吓住了,仔细想想就发现其实可以写。写过这题后发现网上有人使用并查集来写的,哇,确实是啊,我怎么没想到呢?(个人认为搜索就是暴力,qwq)有兴趣可以看看并查集解法。
解题代码:
#include <stdio.h>#include <string.h>int a,b;char d[110][110];//存储地图 int bo[110][110];//标记地图 void dfs(int x,int y){ int tx,ty; if(bo[x][y]) //如果这个点使用过则返回 return ; bo[x][y] = 1;//把这个点标记,表示这个点已经被使用过 if(d[x][y] == 'C' || d[x][y] == 'D' || d[x][y] == 'E' //如果水管口方向朝下 ,向下搜索 || d[x][y] == 'H' || d[x][y] == 'I' || d[x][y] == 'J' || d[x][y] == 'K'){tx = x + 1,ty = y; if(tx >= 0 && tx < a && ty >= 0 && ty < b){//判断数组下标是否越界 if(d[tx][ty] == 'A' || d[tx][ty] == 'B' || d[tx][ty] == 'E' //水管口方向朝上|| d[tx][ty] == 'H' || d[tx][ty] == 'G' || d[tx][ty] == 'J' || d[tx][ty] == 'K'){dfs(x + 1,y);//需要找水管口方向相反的进行接口 }}}if(d[x][y] == 'C' || d[x][y] == 'A' || d[x][y] == 'F' //如果水管口方向朝左,向左搜索 || d[x][y] == 'H' || d[x][y] == 'I' || d[x][y] == 'G' || d[x][y] == 'K'){tx = x,ty = y - 1;if(tx >= 0 && tx < a && ty >= 0 && ty < b){//判断数组下标是否越界 if(d[tx][ty] == 'B' || d[tx][ty] == 'D' || d[tx][ty] == 'F' //水管口方向朝右 || d[tx][ty] == 'G' || d[tx][ty] == 'I' || d[tx][ty] == 'J' || d[tx][ty] == 'K'){ dfs(x,y - 1);//需要找水管口方向相反的进行接口}}}if(d[x][y] == 'A' || d[x][y] == 'B' || d[x][y] == 'E' //如果水管口方向朝上 ,向上搜索 || d[x][y] == 'H' || d[x][y] == 'G' || d[x][y] == 'J' || d[x][y] == 'K'){tx = x - 1,ty = y;if(tx >= 0 && tx < a && ty >= 0 && ty < b){//判断数组下标是否越界 if(d[tx][ty] == 'C' || d[tx][ty] == 'D' || d[tx][ty] == 'E' //水管口方向朝下 || d[tx][ty] == 'H' || d[tx][ty] == 'I' || d[tx][ty] == 'J' || d[tx][ty] == 'K'){ dfs(x - 1,y);//需要找水管口方向相反的进行接口}}}if(d[x][y] == 'B' || d[x][y] == 'D' || d[x][y] == 'F' //如果水管口方向朝右 ,向右搜索 || d[x][y] == 'G' || d[x][y] == 'I' || d[x][y] == 'J' || d[x][y] == 'K'){tx = x,ty = y + 1;if(tx >= 0 && tx < a && ty >= 0 && ty < b){//判断数组下标是否越界 if(d[tx][ty] == 'C' || d[tx][ty] == 'A' || d[tx][ty] == 'F' //水管口方向朝左 || d[tx][ty] == 'H' || d[tx][ty] == 'I' || d[tx][ty] == 'G' || d[tx][ty] == 'K'){ dfs(x,y + 1);//需要找水管口方向相反的进行接口}}}return ;}int main(){ int i,j,t,q; while(scanf("%d%d",&a,&b), a >= 0 && b >= 0){ memset(d,'\0',sizeof(d)); memset(bo,0,sizeof(bo)); for(i = 0 ;i < a;i ++) scanf("%s",d[i]); for(t = i = 0 ;i < a;i ++)//遍历整个地图 for(j = 0;j < b;j ++) if(bo[i][j] == 0){//如果这个点没有被使用则进入搜索 t ++;//每进入一次则水源点加一 ,因为每次搜索都会把这个点的所有接口全部找出来,并把相应的bo数组置为1 dfs(i,j);//进入深搜 } printf("%d\n",t); } return 0;}
阅读全文
0 0
- 杭电OJ——1198 Farm Irrigation (搜索)
- 杭电OJ——1198 Farm Irrigation (并查集)
- 杭电1198 Farm Irrigation(并查集)
- HDU 1198 Farm Irrigation(搜索)
- HDU 1198 - Farm Irrigation(搜索)
- 杭电1198--Farm Irrigation(BFS外…
- HDU 1198——Farm Irrigation
- hdu 1198 Farm Irrigation(搜索+并查集)
- hdu 1198 Farm Irrigation (搜索或并查集)
- HDU1213——Farm Irrigation(并查集)
- zoj2412 Farm Irrigation 搜索dfs
- Farm Irrigation 【三维数组搜索】
- HDU 1198 Farm Irrigation(深搜)
- HDU 1198 Farm Irrigation(DFS)
- HDU 1198 Farm Irrigation(dfs)
- HDU 1198 Farm Irrigation(DFS)
- HDU 1198 Farm Irrigation (BFS)
- HDU 1198 Farm Irrigation
- Anaconda 安装、配置、基本使用
- ehcache的使用
- vue 事件绑定 处理-
- celery初级教程(六)
- 重新认识递归
- 杭电OJ——1198 Farm Irrigation (搜索)
- 无法ping通阿里云ECS服务器公网地址解决
- 浅谈Maven中的POM.XML
- BZOJ 3262: 陌上花开(CDQ分治)
- My-第一个程序
- 2018北邮计算机、网研、网安803考研报考建议
- 使用Sqoop导入导出关系型数据库数据
- JAVA多线程和并发基础面试题
- hdu 3062 Party(2-sat)