N. Find a way

题目网址:http://www.bnuoj.com/v3/contest_show.php?cid=6453#problem/N

题目描述:

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.

Imput:

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

解题思路:两次广搜加枚举到每个KFC店的最短距离,初始化时注意全部为最大,因为存在不可能到达的KFC店,所以如果初始化为0,则枚举时最小距离为0,因此最开始应看成所有的KFC店都不能到达,初始化一个很大的值。就是这点错误,这题WA了4次~~

源代码如下:

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define MAXN 200 + 10

struct Node {
    int x, y;
};
char MAPT[MAXN][MAXN];
int dir[4][2] = {-1, 0, 0, -1, 1, 0, 0, 1};
int vis[MAXN][MAXN], mdist[MAXN][MAXN], ydist[MAXN][MAXN];
int M, N;

int check(int xx, int yy) {
    int flag = 1;
    if (xx < 0 || xx >= M || yy < 0 || yy >= N) flag = 0;
    if (vis[xx][yy]) flag = 0;
    return flag;
}
int BFS(int sx, int sy, char ch) {
    queue<Node> q;
    Node now, pre;
    int i, j;
    now.x = sx; now.y = sy;
    q.push(now);
    vis[now.x][now.y] = 1;
    while (!q.empty()) {
        pre = q.front(); q.pop();
        for (i = 0; i < 4; i++) {
            now.x = pre.x + dir[i][0];
            now.y = pre.y + dir[i][1];
            if (check(now.x, now.y) && (MAPT[now.x][now.y] != '#')) {
                q.push(now);
                vis[now.x][now.y] = 1;
                if (ch == 'Y') ydist[now.x][now.y] = ydist[pre.x][pre.y] + 1;
                if (ch == 'M') mdist[now.x][now.y] = mdist[pre.x][pre.y] + 1;
                //if (MAPT[now.x][now.y] == ch) return dist[now.x][now.y];
            }
        }
    }
    return 0;
}
int main() {
    //FILE *p = freopen("test.txt", "r", stdin);
    int i, j;
    while (scanf("%d%d", &M, &N) != EOF) {
        for (i = 0; i < M; i++) {
            scanf("%s", MAPT[i]);
        }
        int sx = 0, sy = 0, ex = 0, ey = 0;
        for (i = 0; i < M; i++) {
            for (j = 0; j < N; j++) {
                if (MAPT[i][j] == 'Y') {
                    sx = i; sy = j;
                }
                if (MAPT[i][j] == 'M') {
                    ex = i; ey = j;
                }
            }
        }
        memset(vis, 0, sizeof(vis));
        memset(ydist, 0, sizeof(ydist));
        BFS(sx, sy, 'Y');
        memset(vis, 0, sizeof(vis));
        memset(mdist, 0, sizeof(mdist));
        BFS(ex, ey, 'M');
        int ans = 11111111;
        for (i = 0; i < M; i++) {
            for (j = 0; j < N; j++) {
                if (MAPT[i][j] == '@' && mdist[i][j] != 0 && ydist[i][j] != 0) {
                    ans = min(ans, mdist[i][j] + ydist[i][j]);
                }
            }
        }
        printf("%d\n", ans * 11);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值