HDU-2612-Find a way
来源:互联网 发布:ubuntu 无法安装 编辑:程序博客网 时间:2024/06/03 19:39
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612
Find a way
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.
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
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 4Y.#@.....#..@..M4 4Y.#@.....#..@#.M5 5Y..@..#....#...@..M.#...#
Sample Output
668866
题目分析:Y和M两个人从各自的起点出发,约定在kfc见面,求二人到kfc的步数之和最小。经典bfs。
#include<iostream>#include<queue>#include<cstring>using namespace std;int n,m;int ans1[201][201],ans2[201][201];int vis1[201][201],vis2[201][201];int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};char mp[201][201];struct node{ int x,y;};bool check(node t){ if(t.x>=0&&t.x<n&&t.y>=0&&t.y<m&&mp[t.x][t.y]!='#') return 1; else return 0;}void bfs(int sx,int sy,int vis[201][201],int ans[201][201]){ queue<node>q; node now,next; now.x=sx;now.y=sy; vis[now.x][now.y]=1; ans[now.x][now.y]=0; q.push(now);while(!q.empty()) { now=q.front(); q.pop(); for(int i=0;i<4;i++) { next.x=now.x+dir[i][0]; next.y=now.y+dir[i][1]; if(check(next)&&!vis[next.x][next.y]) { vis[next.x][next.y]=1; ans[next.x][next.y]=ans[now.x][now.y]+1; q.push(next); } } } }int main(){ int x1,y1,x2,y2; while(cin>>n>>m) { if(n==0&&m==0) break; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { cin>>mp[i][j]; if(mp[i][j]=='Y') { x1=i; y1=j; } else if(mp[i][j]=='M') { x2=i; y2=j; }}}int min=10000;memset(vis1,0,sizeof(vis1));memset(ans1,0,sizeof(ans1));bfs(x1,y1,vis1,ans1);memset(vis2,0,sizeof(vis2));memset(ans2,0,sizeof(ans2));bfs(x2,y2,vis2,ans2);for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(mp[i][j]=='@'&&vis1[i][j]&&vis2[i][j])//这个点必须被同时访问到。 {if(ans1[i][j]+ans2[i][j]<min){min=ans1[i][j]+ans2[i][j];}}}}cout<<min*11<<endl;} return 0;}
阅读全文
0 0
- HDU 2612 Find a way
- HDU-2612-Find a way
- hdu 2612 Find a way
- HDU 2612 Find a way
- hdu 2612 Find a way
- HDU 2612 Find a way
- HDU 2612 Find A Way
- hdu 2612 Find a way
- HDU-2612-Find a way
- HDU 2612 Find a way
- hdu 2612 Find a way
- HDU 2612 Find a way
- HDU-2612 Find a way
- HDU 2612 Find a way
- hdu 2612 Find a way
- HDU 2612 find a way
- HDU 2612 Find a way
- hdu 2612 Find a way
- poj2495 Aggressive cows(贪心+二分)
- 中缀到后缀表达式的转换:java-stack实现
- React Native之如何优化组件来达到减少代码冗余
- Python学习11_图像自动阈值分割
- std::function
- HDU-2612-Find a way
- Word2Vec + Jieba 分词实现视频标签距离计算
- JDK安装多个版本出现Java -version和你配置的环境变量的jdk版本不一致的问题
- 过滤器
- webview与js交互,window.close()回调方法onCloseWindow()无效的解决方法
- jquery 获取div下的第一个子元素方法总结
- Java中的多线程
- 实习总结二
- UE4中窗口模式切换