[HDU](2612)Find a way ---bfs
来源:互联网 发布:linux man手册显示中文 编辑:程序博客网 时间:2024/06/03 15:41
Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 17354 Accepted Submission(s): 5582
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
Author
yifenfei
AC代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;int vis[1505][1505];char mmap[1505][1505];int dir[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};typedef struct Node{ int x; int y; int step;}node;node now,nex,startM,startY;node kk[1505];int StepY[1505][1505];int StepM[1505][1505];int n,m,k;void bfsY(){ memset(vis,0,sizeof(vis)); queue<node>qy; vis[startY.x][startY.y] = 1; qy.push(startY); while(!qy.empty()) { now = qy.front(); qy.pop(); if(mmap[now.x][now.y]=='@') { StepY[now.x][now.y] = now.step; kk[k].x = now.x; kk[k].y = now.y; k++; continue; } for(int i=0;i<4;i++) { nex.x = now.x+dir[i][0]; nex.y = now.y+dir[i][1]; if(nex.x>=0 && nex.x<n && nex.y>=0 && nex.y<m && mmap[nex.x][nex.y]!='#'&&!vis[nex.x][nex.y]) { vis[nex.x][nex.y] = 1; nex.step = now.step+1; qy.push(nex); } } }}void bfsM(){ memset(vis,0,sizeof(vis)); queue<node>qm; vis[startM.x][startM.y] = 1; qm.push(startM); while(!qm.empty()) { now = qm.front(); qm.pop(); if(mmap[now.x][now.y]=='@') { StepM[now.x][now.y] = now.step; } for(int i=0;i<4;i++) { nex.x = now.x+dir[i][0]; nex.y = now.y+dir[i][1]; if(nex.x>=0 && nex.x<n && nex.y>=0 && nex.y<m && mmap[nex.x][nex.y]!='#'&&!vis[nex.x][nex.y]) { vis[nex.x][nex.y] = 1; nex.step = now.step+1; qm.push(nex); } } }}int main(){ while(~scanf("%d %d",&n,&m)) { memset(mmap,0,sizeof(mmap)); k = 0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { cin>>mmap[i][j]; if(mmap[i][j] == 'Y') { startY.x = i; startY.y = j; startY.step = 0; } if(mmap[i][j] == 'M') { startM.x = i; startM.y = j; startM.step = 0; } } } bfsY(); bfsM(); int This; int Min = 999999; for(int i=0;i<k;i++) { This = (StepY[kk[i].x][kk[i].y]+StepM[kk[i].x][kk[i].y])*11; Min = min(Min,This); } printf("%d\n",Min); } return 0;}
阅读全文
0 0
- hdu 2612 Find a way(BFS)
- HDU 2612 Find a way(两次BFS)
- HDU 2612 Find a way(BFS)
- HDU 2612 Find a way BFS
- Hdu 2612 Find a way(bfs)
- HDU 2612 Find a way (BFS)
- HDU 2612--Find a way【BFS】
- HDU 2612 Find a way(双BFS)
- hdu 2612 Find a way(双向bfs)
- HDU 2612 Find a way (bfs)
- HDU 2612 Find a way(bfs)
- HDU - 2612 - Find a way (BFS)
- HDU 2612 Find a way【bfs】
- BFS-HDU-2612-Find a Way
- HDU 2612 Find a way (BFS)
- hdu 2612 Find a way (BFS)
- HDU 2612 Find a way(两次bfs)
- HDU 2612 Find a way(BFS)
- oracle数据库修改用户名
- Redis String命令
- Vue条件循环判断+计算属性+绑定样式v-bind
- centos下配置nginx支持php
- 关于memparse函数的使用方法
- [HDU](2612)Find a way ---bfs
- 论可持久化Trie树贪心在区间最大最小值的运用
- 给LinearLayout的内部的view全部设置上分隔drawable(空白)
- 异常在类层次中的使用
- selenium之执行JavaScript脚本
- C# WinForm 两个窗体之间发送消息
- 怎样创建网页快捷方式,用非默认浏览器打开该网页
- 微擎缓存
- 【新国产化】采用开源技术的云计算厂商,能算国产化品牌吗?