杭电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.


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


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 平衡车系统乱了怎么办 监控老是滴滴的响怎么办 磁盘已满 文件未保存怎么办 cocos只有代码没有项目怎么办 电脑系统管理员密码忘记了怎么办 魔兽小队不显示职业颜色怎么办 魔兽多余的橙装怎么办? f117-f6不读硬盘怎么办 中飞院飞行学生停飞了怎么办 军人对你敬礼时怎么办 小孩抱着就睡放下就醒怎么办 着火了怎么办 我的世界 生存战争2感冒了怎么办 生存战争2吐了怎么办 我的世界hqm重置怎么办 不小心打了110怎么办 我的世界皮肤有黑影怎么办 我的世界字体变大了怎么办 生锈的铁钉扎了怎么办 每天晚上窗纱上老有蝙蝠倒挂怎么办 我的世界没有痒怎么办 七日杀被ban了怎么办 吕框箱子上保护摸撕不掉怎么办 我的世界开光影卡怎么办 我的世界买不了怎么办 我的世界延迟高怎么办 我的世界过于昂贵怎么办 白色麻布染上别的颜色怎么办 印度老山檀香开裂了怎么办 专升本没过线怎么办 西安公租房小孩上学怎么办 全民k歌直播没人怎么办 在全民直播没人看怎么办 皮肤又黄又粗怎么办 被强制消费后应怎么办? 当保安不发工资怎么办? 辅警改革流管员怎么办 退伍证上照片毁了怎么办 辅警年龄大了怎么办 交警2小时不出警怎么办 中暑发烧39度了怎么办