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

思路:
A题的快感胜于一切不解释
题目不错,让我重新认识了bfs的功效。其实这个题思路倒是很容易,就是遍历下两个人到每个‘@’的距离,存起来然后看看哪个最短就行,关键的问题在于数据结构,即数据的存储和操作方式。
一开始我的想法是用map,即将每个点到行走人的距离映射到该点的坐标上,但是我卡在了不会求行走距离,之前那会儿总是想用dfs来求,做的题目还不够多= =后来上网看了题解,发现使用bfs以Y或M为初始点遍历图中所有点到出发点的距离,关键的公式就是

dis[dx][dy] = dis[temp.x][temp.y]+1;

当时第一次看这到这个公式的时候反应是DP(我真是好久没做DP了)

后来发现原来这就是bfs的精髓


#include <iostream>
#include <cstring>
#include <queue>
#define INF 0x7fffffff
using namespace std;int n,m;
char G[207][207];
int dir[4][2] = {{-1,0},{0,-1},{0,1},{1,0}};
struct pos{int x,y;
};
int dis_1[207][207];
int dis_2[207][207];bool judge(int x,int y)
{if(x>=1&& x<=n && y>=1 && y<=m && G[x][y]!='#')return true;else return false;
}void bfs(int a,int b,int vis[207][207],int dis[207][207])
{vis[a][b] = 1;queue<pos> q;pos s;s.x = a;s.y = b;q.push(s);while(!q.empty()){pos temp = q.front();q.pop();for(int i = 0;i <= 3;i++){int dx = temp.x+dir[i][0];int dy = temp.y+dir[i][1];if(!vis[dx][dy] && judge(dx,dy)) {vis[dx][dy] = 1;dis[dx][dy] = dis[temp.x][temp.y]+1;pos n;n.x = dx;n.y = dy;q.push(n);}}}
}int main()
{while(cin>>n>>m){int x1,y1,x2,y2;int vis_1[207][207];int vis_2[207][207];for(int i = 1;i <= n;i++)cin>>G[i]+1;for(int i = 1;i <= n;i++)for(int j = 1;j <= m;j++) {if(G[i][j] == 'Y'){x1 = i;y1 = j;}if(G[i][j] == 'M'){x2 = i;y2 = j;}}memset(dis_1,0,sizeof(dis_1));memset(dis_2,0,sizeof(dis_2));memset(vis_1,0,sizeof(vis_1));memset(vis_2,0,sizeof(vis_2));bfs(x1,y1,vis_1,dis_1); bfs(x2,y2,vis_2,dis_2);int ans = INF;for(int i = 1;i <= n;i++)for(int j = 1;j <= m;j++)if(G[i][j] == '@' && vis_1[i][j] && vis_2[i][j])ans = dis_1[i][j]+dis_2[i][j]<ans?dis_1[i][j]+dis_2[i][j]:ans;cout<<ans*11<<endl;}return 0;
}

转载于:https://www.cnblogs.com/immortal-worm/p/5133046.html

HDU-2612 Find a way相关推荐

  1. N - Find a way HDU - 2612

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

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

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

  3. HDU 2612 Find a way(BFS)

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

  4. [HDU] 2612 Find a way - 用单源最短论经模拟的简单广搜

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2612 方法:其实就是从两个点分别探寻单源最短路径,两个点到同一个目标位置的最短路径都求出来,相加,然 ...

  5. 【hdu1241Oil Deposits】【HDU 2612 Find a way】

    HDU1241Oil Deposits 求联通块数量 Sample Input 1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5  ****@ *@@*@ *@ ...

  6. HDU 2612 Find a way

    //2612 Find a way //题意:给一幅图,有墙,有KFC,有路.两个人要去KFC约会,有很多个KFC,问两个人去一间KFC总共走的最少步数 //广搜水题,居然被初始化卡了两个钟悲剧了.. ...

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

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

  8. hdu 2612(bfs)Find a way

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

  9. HDU 2612 Find a way bfs

    点击打开链接 Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  10. HDU 2612 (两边一起)

    /* Find a way Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave N ...

最新文章

  1. 根据镜像安装oracle插件,docker镜像alpine中安装oracle客户端
  2. sublime Text 3 安装emmet
  3. 数据全裸时代,你的隐私有多容易获取?
  4. (旧)子数涵数·C语言——条件语句
  5. 知行动手实验室可以用来做什么?
  6. ghost linux pe,winPE下安装系统ghost的详细教程
  7. 关于文章手把手教你使用stata做竞争风险模型的一些其他操作
  8. android仿饿了么,Android 仿饿了么下拉Dialog
  9. Linux常用命令分享
  10. 南京师范计算机科学研究生分数线,2020南京师范大学考研复试分数线已公布
  11. 苹果id可以彻底注销吗_如何注销苹果id账号
  12. java 痛并快乐着 day02(2021-11-09)
  13. 如何在Windows下使用DOS命令进入MySQL数据库?
  14. 理解PHP网页运行原理
  15. photoshop 重复上一次变换操作 ctrl+shift+alt+t
  16. 计算机网络冲刺串讲,计算机应用基础串讲冲刺讲义(二)
  17. 《Python中神奇的第三方库:Faker》
  18. CUDA 编程简介(上)
  19. Tableau服务器部署方案
  20. FCPX插件:时尚动感快闪图文展示开场片头 Dynamic Intro

热门文章

  1. GoWorld – 用Golang写一个分布式可扩展、可热更的游戏服务器
  2. ant design pro(一)安装、目录结构、项目加载启动【原始、以及idea开发】
  3. rman实验——测试备份压缩
  4. Centos7环境下etcd集群的搭建
  5. xz命令--Linux命令应用大词典729个命令解读
  6. 菜鸟学习笔记3——jQuery 选择器
  7. Open source robotics toolkits: use virtual arenas to test your robotics algorithms
  8. 关于引用与指针实现多态的一些记录
  9. 前端笔记之NodeJS(三)Expressejs模板引擎请求识别
  10. Laravel+nginx环境配置好后,url加参数提交报404错误