Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 37587 Accepted Submission(s): 11925
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 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#
Sample Output
66
88
66
Author
yifenfei
Source
奋斗的年代
Recommend
yifenfei
解题思路: BFS,细心。
解体代码:
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef pair<int, int> PII;
const int N = 250;
int m, n;
char map[N][N]; //存地图;
int d_Y[N][N], d_M[N][N]; int d_Y[N][N], d_M[N][N]; // Y到地图上点的距离,M到地图上点的距离
int a1, b1; //y 的坐标;
int a2, b2; //m 的坐标;
void chazhao1(int x_a1, int y_b1)
{
PII q[N * N];
int hh = 0, tt = 0;
q[0] = {x_a1, y_b1};
memset(d_Y, - 1, sizeof d_Y);
d_Y[x_a1][y_b1] = 0;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
while(hh <= tt)
{
PII t = q[hh++];
for(int i = 0; i < 4; i ++)
{
int x = t.first + dx[i], y = t.second + dy[i];
if(x >= 0 && x < n && y >= 0 && y < m && map[x][y] != '#' && d_Y[x][y] == -1)
{
d_Y[x][y] = d_Y[t.first][t.second] + 1;
q[++tt] = {x, y};
}
}
}
}
void chazhao2(int x_a2, int y_b2)
{
PII q[N * N];
int hh = 0, tt = 0;
q[0] = {x_a2, y_b2};
memset(d_M, - 1, sizeof d_M);
d_M[x_a2][y_b2] = 0;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
while(hh <= tt)
{
PII t = q[hh++];
for(int i = 0; i < 4; i ++)
{
int x = t.first + dx[i], y = t.second + dy[i];
if(x >= 0 && x < n && y >= 0 && y < m && map[x][y] != '#' && d_M[x][y] == -1)
{
d_M[x][y] = d_M[t.first][t.second] + 1;
q[++tt] = {x, y};
}
}
}
}
int main()
{
while(cin >> n >> m)
{
//存地图;
for(int i = 0; i <= n - 1; i ++)
{
for(int j = 0; j <= m - 1; j ++)
{
cin >> map[i][j];
}
getchar();
}
//遍历地图 , 找肯德基的位置;
int idx = 0;
for(int i = 0; i <= n - 1; i ++)
{
for(int j = 0; j <= m - 1; j ++)
{
if(map[i][j] == 'Y')
{
a1 = i;
b1 = j;
}
if(map[i][j] == 'M')
{
a2 = i;
b2 = j;
}
}
}
//找距离;
chazhao1(a1, b1);
chazhao2(a2, b2);
int aws = 99999999;
for(int i = 0; i <= n - 1; i ++)
{
for(int j = 0; j <= m - 1; j ++)
{
if(map[i][j] == '@' && (d_Y[i][j] + d_M[i][j] < aws) && d_M[i][j] != -1 && d_M[i][j] != -1)
aws = d_Y[i][j] + d_M[i][j];
}
}
memset(map, 'q', sizeof map);
cout << aws * 11 << endl;
}
return 0;
}
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612