码迷,mamicode.com
首页 > 其他好文 > 详细

HDU1885 Key Task(BFS+状态压缩)

时间:2016-05-13 00:50:33      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

HDU1429大致一样。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int MAX=100+10;
struct point
{
    int x,y,step,state;
    point(int x=0,int y=0,int step=0,int state=0):x(x),y(y),step(step),state(state){};
};
char g[MAX][MAX];
int vis[MAX][MAX][20];
int dirx[4]={0,1,0,-1};
int diry[4]={1,0,-1,0};
int n,m;
int sx,sy;
bool judgein(int x,int y)
{
    return 0<=x&&x<n&&0<=y&&y<m;
}
int tran(char c)
{
    if(c=='B'||c=='b') return 0;
    if(c=='G'||c=='g') return 1;
    if(c=='Y'||c=='y') return 2;
    if(c=='R'||c=='r') return 3;
}
int bfs()
{
    memset(vis,0,sizeof(vis));
    queue<point> que;
    vis[sx][sy][0]=1;
    que.push(point(sx,sy,0,0));
    while(!que.empty())
    {
        point top=que.front();
        que.pop();
        int x=top.x;
        int y=top.y;
        int step=top.step;
        int state=top.state;
        if(g[x][y]=='X')
        {
            return step;
        }
        for(int i=0;i<4;i++)
        {
            int nx=x+dirx[i];
            int ny=y+diry[i];
            int nstep=step+1;
            int nstate=state;
            if(judgein(nx,ny)&&g[nx][ny]!='#'&&!vis[nx][ny][nstate])
            {
                if(g[nx][ny]=='.'||g[nx][ny]=='X')
                {
                    vis[nx][ny][nstate]=1;
                    que.push(point(nx,ny,nstep,nstate));
                }
                else if(g[nx][ny]>='A'&&g[nx][ny]<='Z')
                {
                    int key=nstate&(1<<(tran(g[nx][ny])));
                    if(key)
                    {
                        vis[nx][ny][nstate]=1;
                        que.push(point(nx,ny,nstep,nstate));
                    }
                }
                else if(g[nx][ny]>='a'&&g[nx][ny]<='z')
                {
                    nstate=nstate|(1<<(tran(g[nx][ny])));
                    vis[nx][ny][nstate]=1;
                    que.push(point(nx,ny,nstep,nstate));
                }
            }
        }
    }
    return 0;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    while(scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0) break;
        getchar();
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                scanf("%c",&g[i][j]);
                if(g[i][j]=='*')
                {
                    sx=i;
                    sy=j;
                    g[i][j]='.';
                }
            }
            getchar();
        }
/*        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                printf("%c",g[i][j]);
            }
            printf("\n");
        }
        printf("%d %d\n",sx,sy);*/
        int ans=bfs();
        if(ans)
        {
            printf("Escape possible in %d steps.\n",ans);
        }
        else 
        {
            printf("The poor student is trapped!\n");
        }
    }
    return 0;
}


HDU1885 Key Task(BFS+状态压缩)

标签:

原文地址:http://blog.csdn.net/u011523762/article/details/51345817

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!