牛客网练习赛12迷宫(宽搜)

题目链接:

点击打开链接


里面设置了一扇大门和钥匙,但是到底要不要走过大门还不一定所以要都搜一下,取要还是不要最小值.

经过大门的搜法是先搜钥匙,再删了门,最后从门搜到终点.

注意有可能拿不到钥匙.

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
#define f first
#define s second

int H, W;
int vx[] = {0, 0, 1, -1};
int vy[] = {-1, 1, 0, 0};

int sx, sy, dx, dy, kx, ky, xx, yy;
bool used[505][505];
char maze[505][505];
int ans[505][505];
bool k[505][505];

int bfs(int goal)
{
    queue<P> que;
    que.push(P(sx, sy));
    memset(used, 0, sizeof used);
    memset(ans, 0, sizeof ans);
    memset(k, 0, sizeof k);

    while(!que.empty())
    {
        P head = que.front();
        que.pop();
        int x = head.f, y = head.s;
        used[x][y] = true;

        if(goal == 0 && x == dx && y == dy)
            return ans[x][y];
        else if(goal == 1 && x == kx && y == ky)
            return ans[x][y];

        for(int i = 0; i < 4; i++)
        {
            int nx = x + vx[i], ny = y + vy[i];

            if(0 <= nx && nx < H && 0 <= ny && ny < W && maze[nx][ny] != 'W' && maze[nx][ny] != 'D' && !used[nx][ny])
            {
                ans[nx][ny] = ans[x][y] + 1;
                que.push(P(nx, ny));
            }
        }
    }

    return -1;
}

int main()
{

    while(scanf("%d%d", &H, &W) != EOF)
    {
        for(int i = 0; i < H; i++)
            scanf("%s", maze[i]);

        for(int i = 0; i < H; i++)
            for(int j = 0; j < W; j++)
            {
                if(maze[i][j] == 'S')
                {
                    sx = i;
                    sy = j;
                }
                if(maze[i][j] == 'K')
                {
                    kx = i;
                    ky = j;
                }
                if(maze[i][j] == 'E')
                {
                    dx = i;
                    dy = j;
                }
                if(maze[i][j] == 'D')
                {
                    xx = i; yy = j;
                }
            }

        int a = bfs(0);
        int b = bfs(1);
        sx = kx;
        sy = ky;
        maze[xx][yy] = '.';
        int c = bfs(0);

        if(a == -1 && b == -1)
            printf("-1\n");
        else if(a != -1 && b == -1)
            printf("%d\n", a);
        else if(a == -1 && b != -1 && c != -1)
            printf("%d\n", c + b);
        else if(a == -1 && c == -1)
            printf("-1\n");
        else if(a != -1 && b != -1 && c != -1)
            printf("%d\n", min(a, b + c));
    }

    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值