D - 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 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@
.
.#.
..
.#
...
@
..M.
#...#
Sample Output
66
88
66
//首先,我的思路是找到一个KFC就从两个点出发,去寻找这个KFC,不断刷新最小的距离,但是这样会超时。
去网上看了一下,换了一种做法,先从两个点出发,去遍历一次地图,将到达地图任意可达的位置的最小步数记录下来,然后在用循环遍历一次地图,找到最小之和。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
struct point
{
int x,y;
int step;
};
point py,pm;
int m,n;
char map[210][210];
int test_y[210][210];
int test_m[210][210];
int min_all;
void Init()
{
for (int i=1;i<=m;i++)
{
for (int j=1;j<=n;j++)
{
cin>>map[i][j];
if (map[i][j]=='Y')
{
py.x=i;
py.y=j;
py.step=0;
}
if (map[i][j]=='M')
{
pm.x=i;
pm.y=j;
pm.step=0;
}
}
}
}
int check_y(point t)
{
if (t.x<=m&&t.x>=1&&t.y>=1&&t.y<=n&&test_y[t.x][t.y]==0&&map[t.x][t.y]!='#')
return 1;
return 0;
}
int check_m(point t)
{
if (t.x<=m&&t.x>=1&&t.y>=1&&t.y<=n&&test_m[t.x][t.y]==0&&map[t.x][t.y]!='#')
return 1;
return 0;
}
void bfs()
{
queue<point> Q;
point now,next;
while (!Q.empty()) Q.pop();
memset(test_y,0,sizeof(int)*(m+1)*210);
now.x=py.x;
now.y=py.y;
now.step=0;
Q.push(now);
while (!Q.empty())
{
now=Q.front();
Q.pop();
next.x=now.x+1;
next.y=now.y;
next.step=now.step+1;
if (check_y(next)){ Q.push(next); test_y[next.x][next.y]=next.step;}
next.x=now.x;
next.y=now.y-1;
next.step=now.step+1;
if (check_y(next)){ Q.push(next); test_y[next.x][next.y]=next.step;}
next.x=now.x-1;
next.y=now.y;
next.step=now.step+1;
if (check_y(next)){ Q.push(next); test_y[next.x][next.y]=next.step;}
next.x=now.x;
next.y=now.y+1;
next.step=now.step+1;
if (check_y(next)){ Q.push(next); test_y[next.x][next.y]=next.step;}
}
while (!Q.empty()) Q.pop();
memset(test_m,0,sizeof(int)*(m+1)*210);
now.x=pm.x;
now.y=pm.y;
now.step=0;
Q.push(now);
while (!Q.empty())
{
now=Q.front();
Q.pop();
next.x=now.x+1;
next.y=now.y;
next.step=now.step+1;
if (check_m(next)){ Q.push(next); test_m[next.x][next.y]=next.step;}
next.x=now.x;
next.y=now.y-1;
next.step=now.step+1;
if (check_m(next)){ Q.push(next); test_m[next.x][next.y]=next.step;}
next.x=now.x-1;
next.y=now.y;
next.step=now.step+1;
if (check_m(next)){ Q.push(next); test_m[next.x][next.y]=next.step;}
next.x=now.x;
next.y=now.y+1;
next.step=now.step+1;
if (check_m(next)){ Q.push(next); test_m[next.x][next.y]=next.step;}
}
}
int main()
{
while (cin>>m>>n&&m&&n)
{
Init();
bfs();
min_all=999999;
for (int i=1;i<=m;i++)
{
for (int j=1;j<=n;j++)
{
if (map[i][j]=='@' && test_y[i][j]+test_m[i][j] < min_all&& test_y[i][j]!=0 && test_m[i][j]!=0 )//
{
min_all=test_y[i][j]+test_m[i][j];
}
}
}
cout<<min_all*11<<endl;
}
return 0;
}