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 Starship Enterprise, fell into Klingon’s trick and was held as prisoner on their mother planet Qo’noS.

The captain of Enterprise, James T. Kirk, had to fly to Qo’noS to rescue his deputy. Fortunately, he stole a map of the maze where Spock was put in exactly.

The maze is a rectangle, which has n rows vertically and m columns horizontally, in another words, that it is divided into n*m locations. An ordered pair (Row No., Column No.) represents a location in the maze. Kirk moves from current location to next costs
1 second. And he is able to move to next location if and only if:

Next location is adjacent to current Kirk’s location on up or down or left or right(4 directions)

Open door is passable, but locked door is not.

Kirk cannot pass a wall

There are p types of doors which are locked by default. A key is only capable of opening the same type of doors. Kirk has to get the key before opening corresponding doors, which wastes little time.

Initial location of Kirk was (1, 1) while Spock was on location of (n, m). Your task is to help Kirk find Spock as soon as possible.

Input

The input contains many test cases.

Each test case consists of several lines. Three integers are in the first line, which represent n, m and p respectively (1<= n, m <=50, 0<= p <=10).

Only one integer k is listed in the second line, means the sum number of gates and walls, (0<= k <=500).

There are 5 integers in the following k lines, represents xi1, yi1, xi2, yi2, gi; when gi >=1, represents there is a gate of type gi between location (xi1, yi1) and (xi2,
yi2); when gi = 0, represents there is a wall between location (xi1, yi1) and (xi2, yi2), ( | xi1 - xi2 | + | yi1 - yi2 |=1, 0<= gi <=p
)

Following line is an integer S, represent the total number of keys in maze. (0<= S <=50).

There are three integers in the following S lines, represents xi1, yi1 and qi respectively. That means the key type of qi locates on location (xi1, yi1), (1<= qi<=p).

Output

Output the possible minimal second that Kirk could reach Spock.

If there is no possible plan, output -1.

Sample Input

4 4 9
9
1 2 1 3 2
1 2 2 2 0
2 1 2 2 0
2 1 3 1 0
2 3 3 3 0
2 4 3 4 1
3 2 3 3 0
3 3 4 3 0
4 3 4 4 0
2
2 1 2
4 2 1

Sample Output

14

大致题意:

有一个N*M的地图,图里面有p种门。

下一行输入一个k,代表有k个门 (或墙)。下面k行给出它们所在的位置,样例(x1,x2,y1,y2,op)意义:前四个数值分别代表两个位置的坐标,当op为0时说明两位置之间是一堵墙,当op大于0时说明两位置之间是一扇门且op代表门的型号。

然后输入一个S,代表钥匙的数目,接下来S行每行三个数(x,y,op)代表位置(x,y)有 一把型号op的门 的钥匙。

问你能够从(1,1)出发到达(N,M),若可以输出最小步数,否则输出-1。当然每个点是可以无限走的。

自己就是脑残,忘了输出-1了。 WA了两次。。。

思路:简单BFS + 状态压缩。注意状态压缩处理二进制时,对门和钥匙的编号要减一处理(不要问我为什么)。

注意此题有坑处!!!

一:每个点可能有 很多把 相同型号或者不同型号的钥匙。

二:如果你想用vector存储每个位置的钥匙,我劝你还是放弃吧。

三:起点可能有钥匙,注意初始状态的处理。

说下代码中数组的意义:

1,三维数组vis[ x ][ y ][ State ]存储在位置(x,y)的State状态。

2,三维数组pos[ x ][ y ][ i ]存储位置(x,y)是否有第 i 种钥匙,若有为1,反之为0。

3,四维数组rec[x1][y1][x2][y2]记录两个位置间是否为墙 或 门 或 什么都没有,-1表示什么都没有,-2表示有墙,大于或等于0表示有门 且 相应数值 为门的型号。

4,Map[ x ][ y ] 为 0 时表示位置(x,y)没有钥匙,大于0时说明有钥匙。

有了以上数组就是裸BFS了,详细看我代码。

提醒:可以选用优先队列进行优化,对于这道题优先队列比普通队列快了点。

