点击打开链接

Find a way

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 21857    Accepted Submission(s): 7120

Problem Description
Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki.
Yifenfei’s home is at the countryside, but Merceki’s home is in the center of city. So yifenfei made arrangements with Merceki to meet at a KFC. There are many KFC in Ningbo, they want to choose one that let the total time to it be most smallest.
Now give you a Ningbo map, Both yifenfei and Merceki can move up, down ,left, right to the adjacent road by cost 11 minutes.
Input
The input contains multiple test cases.
Each test case include, first two integers n, m. (2<=n,m<=200).
Next n lines, each line included m character.
‘Y’ express yifenfei initial position.
‘M’    express Merceki initial position.
‘#’ forbid road;
‘.’ Road.
‘@’ KCF
Output
For each test case output the minimum total time that both yifenfei and Merceki to arrival one of KFC.You may sure there is always have a KFC that can let them meet.
Sample Input
4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#
Sample Output
66
88
66
Author
yifenfei
Source
奋斗的年代
Recommend
yifenfei   |   We have carefully selected several similar problems for you:  2717 1254 1728 2102 1072

题意:

给一张地图,Y代表第一个人所在的位置,M代表第二个人所在的位置,.代表路,#代表墙,@代表KFC,
两个人想在同一个kfc见面,问他们见面花费最小的时间;

分析:

遍历地图,对于每一KFC,分别求出Y到这个KFC的距离dy,M到这个KFC的距离dm,
如果距离为dy=-1或者dm=-1,则说明无法到达;

否则,跟新最短距离:ans=min(ans,dy+dm);

这个思路比较简单,但是当KFC的数量非常多的时候,就会超时

根据上面的思路,我们可以优化一下,减少重复计算,设置一个距离数组dis[N][N][2];初始化为INF

dis[i][j][0]代表Y到坐标为(i,j)的KFC的距离,dis[i][j][1]代表M到坐标为(i,j)的KFC的距离

然后,BFS求出Y到地图上所有点的最短距离,若这个点是KFC,则更新dis[i][j][0],

同理,BFS求出M到地图上所有点的最短距离,若这个点是KFC,则更新dis[i][j][1],

最后遍历地图,如果这个点是KFC,且当前最短距离 > Y到这个KFC的距离+M到这个KFC的距离,

则更新最短距离,最后不要忘记*11哦

超时代码

