HDU5094【BFS+状态压缩】

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
const int NO=56;
const int INF=1000000000;
struct X
{
    int x,y;
    int key;
}dir[]={{-1,0}/*&iexcl;ü*/,{0,1}/*&iexcl;ú*/,{1,0}/*&iexcl;&yacute;*/,{0,-1}/*&iexcl;&ucirc;*/},a,b;
int n,m,q;
int MAP[NO][NO][NO][NO];
int key[NO][NO];
int step[NO][NO][1024];
void reset_step()
{
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            for(int k=0;k<1<<q;k++)
                step[i][j][k]=INF;
}
int bfs()
{
    reset_step();
    queue<X> t;
    a.x=a.y=1;
    a.key=0;
    step[a.x][a.y][a.key]=0;
    t.push(a);
    while(!t.empty())
    {
        a=t.front();
        t.pop();
        if(a.x==n&&a.y==m)
            return step[a.x][a.y][a.key];
        for(int i=0;i<4;i++)
        {
            b.x=a.x+dir[i].x;
            b.y=a.y+dir[i].y;
            if((MAP[a.x][a.y][b.x][b.y]&a.key)==MAP[a.x][a.y][b.x][b.y])
            {
                b.key=a.key|key[b.x][b.y];
                if(step[b.x][b.y][b.key]==INF)
                {
                    step[b.x][b.y][b.key]=step[a.x][a.y][a.key]+1;
                    t.push(b);
                }
            }
        }
    }
    return -1;
}
int main()
{
    while(scanf("%d%d%d",&n,&m,&q)==3)
    {
        for(int i=1;i<=n;i++)
        {
            MAP[i][0][i][1]=MAP[i][1][i][0]=1<<q;
            MAP[i][m][i][m+1]=MAP[i][m+1][i][m]=1<<q;
        }
        for(int j=1;j<=m;j++)
        {
            MAP[0][j][1][j]=MAP[1][j][0][j]=1<<q;
            MAP[n][j][n+1][j]=MAP[n+1][j][n][j]=1<<q;
        }
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                key[i][j]=0;
                for(int x=1;x<=n;x++)
                    for(int y=1;y<=m;y++)
                        MAP[i][j][x][y]=MAP[x][y][i][j]=0;
            }
        int k;
        scanf("%d",&k);
        int a,b,c,d,e;
        while(k--)
        {
            scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
            if(e==0)
                e=1<<q;
            else
                e=1<<(e-1);
            MAP[a][b][c][d]=MAP[c][d][a][b]=e;
        }
        scanf("%d",&k);
        while(k--)
        {
            scanf("%d%d%d",&a,&b,&c);
            key[a][b]|=1<<(c-1);
        }
        printf("%d\n",bfs());
    }
    return 0;
}

时间: 2024-08-07 15:56:48

HDU5094【BFS+状态压缩】的相关文章

hdu--1429--胜利大逃亡(续) (bfs+状态压缩)

胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8560    Accepted Submission(s): 3071 Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)-- 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带

ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))

求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其到(1,1)的最短路长 题解:开始做的时候用BFS暴力做了一次,结果RE了,后来看了其他的题解和discuss才转向状态压缩.也看到有人用A*做出来了. 现在简要介绍一下状态压缩的思路: 由于蛇身最长只有8,可以利用两条相邻蛇身坐标确定其相对方向(四个方向),两位二进制可以表示 这样 一个蛇头坐标+

hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】

Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Submission(s): 901    Accepted Submission(s): 314 Problem Description This story happened on the background of Star Trek. Spock, the deputy captain of St

hdu2209翻纸牌游戏(bfs+状态压缩)

Problem Description 有一种纸牌游戏,很有意思,给你N张纸牌,一字排开,纸牌有正反两面,开始的纸牌可能是一种乱的状态(有些朝正,有些朝反),现在你需要整理这些纸牌.但是麻烦的是,每当你翻一张纸牌(由正翻到反,或者有反翻到正)时,他左右两张纸牌(最左边和最右边的纸牌,只会影响附近一张)也必须跟着翻动,现在给你一个乱的状态,问你能否把他们整理好,使得每张纸牌都正面朝上,如果可以,最少需要多少次操作. Input 有多个case,每个case输入一行01符号串(长度不超过20),1表

poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)

Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it's black or white side up. Each round you f

hdu 4856 Tunnels(bfs+状态压缩)

题目链接:hdu 4856 Tunnels 题目大意:给定一张图,图上有M个管道,管道给定入口和出口,单向,现在有人想要体验下这M个管道,问最短需要移动的距离,起点未定. 解题思路:首先用bfs处理出两两管道之间移动的距离,然后后用状态压缩求出最短代价,dp[i][j],i表示的已经走过的管道,j是当前所在的管道. #include <cstdio> #include <cstring> #include <queue> #include <algorithm&g

HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)

题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP 用最短路预处理出状态的转移.能够优化非常多 AC代码: #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <queue> us

hdu1254推箱子 (BFS+BFS+状态压缩)

推箱子 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5089 Accepted Submission(s): 1421 Problem Description 推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不能拉箱子,因此如果箱

HDU 3247 Resource Archiver (AC自动机 + BFS + 状态压缩DP)

题目链接:Resource Archiver 解析:n个正常的串,m个病毒串,问包含所有正常串(可重叠)且不包含任何病毒串的字符串的最小长度为多少. AC自动机 + bfs + 状态压缩DP 用最短路预处理出状态的转移.可以优化很多 AC代码: #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <queue> using n

zoj3675 BFS+状态压缩

#include <stdio.h> #include <string.h> #include <queue> using namespace std; int n; int vis[10000000]; int mode1,mode2; struct node { int step,status; }; void print(int x) { int tmp=x%2; if (!(x==0 || x==1)) print(x>>1); printf(&qu