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.
#...#
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#
Sample Output
66
88
66
88
66
问题分析:依旧是bfs,不过要考虑kfc被包围,以及重置地图,和两人所占位都是可以互相走过的。
#include "iostream"
#include "queue"
using namespace std;
struct ma
{
char z;
int sum;
};
struct person
{
int i;
int j;
int time;
};
int v[][] ={,,-,,,-,,};
ma maze[][];
ma maze2[][];
person fir,sec;
void scopy(int n,int m)
{
for (int i=;i<=n+;i++)
for (int j=;j<=m+;j++)
{
if (maze2[i][j].z == '@')
maze[i][j].z = '@';
else
maze[i][j]=maze2[i][j];
}
}
void mbegin(int n,int m)
{
int i,j;
for (i=;i<=n+;i++)
for (j=;j<=m+;j++)
if (i*j == || i == n+ || j ==m+)
maze2[i][j].z ='#';
else
{
cin>>maze2[i][j].z;
maze2[i][j].sum=;
maze[i][j].sum=;
if (maze2[i][j].z == 'Y')
{
fir.i = i;
fir.j = j;
}
if (maze2[i][j].z == 'M')
{
sec.i =i;
sec.j =j;
}
}
}
void bfs(person &then)
{
queue<person> p;
person next;
then.time=;
maze[then.i][then.j].z = '#';
p.push(then);
while (!p.empty())
{
next = p.front();
p.pop();
for (int k=;k<;k++)
{
then.i = next.i+v[k][];
then.j = next.j+v[k][];
if (maze[then.i][then.j].z != '#')
{
then.time = next.time+;
if (maze[then.i][then.j].z == '@')
maze[then.i][then.j].sum += then.time;
maze[then.i][then.j].z ='#';
p.push(then);
}
}
}
}
int mintime(int n,int m)
{
int k=,time;
for (int i=;i<=n;i++)
for (int j=;j<=m;j++)
if (maze[i][j].z == '@')
{
if (maze[i][j].sum == )
continue;
if (k++ == )
time=maze[i][j].sum;
if (time > maze[i][j].sum)
time=maze[i][j].sum;
}
return time;
}
int main()
{
int n,m;
while (cin>>n>>m)
{
mbegin(n,m);
scopy(n,m);
bfs(fir);
scopy(n,m);
bfs(sec);
scopy(n,m);
cout<<mintime(n,m)<<endl;
}
return ;
}