#include<iostream>
#include<cstring>
#include<queue>
#include<cmath>
#include<cstdio>
using namespace std;
#define MAXV 209
char mp[MAXV][MAXV];//地图
bool vis[MAXV][MAXV];//判断点是否在队列
int n,m;
struct point
{int x,y,step;
};
point Y,M;//两人的位置
vector<point> KFC;//储存KFC的位置
int dic[4][2]={0,1,0,-1,1,0,-1,0};
bool check(int x,int y)//若这一点可以走,返回0
{if(x<0||x>=n||y<0||y>=m||vis[x][y]||mp[x][y]=='#')return 1;return 0;
}
int bfs(point s,point e)//求s点到e点的最短距离,无法到达距离为-1
{memset(vis,0,sizeof(vis));queue<point>q;q.push(s);//起点入队vis[s.x][s.y]=1;while(!q.empty()){point now=q.front();q.pop();if(now.x==e.x&&now.y==e.y)//已经到达终点return now.step;for(int i=0;i<4;i++)//上下左右,四个方向搜索{int x=now.x+dic[i][0];int y=now.y+dic[i][1];if(check(x,y))//不合法continue;point next;next.x=x;next.y=y;next.step=now.step+1;vis[x][y]=1;q.push(next);}}return -1;//无法到达
}
int main()
{while(scanf("%d%d",&n,&m)!=EOF){KFC.clear();for(int i=0;i<n;i++){scanf("%s",mp[i]);for(int j=0;j<m;j++){if(mp[i][j]=='Y'){Y.x=i;Y.y=j;Y.step=0;}if(mp[i][j]=='M'){M.x=i;M.y=j;M.step=0;}if(mp[i][j]=='@'){point temp;temp.x=i;temp.y=j;temp.step=0;KFC.push_back(temp);}}}int ans=1<<20;for(int i=0;i<KFC.size();i++)//遍历所有KFC{int dy=bfs(Y,KFC[i]),dm=bfs(M,KFC[i]);if(dy==-1||dm==-1)continue;ans=min(ans,dy+dm);}cout<<ans*11<<endl;}return 0;
}
AC代码
#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 210;
const int inf = 100000000;
int n, m, flag;//flag=0代表Y,flag=1代表M
int dis[N][N][2];
//dis[i][j][0]代表Y到坐标为(i,j)的KFC的距离
//dis[i][j][1]代表M到坐标为(i,j)的KFC的距离
int mark[N][N];//标记点是否在队列中
int dir[4][2] = {{1, 0}, {0, -1}, {0, 1}, {-1, 0}};//搜索方向
char s[N][N];//地图
struct node
{int x, y, step;
};
void bfs(int x, int y)
{queue<node>q;node temp, type;temp.x = x;temp.y = y;temp.step = 0;q.push(temp);//起点入队mark[x][y] = 1;//标记while(!q.empty()){temp = q.front();q.pop();//出队type.step = temp.step + 1;//步数加一for(int i = 0; i < 4; i++){type.x = x = temp.x + dir[i][0];type.y = y = temp.y + dir[i][1];if(x >= 0 && x < n && y >= 0 && y < m && mark[x][y] == 0 && s[x][y]!='#')//可以走{mark[x][y] = 1;if(s[x][y] == '@')dis[x][y][flag] = type.step;//距离q.push(type);}}}
}
int main()
{while(scanf("%d%d", &n, &m)!=EOF){int min = inf;for(int i = 0; i < n; i++)for(int j = 0; j < m; j++)dis[i][j][0] = dis[i][j][1] = inf;//初始化距离为INFfor(int i = 0; i < n; i++)scanf("%s", s[i]);for(int i = 0; i < n; i++)for(int j = 0; j < m; j++){if(s[i][j] == 'Y'){flag = 0;memset(mark, 0, sizeof(mark));bfs(i, j);}if(s[i][j] == 'M'){flag = 1;memset(mark, 0, sizeof(mark));bfs(i, j);}}for(int i = 0; i < n; i++)for(int j = 0; j < m; j++)if(s[i][j] == '@' && min > dis[i][j][0] + dis[i][j][1])//松驰操作{min = dis[i][j][0] + dis[i][j][1];//更新最短距离}printf("%d\n", min*11);}
}

HDU 2612 Find a way bfs相关推荐

  1. 【HDU 2612 Find a Way(BFS)】(兼BFS入门笔记)

    [HDU 2612 Find a Way(BFS)](兼BFS入门笔记) 原题入口: http://acm.hdu.edu.cn/showproblem.php?pid=2612 第一篇在CSDN的博 ...

  2. N - Find a way HDU - 2612

    N - Find a way HDU - 2612 首先的想法是找到他们俩到每个 KFC 的最小距离,然后让和最小即可 那么怎么求他们俩到 KFC 的最小距离呢,可以从 KFC 出发去找他俩,也可以从 ...

  3. 专题训练二 搜索进阶 HDU - 3085 Nightmare Ⅱ (双向BFS + 曼哈顿距离)

    HDU - 3085 Nightmare Ⅱ (双向BFS + 曼哈顿距离) Problem Description Last night, little erriyue had a horrible ...

  4. HDU 2612 Find a way(BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 题目大意:给你一张n*m的图,图上有两个点Y.M,和若干个点@,找出某个点@使Y.M到这里的距离 ...

  5. HDU - 2612 Find a way(BFS搜索)

    题目: 链接 思路: 用BFS分别以'Y'和'M'的位置为起点进行两次搜索,并把这两次的搜索结果在一个二维数组中保存下来,在对地图遍历遇到'@'更行最小值. PS: 如果用'Y'和'M'点分别去搜每个 ...

  6. hdu 2612(bfs)Find a way

    题意:就是Y和M在@相遇的最短的时间. 思路:基本的广搜题,先Y搜一次,然后M搜一次,最后求出Y和M在@相遇的最短的时间. 代码实现: #include<iostream> #includ ...

  7. 【HDU - 2612】Find a way(bfs)

    -->Find a way 直接上Chinese  Descriptions: hsj和lsh最近迷上了pokemon go的游戏.在双十一大物期中考试来临之前,他们想抓一只稀有土拨鼠来攒攒人品 ...

  8. hdu 2612 FindAWay 两点BFS

    参考大佬的代码,简洁明了 https://www.cnblogs.com/sky-stars/p/11140928.html 两次bfs,分别求出Y.M在这张地图所有地方的所用时间,再求出在" ...

  9. HDU 5836 Rubik's Cube BFS

    Rubik's Cube 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5836 Description As we all know, Zhu is ...

最新文章

  1. 一个技术总监的忠告:精通那么多技术,你为何还是受不到重用?
  2. 一些思考,没想好标题
  3. 解决:VS中进行Qt开发,编译时报错:打不开QWidgets.h等文件的问题
  4. oracle占用内存 100,System表空间占用率100%,管理Oracle系统审计
  5. 开课吧Java面试题:虚引用与软引用和弱引用的区别
  6. IEEE754标准浮点格式
  7. win10切换桌面_探秘Win10系统中的不可错过的实用功能
  8. 控制层SpringMVC和Struts2的区别
  9. 运筹学基础及其matlab,【官方直发】 运筹学基础及其MATLAB应用
  10. 应用统计学学什么科目_应用统计学考研科目及总数
  11. 从表征到行动---意向性的自然主义进路(续三)
  12. 政务区块链平台设计思路
  13. 空间金字塔池化(Spatial Pyramid Pooling)
  14. 深入理解Plasma(四)Plasma Cash
  15. 年度盘点:2018云栖社区15大影响力技术团队(附100+干货博文)
  16. 10-STM32F1-RTC and BKP
  17. 深圳软件测试几月份好找工作,上海与深圳的软件测试发展,未来哪个更有发展前景?...
  18. Coursera--DataStructure-加州理工大学圣地亚哥分校课程
  19. 星起航:跨境电商行业卖家可利用新技术打造成熟供应链
  20. Kvaser Leaf light HS v2 | 如何使用Excel发送和接收CAN报文数据

热门文章

  1. 线段树专辑——hdu 1698 Just a Hook
  2. 各维度 特征 重要程度 随机森林_机器学习算法——随机森林
  3. 输变电设备物联网节点设备无线组网协议_U-Link 物联网(工业互联网)服务平台
  4. 在线考试计算机文化基础,计算机文化基础在线考试.pdf
  5. c语言错误指导,c语言编程指导.pdf
  6. Zencart的首页php 301,Zencart 做了301重定向后不能登陆网站后台的解决方案
  7. 智能机器人比巴和智伴哪个好_扫地机器人和吸尘器哪个好?
  8. python读取所有txt文件_python如何批量读取txt文件
  9. python 随机获取列表的元素_练习 34 - 获取列表元素 - Learn Python 3 The Hard Way
  10. java mesos kubernete_Fabric8操作Kubernetes(一)