Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 403 Accepted Submission(s): 129
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
解题心得:
1、关于双向bfs的判定,一般是有两个移动的点,没有目标或者一个目标,当有三个或者更多的移动的点的时候一般要考虑是否可以用一些技巧消去一些。
2、双向bfs在找最小的路径之和的时候并不是找到了之后马上跳出,因为并不是第一个相遇的点一定是最短的点,所以在不超时,数据量比较小的时候尽量跑完,不然很可能在某个角落差生错误,找都找不到,在跑双向bfs 的时候标记一定要弄好,不然很目标是一个bfs跑了两次,这就很恼火了。
#include
using namespace std;
const int maxn = 210;
char maps[maxn][maxn];
int y_x,y_y,m_x,m_y;
int dir[4][2] ={0,1,0,-1,1,0,-1,0};
int n,m,Min,num_2;
bool use[2][maxn][maxn];//两个bfs,标记也要开两层
struct Vis
{
int num;//记录这个@点被找到了几次
bool is;
int sum;//用来记录两个bfs在@点相遇的时间的和
} vis[maxn][maxn];
struct node
{
int x,y;
};
queueq[2],qt;//两个bfs的队列和层使用的队列
bool check(int x,int y)//检查一下可不可以走到那里
{
if(x<0 || y<0 || x>=n || y>=m)
return true;
if(maps[x][y] == '#')
return true;
return false;
}
void pre_maps()
{
//初始化很重要
Min = 0x7f7f7f7f;
memset(use,0,sizeof(use));
num_2 = 0;
while(!q[0].empty())
q[0].pop();
while(!q[1].empty())
q[1].pop();
while(!qt.empty())
qt.pop();
for(int i=0; i