HDU 5094 Maze (状压的普通迷宫)

加一个维度,钥匙的状态,位压一下。n很小,钥匙也只有10个,普通的bfs就好了。

当时写的时候很傻,忘了数组初始化。以后坚决不犯这种低级错误。

#include<cstdio>

#include<queue>

#include<cstring>

using namespace std;

const int maxn = 50+1;

int g[maxn][maxn][4];

int k[maxn][maxn];

int dx[] = {1,-1,0,0};

int dy[] = {0,0,1,-1};

int dir[5] = { 3,1,-1,0,2 };



struct node

{

    int x,y,step;

    int s;

    node(int X = 0, int Y = 0,int S = 0,int Step = 0) {

        x = X; y =Y; s = S; step = Step;

    }

};

int n,m,p;

bool vis[maxn][maxn][1024+10];

queue<node> q;

int tx,ty;



int bfs()

{

    memset(vis,0,sizeof(vis));

    while(q.size()) q.pop();

    node u(1,1,k[1][1],0);

    q.push(u);

    while(q.size()){

        u = q.front();q.pop();

        if(u.x == tx && u.y == ty) {return u.step;}

        for(int i = 0; i < 4; i++){

            if( g[u.x][u.y][i] == -1 || u.s&(1<<g[u.x][u.y][i])){

                int nx = u.x+dx[i], ny = u.y+dy[i], ns = u.s|k[nx][ny];

                if(1<=nx&&nx<=n&&1<=ny&&ny<=m&&!vis[nx][ny][ns>>1]){

                    vis[nx][ny][ns>>1] = 1;

                    q.push(node(nx,ny,ns,u.step+1));//+(g[u.x][u.y][i]!=-1)

                }

            }

        }

    }

    return -1;

}





int main()

{

    //freopen("E_in.txt","r",stdin);

    //freopen("E_out.txt","w",stdout);

    while(scanf("%d%d%d",&n,&m,&p) == 3){

        int K;

        scanf("%d",&K);

        memset(g,-1,sizeof(g));

        for(int i = 0; i < K; i++){

            int x1,x2,y1,y2,tp;

            scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&tp);

            int Dir = dir[((y2-y1+1)<<1)+x2-x1];

            g[x1][y1][Dir] = tp;

            g[x2][y2][Dir^1] = tp;

        }

        int S;

        memset(k,0,sizeof(k));

        scanf("%d",&S);

        for(int i = 0; i < S; i++){

            int x0,y0,tp;

            scanf("%d%d%d",&x0,&y0,&tp);

            if(tp)

            k[x0][y0] |= 1<<tp;

        }

        tx = n; ty = m;

        int ans = bfs();

        printf("%d\n",ans);

    }

    return 0;

}

 

你可能感兴趣的:(HDU)