HDU 2612 Find a way
来源:互联网 发布:xplay6知乎 编辑:程序博客网 时间:2024/06/06 05:07
Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 16200 Accepted Submission(s): 5200
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
对这两个人进行广搜即可
#include<iostream>#include<cstring>#include<cstdio>#include<queue>using namespace std;int n,m;char map[250][250];int book[250][250];int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};int stepY[250][250];int stepM[250][250];struct Node{int x,y;};Node yff,mer;bool judge(int x,int y){if(x>=0&&y>=0&&x<n&&y<m&&map[x][y]!='#'&&!book[x][y]) return true;return false;}void bfsY(Node a){queue<Node> que;que.push(a);book[a.x][a.y]=1;while(!que.empty()){Node now,next;now=que.front();que.pop();for(int i=0;i<4;i++){next.x=now.x+dir[i][0];next.y=now.y+dir[i][1];if(judge(next.x,next.y)){book[next.x][next.y]=1;stepY[next.x][next.y]=stepY[now.x][now.y]+1;que.push(next);}}}return;}void bfsM(Node a){queue<Node> que;que.push(a);book[a.x][a.y]=1;while(!que.empty()){Node now,next;now=que.front();que.pop();for(int i=0;i<4;i++){next.x=now.x+dir[i][0];next.y=now.y+dir[i][1];if(judge(next.x,next.y)){book[next.x][next.y]=1;stepM[next.x][next.y]=stepM[now.x][now.y]+1;que.push(next);}}}return;}int main(){while(~scanf("%d%d",&n,&m)){int max=6666666;for(int i=0;i<n;i++) scanf("%s",map[i]);for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(map[i][j]=='Y'){yff.x=i;yff.y=j;}if(map[i][j]=='M'){mer.x=i;mer.y=j;}}}memset(stepY,0,sizeof(stepY));memset(book,0,sizeof(book));bfsY(yff);memset(stepM,0,sizeof(stepM));memset(book,0,sizeof(book));bfsM(mer);for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(map[i][j]=='@'&&stepY[i][j]!=0&&stepM[i][j]!=0){if(stepY[i][j]+stepM[i][j]<max){max=stepY[i][j]+stepM[i][j];}}}}cout<<max*11<<endl;}}
阅读全文
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
- logistics回归--梯度上升算法以及改进--用于二分类
- 《代码整洁之道》读书笔记
- if __name__ == '__main__'的作用与原理
- 深入理解java虚拟机学习笔记-4.java类加载机制
- hdu 6053 TrickGCD
- HDU 2612 Find a way
- C 非标准库(conio.h)
- bootstrap按钮和折叠插件
- 安卓开发debug-app卸载不干净,导致安装、升级不成功问题的解决办法
- k&R scanf 函数
- [LeetCode]121. Best Time to Buy and Sell Stock
- 动态稀疏矩阵对应的三元组的输出,以及逆置和赋值算法
- VS 安装助手,弹出“The security key for this program currently stored on your system does not appear to be ”
- 如何对迭代器做切片操作