Find a way
来源:互联网 发布:金融随机分析 知乎 编辑:程序博客网 时间:2024/05/17 09:15
F - Find a way
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64uDescription
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<stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>#include<queue>#include<stack>#include<iostream>#include<algorithm>using namespace std;const int MAXN = 213;char a[MAXN][MAXN];bool vis[MAXN][MAXN];int map[2][MAXN][MAXN];int m,n;int dir[4][2] = {0,1,1,0,0,-1,-1,0};struct node{int x,y,step;};bool ok(int x,int y){if(x<0||x>=n||y<0||y>=m){return false;}else{return true;}}void bfs(int idx,int x,int y){memset(vis,false,sizeof(vis));vis[x][y] = true;queue<node>q;node cur,next;cur.x = x;cur.y = y;cur.step = 0;q.push(cur);while(!q.empty() ){cur = q.front() ;q.pop() ;if(a[cur.x][cur.y] == '@'){map[idx][cur.x][cur.y] = cur.step ;}for(int i=0; i<4; i++){next.x = cur.x + dir[i][0];next.y = cur.y + dir[i][1];if(!vis[next.x][next.y] && a[next.x][next.y] != '#' && ok(next.x,next.y)){vis[next.x][next.y] = true;next.step = cur.step + 1;q.push(next); }}} }int main(){while(cin>>n>>m){int x1,y1,x2,y2;for(int i=0; i<n; i++){for(int j=0; j<m; j++){cin>>a[i][j];if(a[i][j] == 'Y')x1=i,y1=j;else if(a[i][j] == 'M')x2=i,y2=j;}}memset(map,0x3f3f3f3f,sizeof(map));bfs(0,x1,y1);bfs(1,x2,y2);int ans = 0x3f3f3f3f;for(int i=0; i<n; i++){for(int j=0; j<m; j++){if(a[i][j] == '@'){ans = min(ans,map[0][i][j] + map[1][i][j]);}}}printf("%d\n",ans*11);}return 0;}
0 0
- Find a way
- Find a way hdu2612
- Find a way
- Find a way 2612
- HDU2612---Find a way
- Find a way
- hdu2612---Find a way
- Find a way
- N. Find a way
- HDU2612 Find a way
- Find a way--hdoj
- hdu Find a way
- Find a way
- Find a way
- hdu Find a way
- Find a way
- Find a way<hdoj2612>
- HDU:Find a way
- 【Android学习笔记】canvas drawText居中问题
- 一些博客地址
- Java到底是值传递还是引用传递?
- 国标建筑结构分类
- jsp 日期格式化
- Find a way
- env中调用python或perl
- 剑指offer53--判断对称二叉树
- UITextField 点击事件 --- 不触发键盘弹出,而触发其他事件的实现
- JAVA全排列算法
- 搭建nginx反向代理用做内网域名转发
- 【OpenCV源码分析】core-Algorithm
- Android Material Design系列之夜间模式
- HDU 5775 (Bubble Sort 逆序数)