HDU - 2612 Find a way(BFS)
Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 22363 Accepted Submission(s): 7293
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 4 Y.#@ .... .#.. @..M 4 4 Y.#@ .... .#.. @#.M 5 5 Y..@. .#... .#... @..M. #...#
Sample Output
66 88 66
Author
yifenfei
这道题就是有两个起点Y,M,想知道他们最短的路径和到达最近@是多少,其中#不能走;
但是有一个点我要更加认真的反思下,以后绝对不能用0来初始化vis数组,非常容易会使不能到达的地方(0),失误的进行运算!!;
#include<iostream>
#include<cstdio>
#include<queue>
#include<utility>
using namespace std;
int m, n;
char map[205][205];
int fx[4][2] = { 0,1,1,0,-1,0,0,-1 };
int fir_vis[205][205];
int sec_vis[205][205];
void BFS(int x, int y, int vis[][205])
{
vis[x][y] = 1;
queue<pair<int, int>>q;
q.push(make_pair(x, y));
while (!q.empty())
{
pair<int,int>top = q.front();
q.pop();
for (int s = 0; s < 4; s++)
{
int li = top.first + fx[s][0];
int ri = top.second + fx[s][1];
if (li >= 0 && li < m&&ri >= 0 && ri < n&& !vis[li][ri]&&map[li][ri]!='#')
{
vis[li][ri] = vis[top.first][top.second] + 1;
q.push(make_pair(li, ri));
}
}
}
}
int main()
{
while (cin >> m >> n)
{
memset(fir_vis, 0, sizeof(fir_vis));
memset(sec_vis, 0, sizeof(sec_vis));
for (int s = 0; s < m; s++)
{
for (int w = 0; w < n; w++)
{
cin >> map[s][w];
}
}
for (int s = 0; s < m; s++)
{
for (int w = 0; w < n; w++)
{
if (map[s][w] == 'Y')
{
BFS(s, w, fir_vis);
}
if (map[s][w] == 'M')
{
BFS(s, w, sec_vis);
}
}
}
int ans = 0x3f3f3f3f;
for (int s = 0; s < m; s++)
{
for (int w = 0; w < n; w++)
{
if (map[s][w] == '@'&&fir_vis[s][w]!=0&&sec_vis[s][w]!=0)
{
ans = min(ans, fir_vis[s][w] + sec_vis[s][w]);
}
}
}
cout << 11*ans - 22 << endl;
}
return 0;
}