hdu2612——Find a way

来源:互联网 发布:淘宝哪种付款方式最快 编辑:程序博客网 时间:2024/04/29 20:55
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 4Y.#@.....#..@..M4 4Y.#@.....#..@#.M5 5Y..@..#....#...@..M.#...#
 

Sample Output
668866
 

Author


简单的BFS  对两个点bfs就行

#include<queue>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;int dir[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };char mat[205][205];bool vis1[205][205];bool vis2[205][205];int ans1[205][205];int ans2[205][205];struct node{int x,y;int t;}temp1, temp2;queue< node >qu;int n, m;bool is_legal(int x, int y){  if( x < 0 || x >= n || y < 0 || y >= m || mat[x][y] == '#')    return false;  return true;}int bfs1(int sx, int sy){memset( vis1, 0, sizeof(vis1) );while( !qu.empty() )qu.pop();temp1.x = sx;temp1.y = sy;temp1.t = 0;vis1[temp1.x][temp1.y] = 1;ans1[temp1.x][temp1.y] = 0;qu.push(temp1);while( !qu.empty() ){temp1 = qu.front();qu.pop();for(int i = 0; i < 4; i ++){temp2.x = temp1.x + dir[i][0];temp2.y = temp1.y + dir[i][1];if( !is_legal(temp2.x, temp2.y) )continue;if(vis1[temp2.x][temp2.y] == 1)continue;vis1[temp2.x][temp2.y] = 1;temp2.t = temp1.t + 11;ans1[temp2.x][temp2.y] = temp2.t;qu.push(temp2);}}}int bfs2(int sx, int sy){memset( vis2, 0, sizeof(vis1) );while( !qu.empty() )qu.pop();temp1.x = sx;temp1.y = sy;temp1.t = 0;vis2[temp1.x][temp1.y] = 1;ans2[temp1.x][temp1.y] = 0;qu.push(temp1);while( !qu.empty() ){temp1 = qu.front();qu.pop();for(int i = 0; i < 4; i ++){temp2.x = temp1.x + dir[i][0];temp2.y = temp1.y + dir[i][1];if( !is_legal(temp2.x, temp2.y) )continue;if(vis2[temp2.x][temp2.y] == 1)continue;vis2[temp2.x][temp2.y] = 1;temp2.t = temp1.t + 11;ans2[temp2.x][temp2.y] = temp2.t;qu.push(temp2);}}}int main(){while( ~scanf("%d%d", &n, &m) ){int x1, y1, x2, y2, min_ans = 0x3f3f3f3f;for(int i = 0; i < n; i ++){scanf("%s", mat[i]);for(int j = 0; j < m; j ++){if(mat[i][j] == 'Y'){x1 = i;y1 = j;}else if(mat[i][j] =='M'){x2 = i;y2 = j;}}}bfs1(x1,y1);bfs2(x2,y2);for(int i = 0; i < n; i ++)for(int j = 0; j < m; j ++){if(mat[i][j] == '@' && vis1[i][j] && vis2[i][j]){min_ans = min(min_ans, ans1[i][j] + ans2[i][j]);}}printf("%d\n", min_ans);}return 0;}


0 0