Find a way
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19966 Accepted Submission(s): 6459
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
Accepted Code
// Author : Weihao Long
// Created : 2018/03/11
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
struct node {
int x;
int y;
int a;
int b;
char c;
};
node ak[250][250];
node s, e, now;
int main() {
int n, m;
while (cin >> n >> m) {
getchar();
char str[250];
for (int i = 1; i <= n; i++) {
gets(str);
for (int k = 1; k <= m; k++) {
ak[i][k].x = i;
ak[i][k].y = k;
ak[i][k].a = -1;
ak[i][k].b = -1;
ak[i][k].c = str[k - 1];
if (ak[i][k].c == `Y`)
s = ak[i][k];
if (ak[i][k].c == `M`)
e = ak[i][k];
}
}
s.a = 0;
e.b = 0;
queue <node> q;
q.push(s);
while (!q.empty()) {
now = q.front();
if (now.x - 1 >= 1 && ak[now.x - 1][now.y].c != `#` && ak[now.x - 1][now.y].a == -1) {
ak[now.x - 1][now.y].a = now.a + 1;
q.push(ak[now.x - 1][now.y]);
}
if (now.x + 1 <= n && ak[now.x + 1][now.y].c != `#` && ak[now.x + 1][now.y].a == -1) {
ak[now.x + 1][now.y].a = now.a + 1;
q.push(ak[now.x + 1][now.y]);
}
if (now.y - 1 >= 1 && ak[now.x][now.y - 1].c != `#` && ak[now.x][now.y - 1].a == -1) {
ak[now.x][now.y - 1].a = now.a + 1;
q.push(ak[now.x][now.y - 1]);
}
if (now.y + 1 <= m && ak[now.x][now.y + 1].c != `#` && ak[now.x][now.y + 1].a == -1) {
ak[now.x][now.y + 1].a = now.a + 1;
q.push(ak[now.x][now.y + 1]);
}
q.pop();
}
q.push(e);
while (!q.empty()) {
now = q.front();
if (now.x - 1 >= 1 && ak[now.x - 1][now.y].c != `#` && ak[now.x - 1][now.y].b == -1) {
ak[now.x - 1][now.y].b = now.b + 1;
q.push(ak[now.x - 1][now.y]);
}
if (now.x + 1 <= n && ak[now.x + 1][now.y].c != `#` && ak[now.x + 1][now.y].b == -1) {
ak[now.x + 1][now.y].b = now.b + 1;
q.push(ak[now.x + 1][now.y]);
}
if (now.y - 1 >= 1 && ak[now.x][now.y - 1].c != `#` && ak[now.x][now.y - 1].b == -1) {
ak[now.x][now.y - 1].b = now.b + 1;
q.push(ak[now.x][now.y - 1]);
}
if (now.y + 1 <= m && ak[now.x][now.y + 1].c != `#` && ak[now.x][now.y + 1].b == -1) {
ak[now.x][now.y + 1].b = now.b + 1;
q.push(ak[now.x][now.y + 1]);
}
q.pop();
}
int ans = 0xFFFF;
for (int i = 1; i <= n; i++)
for (int k = 1; k <= m; k++) {
if (ak[i][k].c == `@` && ans > ak[i][k].a + ak[i][k].b && ak[i][k].a != -1 && ak[i][k].b != -1)
ans = ak[i][k].a + ak[i][k].b;
}
cout << ans * 11 << endl;
}
return 0;
}
Notes
題意:
兩人約在一家肯德基面基,但地圖上有很多家肯德基,你要找到一家使兩人的路程和最小。
演算法:
廣搜。
坑點:
可能有肯德基被完全包圍。