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

[HDU]5094Maze(状态压缩BFS)

时间:2014-11-08 23:39:18      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   sp   for   on   2014   log   ad   

状态压缩的题,第一次WA了,怎么改都不对,交了十几遍,之后重新写了一个,1A了

#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 51;
const int dir[4][2] = {{0,1},{-1,0},{0,-1},{1,0}};
int n,m,t;
int door[maxn][maxn][4];
int key[maxn][maxn];
int vis[maxn][maxn][(1 << 11)];
struct St{
    int x,y,key,step;
}now;
void init(){
    memset(key,0,sizeof(key));
    memset(door,-1,sizeof(door));//-1代表可以任意的通过
    memset(vis,0,sizeof(vis));
}
void add_door(int x1,int y1,int x2,int y2,int op){
    if(x1 == x2){
        if(y1 > y2){
            door[x1][y1][2] = (1 << op);
            door[x2][y2][0] = (1 << op);
        }
        else{
            door[x1][y1][0] = (1 << op);
            door[x2][y2][2] = (1 << op);
        }
    }
    else{
        if(x1 > x2){
            door[x1][y1][1] = (1 << op);
            door[x2][y2][3] = (1 << op);
        }
        else{
            door[x1][y1][3] = (1 << op);
            door[x2][y2][1] = (1 << op);
        }
    }
}
bool judge(int d){
    if(door[now.x][now.y][d] == -1) return true;    //可以直接通过
    if(door[now.x][now.y][d] ==  1) return false;   //是一面墙
    int op = door[now.x][now.y][d]&now.key;
    if(!op) return false;
    return true;
}
void dfs(){
    now.x = 1; now.y = 1; now.key = key[1][1]; now.step = 0;
    vis[now.x][now.y][now.key] = 1;
    queue<St>q;
    while(!q.empty()) q.pop();
    q.push(now);
    while(!q.empty()){
        now = q.front(); q.pop();
        //printf("%d %d\n",now.x,now.y);;
        if(now.x == n && now.y == m){
            printf("%d\n",now.step);
            return;
        }
        for(int d = 0; d < 4; d++){
            int x = now.x + dir[d][0];
            int y = now.y + dir[d][1];
            if(x >= 1 && x <= n && y >= 1 && y <= m){
                if(!judge(d)) continue;
                int c_key = now.key|key[x][y];
                if(!vis[x][y][c_key]){
                    St temp; temp.x = x; temp.y = y; temp.key = c_key;
                    temp.step = now.step + 1;
                    q.push(temp);
                    vis[x][y][c_key] = 1;
                }
            }
        }
    }
    printf("-1\n");
}
int main(){
    while(scanf("%d%d%d",&n,&m,&t) != EOF){
        int a,b;
        init();
        scanf("%d",&a);
        for(int i = 0; i < a; i++){
            int x1,y1,x2,y2,op;
            scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&op);
            add_door(x1,y1,x2,y2,op);
        }
        scanf("%d",&b);
        for(int i = 0; i < b; i++){
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            key[x][y] |= (1 << z); //一个坐标的钥匙,状态压缩
        }
        dfs();
    }
    return 0;
}

[HDU]5094Maze(状态压缩BFS)

标签:style   blog   io   sp   for   on   2014   log   ad   

原文地址:http://blog.csdn.net/u013451221/article/details/40931661

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