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

来源:互联网 发布:比较有名的网络女歌手 编辑:程序博客网 时间:2024/05/16 09:29

题目传送门


只有四把钥匙, 使用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 8char 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;}
0 0