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): 25032 Accepted Submission(s): 8161
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
[email protected]
.#...
.#...
@..M.
#...#
Sample Output
66
88
66
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612
题意
有两个人想要见面,他们各自从自己家开始出发,找同一个KFC碰面,问你最小时间是多少。每走一次11min,可以上下左右4个方向走。
思路
分别调用两次BFS,把每一次到达的@的步数记录下来,最后加起来两次的,找最小的即可。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 205;
int n, m;
char s[maxn][maxn];
bool book[maxn][maxn];
struct node{
int x;
int y;
int step;
};
int dis[maxn][maxn];
int dis1[maxn][maxn];
int Move[4][2] = { {1,0}, {0,1}, {-1,0}, {0,-1}};
void bfs(node st, int Step[205][205])
{
memset( book, 0, sizeof(book));
queue <node> q;
node Now, Next;
st.step = 0;
q.push(st);
book[st.x][st.y] = true;
while(!q.empty()) {
Now = q.front();
q.pop();
for(int i = 0; i < 4; i++) {
Next.x = Now.x + Move[i][0];
Next.y = Now.y + Move[i][1];
Next.step = Now.step + 1;
if(Next.x < 0 || Next.y < 0 || Next.x >= n || Next.y >= m) {
continue;
}
if(s[Next.x][Next.y] != '#' && book[Next.x][Next.y] == false) {
if(s[Next.x][Next.y] == '@') {
Step[Next.x][Next.y] = Next.step;
}
q.push(Next);
book[Next.x][Next.y] = 1;
}
}
}
}
int main()
{
while(cin >> n >> m) {
memset( dis, 0, sizeof(dis));
memset( dis1, 0 , sizeof(dis1));
int x0, y0, x1, y1;
for(int i = 0; i < n; i++) {
getchar();
for(int j = 0; j < m; j++) {
scanf("%c", &s[i][j]);
if(s[i][j] == 'Y') {
x0 = i; y0 = j;
}
if(s[i][j] == 'M') {
x1 = i; y1 = j;
}
}
}
node Y, M;
Y.x = x0;
Y.y = y0;
M.x = x1;
M.y = y1;
bfs(Y, dis);
bfs(M, dis1);
int ans = 99999999;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++) {
if(s[i][j] == '@' && dis[i][j] != 0 && dis1[i][j] != 0) {
if(dis[i][j] + dis1[i][j] < ans)
ans = dis[i][j] + dis1[i][j];
}
}
cout << ans*11 << endl;
}
return 0;
}
上一篇: 图-邻接矩阵
推荐阅读
-
C语言BFS--Find a way(Hdu 2612)
-
hdu2612 Find a way BFS
-
POJ 1426 Find The Multiple(BFS)
-
C语言BFS--Find a way(Hdu 2612)
-
POJ 1426 Find The Multiple(BFS&&DFS)
-
HDU-2612 Find a way(BFS)
-
hdu2612 Find a way BFS
-
POJ1426-Find The Multiple(BFS+同余)
-
Find a way out of the ClassLoader maze
-
Find a way out of the ClassLoader maze