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

hdu5094Maze bfs+状态压缩

时间:2015-03-28 08:57:48      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

//开一个四维数组记录墙和门的情况
//开一个三维数组标记在该位置时有哪些钥匙
//钥匙的记录用状态压缩
//注意在同一个位置可以有多把钥匙,在这卡了一个晚上。。。。。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 60 ;
int vis[maxn][maxn][1<<11] ;
int wall[maxn][maxn][maxn][maxn] ;
int maze[maxn][maxn] ;
struct node
{
    int x , y;
    int step ;
    int state ;
};
int dx[4] = {-1,0,0,1} ;
int dy[4] = {0,1,-1,0} ;
int n ,m,p;
int bfs()
{
    queue<struct node> que ;
    memset(vis,0,sizeof(vis));
    struct node first = {1,1,0,0};
    vis[1][1][0] = 1;
    que.push(first) ;
    while(que.size())
    {
        struct node now = que.front() ;
        que.pop() ;
        if(now.x == n && now.y == m)
            return now.step ;
        for(int i = 0;i < 4;i++)
        {
            struct node next ;
            next.x = now.x +dx[i];
            next.y = now.y +dy[i];
            if(next.x<1 || next.x>n || next.y>m || next.y<1 || wall[now.x][now.y][next.x][next.y] == 0)
            continue ;
            if(wall[now.x][now.y][next.x][next.y] >= 1)
                if(!(now.state & (1<<(wall[now.x][now.y][next.x][next.y]))))
                continue;
            next.state = now.state ;
            if(maze[next.x][next.y] > 0)
                next.state |= maze[next.x][next.y];
            next.step = now.step + 1;
            if(vis[next.x][next.y][next.state])
            continue ;
            vis[next.x][next.y][next.state] = 1;
            que.push(next) ;


        }
    }
    return -1;
}
int main()
{
   //freopen("in.txt","r",stdin);
  //  freopen("out.txt","w",stdout);
    while(scanf("%d%d%d", &n , &m , &p) != EOF)
    {
       int k ;
       scanf("%d", &k);
       int xi1 , yi1, xi2 , yi2 , gi;
       memset(wall,-1,sizeof(wall)) ;
       memset(maze,0,sizeof(maze)) ;
       for(int i = 1;i <= k ;i++)
       {
           scanf("%d%d%d%d%d",&xi1,&yi1,&xi2,&yi2,&gi);
           wall[xi1][yi1][xi2][yi2] = gi ;
           wall[xi2][yi2][xi1][yi1] = gi;
       }
       int s;
       scanf("%d", &s) ;
       int xi,yi;
       for(int i = 1;i <= s;i++)
       {
           scanf("%d%d%d",&xi,&yi,&gi) ;
           maze[xi][yi] |= (1<<(gi)) ;
       }
       printf("%d\n",bfs()) ;
    }
}













































































































hdu5094Maze bfs+状态压缩

标签:

原文地址:http://blog.csdn.net/cq_pf/article/details/44683201

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