您的位置:首页 > 其它

hdu 2612 Find a way (广搜)

2013-05-06 20:44 363 查看
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

哈哈,这是第一次自己做广搜的题目,wrong answer - 超时 这就是大半天。

犯得主要错误:

1、没有对连个队列访问的点进行标记,导致重复访问。

2、总觉得广搜首先搜出来的相遇地点就是最短的。

我同学一句话就把我点醒了,首先搜出来的是Y M两个距离相差最小的相遇地点,却不是距离和最小的。

所以,最后还要找出距离和最小的那个相遇地点。

View Code

#include<iostream>
#include<queue>
#define inf 0x7f7f7f7f7f
using namespace std;

char map[210][210];
typedef struct
{
int x ,y;
int step;
}node;
int vis_Y[210][210];
int vis_M[210][210];
int vis[210][210];
int end_step[210][210];
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};
int n , m;

int bfs(node &temp , int i , int flag)
{
temp.x += dx[i];
temp.y += dy[i];
if(temp.x<0||temp.x>=n||temp.y<0||temp.y>=m) return 0; //越界
if(flag)
if(vis_Y[temp.x][temp.y]) return 0;  //已经访问
if(!flag)
if(vis_M[temp.x][temp.y]) return 0;  //已经访问
if(map[temp.x][temp.y] == '#')  return 0;
else if(map[temp.x][temp.y] == '.')
{
if(flag) vis_Y[temp.x][temp.y] = 1;
else vis_M[temp.x][temp.y] = 1;
}
else if(map[temp.x][temp.y] == '@')
{
if(flag) vis_Y[temp.x][temp.y] = 1;
else vis_M[temp.x][temp.y] = 1;
vis[temp.x][temp.y]++;
end_step[temp.x][temp.y] += temp.step+1;
}
temp.step++;
return 1;
}
int main()
{
int i , j , min;
while(cin>>n>>m)
{
memset(vis_Y , 0 , sizeof(vis_Y));
memset(vis_M , 0 , sizeof(vis_M));
memset(vis , 0 , sizeof(vis));
memset(end_step , 0 , sizeof(end_step));
min = inf;
queue<node>q_Y;
queue<node>q_M;
node start_Y , start_M;
for(i = 0; i < n; i++)
{
getchar();
for(j = 0; j < m; j++)
{
scanf("%c" , &map[i][j]);
if(map[i][j] == 'Y')
{start_Y.x = i; start_Y.y = j; start_Y.step = 0; vis_Y[i][j] = 1;}
else if(map[i][j] == 'M')
{start_M.x = i; start_M.y = j; start_M.step = 0; vis_M[i][j] = 1;}
}
map[i][j] = '\0';
}
q_Y.push(start_Y);
q_M.push(start_M);
while(!q_Y.empty() && !q_M.empty())
{
int flag = 0;
node  temp1 = q_Y.front();
node  temp2 = q_M.front();
q_Y.pop();  q_M.pop();
for(i = 0; i < 4; i++)
{
node temp3 = temp1;
node temp4 = temp2;
if(bfs(temp3 , i , 1))
q_Y.push(temp3);
if(bfs(temp4 , i , 0))
q_M.push(temp4);
}
}
for(i = 0; i < n; i++)
for(j = 0; j < m; j++)
if(vis[i][j] == 2)
{
if(min > end_step[i][j])
min = end_step[i][j];
}
cout<<min*11<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: