博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU-2612 Find a way
阅读量:6068 次
发布时间:2019-06-20

本文共 2973 字,大约阅读时间需要 9 分钟。

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 
#include
#include
#define INF 0x7fffffffusing 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
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]

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

你可能感兴趣的文章
在Delphi中隐藏程序进程
查看>>
AngularJS PhoneCat代码分析
查看>>
MEF元数据应用说明
查看>>
maven错误解决:编码GBK的不可映射字符
查看>>
2016/4/19 反射
查看>>
SharePoint Wiki发布页面的“保存冲突”
查看>>
oracle 10g 数据库与客户端冲突导致实例创建无监听问题
查看>>
Delphi中读取文本文件的方法(实例一)
查看>>
Linux常用命令
查看>>
Android开源代码解读の使用TelephonyManager获取移动网络信息
查看>>
想说一点东西。。。。
查看>>
css知多少(8)——float上篇
查看>>
NLB网路负载均衡管理器详解
查看>>
水平添加滚动条
查看>>
PHP中”单例模式“实例讲解
查看>>
VS2008查看dll导出函数
查看>>
VM EBS R12迁移,启动APTier . AutoConfig错误
查看>>
atitit.细节决定成败的适合情形与缺点
查看>>
iOS - Library 库
查看>>
MATLAB 读取DICOM格式文件
查看>>