Hdu-1885-Key Task [状态压缩][bfs]


题目传送门


只有四把钥匙, 使用2^4中状态可以表示,碰到门的时候,跟当前的key与一下就知道是否可以开门, 剩下的就是个简单的bfs, 注意不止有一个出口。
PS:代码写得真的挫。


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
using namespace std;
struct node
{
    int x, y,step;
    int key;
};
int vis[110][110][20]; // b 1 y 2 r 4 g 8
char Map[110][110];
int N[4][2]= {1,0,-1,0,0,1,0,-1};
int R,C,ex,ey;
void bfs(int sx,int sy)
{
    queue<struct node>q;
    struct node a;
    a.x=sx;
    a.y=sy;
    a.step=0;
    q.push(a);
    int i = 0;
    while (!q.empty())
    {
        a = q.front();
        q.pop();
        for (int i = 0; i < 4; i++)
        {
            struct node b;
            b.x = a.x + N[i][0];
            b.y = a.y + N[i][1];
            b.key = a.key;
            if (b.x<0||b.x>=R||b.y<0||b.y>=C)
                continue;
            if (Map[b.x][b.y]=='#')
                continue;
            b.step = a.step + 1;
            if (Map[b.x][b.y]=='X')
            {
                printf("Escape possible in %d steps.\n", b.step);
                return;
            }
            if (Map[b.x][b.y]=='B')
            {
                int nk = a.key&1;
                if (nk&&!vis[b.x][b.y][b.key])
                {
                    vis[b.x][b.y][b.key]=1;
                    q.push(b);
                }
            }
            else if (Map[b.x][b.y]=='b')
            {
                b.key = a.key|1;
                if (!vis[b.x][b.y][b.key])
                {
                    vis[b.x][b.y][b.key]=1;
                    q.push(b);
                }
            }
            else if (Map[b.x][b.y]=='Y')
            {
                int nk = a.key&2;
                if (nk&&!vis[b.x][b.y][b.key])
                {
                    vis[b.x][b.y][b.key]=1;
                    q.push(b);
                }
            }
            else if (Map[b.x][b.y]=='y')
            {
                b.key = a.key|2;
                if (!vis[b.x][b.y][b.key])
                {
                    vis[b.x][b.y][b.key]=1;
                    q.push(b);
                }
            }
            else if (Map[b.x][b.y]=='R')
            {
                int nk = a.key&4;
                if (nk&&!vis[b.x][b.y][b.key])
                {
                    vis[b.x][b.y][b.key]=1;
                    q.push(b);
                }
            }
            else if (Map[b.x][b.y]=='r')
            {
                b.key = a.key|4;
                if (!vis[b.x][b.y][b.key])
                {
                    vis[b.x][b.y][b.key]=1;
                    q.push(b);
                }
            }
            else if (Map[b.x][b.y]=='G')
            {
                int nk = a.key&8;
                if (nk&&!vis[b.x][b.y][b.key])
                {
                    vis[b.x][b.y][b.key]=1;
                    q.push(b);
                }
            }
            else if (Map[b.x][b.y]=='g')
            {
                b.key = a.key|8;
                if (!vis[b.x][b.y][b.key])
                {
                    vis[b.x][b.y][b.key]=1;
                    q.push(b);
                }
            }
            else
            {
                if (!vis[b.x][b.y][b.key])
                {
                    vis[b.x][b.y][b.key]=1;
                    q.push(b);
                }
            }
        }
    }
    printf("The poor student is trapped!\n");
    return;
}
int main(void)
{
    while (scanf("%d %d", &R, &C))
    {
        int sx, sy;
        if (R==0&&C==0)
            break;
        memset(vis,0,sizeof(vis));
        for (int i = 0; i < R; i++)
        {
            scanf(" %s", Map[i]);
        }
        for (int i = 0; i < R; i++)
        {
            for (int j = 0; j < C; j++)
            {
                if (Map[i][j]=='*')
                {
                    sx=i;
                    sy=j;
                    Map[i][j]='.';
                }
            }
        }
        bfs(sx,sy);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值