Time limit | 1000 ms |
---|---|
Memory limit | 32768 kB |
OS | Windows |
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.
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
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.
4 4
Y.#@
....
.#..
@..M
4 4
Y.#@
....
.#..
@#.M
5 5
Y..@.
.#...
.#...
@..M.
#...#
66
88
66
经过一年的杭州学习,伊芬菲终于来到了家乡宁波。离开宁波一年,伊芬菲有很多人要见,尤其是他的朋友美世。伊芬菲的家在农村,美世的家在市中心。于是,伊芬菲和美世安排在肯德基见面。宁波有很多肯德基,他们想选择一个让总时间最短的。现在给你一张宁波地图,已知伊芬菲和美世上下左右移动到相邻的道路上要花费11分钟。
输入包含多个测试用例
每个测试用例包括两个整数n,m(2<=n,m<=200)
接下来的n行中,每行包含m个字符
"Y"表示伊芬菲的初始位置
"M"表示美世的初始位置
"#"死路
"."道路
"@"KFC
对于每一个测试用例,输出伊芬菲和美世到达其中一家KFC的最短总时间
可以确定,总有一家可以让他们都到达的KFC
样例输入输出如上
这里有我大哥发现个bug
就是测试例子中没有一方到不了的KFC或者有但时间比总时间长
我之前只用一个数组存到KFC的时间,两个BFS间没有清零,就是直接加然后AC了
那个只用15ms就过了…但一想不符合题意所以我新改了一版
Time | 46ms |
---|---|
Memory | 1820kB |
Length | 1631 |
Lang | G++ |
#include
#include
#include
using namespace std;
struct zb{
int x;
int y;
}a,b;
int m, n;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};/*第一次不用复制粘贴 建了个方向数组*/
int step[205][205];
char map[205][205];
int KFC_Y[205][205], KFC_M[205][205];
int judge (zb d) {
int f = 1;
if(0 > d.x || d.x >= n || 0 > d.y || d.y >= m) f = 0;
else if(step[d.x][d.y]) f = 0;
else if (map[d.x][d.y] == '#') f = 0;
return f;
}
void bfs(zb c, int KFC[205][205]){
zb d;
queueq;
q.push(c);
while (!q.empty()) {
c = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
d.x = c.x + dir[i][0];
d.y = c.y + dir[i][1];
if(!judge(d)) continue;
step[d.x][d.y] = step[c.x][c.y] + 1;
if(map[d.x][d.y] == '@') KFC[d.x][d.y] = step[d.x][d.y];/*单独存了下到KFC的时间*/
q.push(d);
}
}
}
int main(int argc, char const *argv[]) {
while (~scanf("%d %d", &n, &m)) {
int yi, yj, mi, mj;
memset(KFC_M, 0, sizeof(KFC_M));
memset(KFC_Y, 0, sizeof(KFC_Y));
memset(step, 0, sizeof(step));
for (int i = 0; i < n; i ++) {
scanf("%s", map[i]);
for(int j = 0;j < m;j ++) {
if(map[i][j] == 'Y') a.x = i, a.y = j;
if(map[i][j] == 'M') b.x = i, b.y = j;/*记一下起点位置*/
}
}
bfs(a, KFC_Y);
memset(step, 0, sizeof(step));/*这个一定不要忘ya*/
bfs(b, KFC_M);
int min = 0x3f3f3f3f;
for (int i = 0; i < n; i ++) {
for(int j = 0;j < m;j ++) {
if(KFC_M[i][j] != 0 && KFC_Y[i][j] != 0 && KFC_M[i][j] + KFC_Y[i][j] < min){
min = KFC_M[i][j] + KFC_Y[i][j];
}
}
}
printf("%d\n", 11* min);/*走一步11分钟呢,没想到吧..反正我之前忘了*/
}
return 0;
}