AC代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define MAXN 51
using namespace std;
struct Node
{
    int x, y, step, key;
    friend bool operator < (Node a, Node b)
    {
        return a.step > b.step;
    }
};
int Map[MAXN][MAXN];
int pos[MAXN][MAXN][10];//存储该位置拥有的 钥匙种类
bool vis[MAXN][MAXN][1<<10];//最多10个门
int rec[MAXN][MAXN][MAXN][MAXN];// -2表示两点间有墙 大于或等于0表示两点间有门 -1表示什么都没有
int N, M, p, k;
void getMap()
{
    int x, y, x1, y1, x2, y2, op;
    memset(Map, 0, sizeof(Map));//0表示该位置什么都没有
    memset(rec, -1, sizeof(rec));//-1表示两个位置之间 什么都没有
    scanf("%d", &k);
    for(int i = 1; i <= k; i++)//k个地方有门 或者 有墙
    {
        scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &op);
        if(op == 0)//墙
            rec[x1][y1][x2][y2] = rec[x2][y2][x1][y1] = -2;
        else//门
            rec[x1][y1][x2][y2] = rec[x2][y2][x1][y1] = op-1;
    }
    memset(pos, 0, sizeof(pos));
    int S;//钥匙数目
    scanf("%d", &S);
    while(S--)
    {
        scanf("%d%d%d", &x, &y, &op);
        Map[x][y] = 1;
        if(!pos[x][y][op-1])//该钥匙 还没有
            pos[x][y][op-1] = 1;
    }
}
bool judge(Node a)//是否越界
{
    return a.x >= 1 && a.x <= N && a.y >= 1 && a.y <= M;
}
int wall_or_door(Node a, Node b)//判断两个点间是否有 墙 或有门 或什么都没有
{
    return rec[a.x][a.y][b.x][b.y];
}
void BFS(int x, int y)
{
    priority_queue<Node> Q;
    int move[4][2] = {0,1, 0,-1, 1,0, -1,0};
    memset(vis, false, sizeof(vis));
    Node now, next;
    now.x = x, now.y = y, now.step = 0, now.key = 0;
    if(Map[now.x][now.y])//起点可能有钥匙 这里是个坑
    {
        for(int i = 0; i < 10; i++)
        {
            if(pos[now.x][now.y][i])
                now.key |= (1 << i);
        }
    }
    Q.push(now);
    vis[now.x][now.y][now.key] = true;
    while(!Q.empty())
    {
        now = Q.top();
        Q.pop();
        if(now.x == N && now.y == M)//到达终点
        {
            printf("%d\n", now.step);
            return ;
        }
        for(int k = 0; k < 4; k++)
        {
            next.x = now.x + move[k][0];
            next.y = now.y + move[k][1];
            next.step = now.step + 1;
            int t = wall_or_door(now, next);
            if(judge(next) && t != -2)//不能越界且中间不能有墙
            {
                //分有门 和 没有门 来讨论
                if(t >= 0)//有门
                {
                    next.key = now.key;
                    //先看是否有钥匙 要保证能到达目标位置
                    if(next.key & (1 << t))//有钥匙
                    {
                        //判断目标位置是否有钥匙
                        if(Map[next.x][next.y])//有钥匙 收集钥匙
                        {
                            for(int i = 0; i < 10; i++)
                            {
                                if(pos[next.x][next.y][i])
                                    next.key |= (1 << i);
                            }
                        }
                        if(!vis[next.x][next.y][next.key])//判断该状态 是否出现过
                        {
                            vis[next.x][next.y][next.key] = true;
                            Q.push(next);
                        }
                    }
                    //没有钥匙这条路目前不能走
                }
                else//没有门
                {
                    next.key = now.key;
                    if(Map[next.x][next.y])//目标位置有钥匙
                    {
                        for(int i = 0; i < 10; i++)
                        {
                            if(pos[next.x][next.y][i])
                                next.key |= (1 << i);
                        }
                    }
                    if(!vis[next.x][next.y][next.key])
                    {
                        vis[next.x][next.y][next.key] = true;
                        Q.push(next);
                    }
                }
            }
        }
    }
    printf("-1\n");
}
int main()
{
    while(scanf("%d%d%d", &N, &M, &p) != EOF)
    {
        getMap();
        BFS(1, 1);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-09 22:11:38

hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】的相关文章

hdu 5094 Maze bfs+状态压缩

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

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,可以利用两条相邻蛇身坐标确定其相对方向(四个方向),两位二进制可以表示 这样 一个蛇头坐标+

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