HDU1885 Key Task(BFS+状态压缩)

HDU1429大致一样。

代码:

#include
#include
#include
#include
#include
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 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


你可能感兴趣的:(ACM---图论)