HDU 1885 Key Task (状态压缩+BFS)

来源:互联网 发布:优乐视高清网络播放器 编辑:程序博客网 时间:2024/06/04 18:19

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1885

代码:

#include<stdio.h>#include<string.h>#include<queue>#include<iostream>using namespace std;struct node{    int x,y;    int temp,step;}ui,op;char maps[105][105];int dis[105][105][1<<4];int fx[4]={1,0,-1,0};int fy[4]={0,1,0,-1};int r,c;int startx,starty;char men[4]={'B','Y','R','G'};char key[4]={'b','y','r','g'};void bfs(int x,int y){    memset(dis,0,sizeof(dis));    ui.x=x;    ui.y=y;    ui.temp=ui.step=0;    dis[x][y][0]=0;    queue<node>q;    q.push(ui);    while(!q.empty())    {        ui=q.front();        q.pop();        if(maps[ui.x][ui.y]=='X')        {            printf("Escape possible in %d steps.\n",ui.step);            return;        }        for(int i=0;i<4;i++)        {            op.x=ui.x+fx[i];            op.y=ui.y+fy[i];            op.temp=ui.temp;            op.step=ui.step+1;                        if(op.x>=0&&op.x<r&&op.y>=0&&op.y<c&&maps[op.x][op.y]!='#')            {                if(islower(maps[op.x][op.y]))                //小写                {                    for(int j=0;j<4;j++)                    {                        if(maps[op.x][op.y]==key[j])                        {                            /*if((op.temp&(1<<j))==0)       //      1001&0100==0 表示不存在钥匙j                            {                                op.temp=op.temp+(1<<j);      //      1001+0100=1101  将钥匙j加入01串中                            }*/                            op.temp = (op.temp|(1<<j)) ;      //     1001 | 0100 =1101 直接将钥匙j加入01串中                                                                  // 两种方法表示 再在钥匙的01串                            if(!dis[op.x][op.y][op.temp])                            {                                dis[op.x][op.y][op.temp]=1;                                q.push(op);                            }                        }                    }                }                else if(isupper(maps[op.x][op.y])&&maps[op.x][op.y]!='X')            //大写                {                    for(int j=0;j<4;j++)                    {                        if(maps[op.x][op.y]==men[j])                        {                            if(op.temp&(1<<j))                            {                                //op.temp=op.temp+(1<<j);                    //大写的时候不添加钥匙                                                                             //只判断是否有钥匙                                if(dis[op.x][op.y][op.temp]==0)                                {                                    dis[op.x][op.y][op.temp]=1;                                    q.push(op);                                }                            }                            break;                        }                    }                }                else                {                    if(dis[op.x][op.y][op.temp]==0)                    {                        dis[op.x][op.y][op.temp]=1;                        q.push(op);                    }                }            }        }    }    printf("The poor student is trapped!\n");}int main(){    while(~scanf("%d%d",&r,&c))    {        if(r==0&&c==0)            break;        for(int i=0;i<r;i++)        {            scanf("%s",&maps[i]);            for(int j=0;j<c;j++)            {                if(maps[i][j]=='*')                {                    startx=i;                    starty=j;                }            }        }        bfs(startx,starty);    }}

0 0
原创粉丝点击