HDU 5094 Maze(状态压缩+BFS)

Maze

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others)
Total Submission(s): 1729    Accepted Submission(s): 597


 

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

 

 

Source

2014上海全国邀请赛——题目重现(感谢上海大学提供题目)

题意:有一个迷宫,每个格子有四个方向,但这四个方向可能是门,可能是墙,也可能是通路,有些格子里有钥匙,每种钥匙只能开对应品种的门,求从(1,1)到(n,m)所花费的最少时间,格子里可能有多把钥匙。

思路:50*50的迷宫,深搜会超时,肯定用广搜,广搜记录状态时不仅要记录每格子的位置,还要记录这个格子有的钥匙,总共有10把钥匙,所以用到了状态压缩,用10位的二进制数来表示每一个格子有哪些钥匙,第i位为1代表这个格子有编号为i的钥匙。

#include <stdio.h>
#include <string.h>
#include <queue>

using namespace std;

struct node
{
    int step,x,y,state;
    //分别记录格子的坐标,步数,有哪些钥匙
    node(){}
    node(int _x,int _y,int _step,int _state)
    {
        x = _x,y = _y;
        step = _step,state = _state;
    }
};
int n,m,p;
int mp[51][51][51][51];//记录地图信息
int to[4][2] = {1,0,0,1,-1,0,0,-1};
int key[51][51];//每一个格子的钥匙有哪些
bool vis[51][51][1 << 10];//标记格子状态,避免重复访问

bool judge(int x,int y)
{
    if(x <= 0 || y <= 0 || x > n || y > m) {
        return 0;
    }
    return 1;
}
void BFS()
{
    struct node now,next;
    memset(vis,0,sizeof(vis));
    queue<node> q;
    q.push(node(1,1,0,key[1][1]));
    vis[1][1][key[1][1]] = 1;

    while(!q.empty()) {
        now = q.front();
        q.pop();

        for(int i = 0; i < 4; i++) {
            next.x = now.x + to[i][0];
            next.y = now.y + to[i][1];
            next.state = now.state | key[next.x][next.y];
            next.step = now.step + 1;
            if(!judge(next.x,next.y)) continue;
            //格子的状态是否被访问过
            if(vis[next.x][next.y][next.state]) continue;
            int ty = mp[now.x][now.y][next.x][next.y];
            //是否为墙
            if(ty == 0) continue;
            //是否有门的钥匙
            if(ty > 0 && !((next.state >> (ty - 1)) & 1)) continue;
            vis[next.x][next.y][next.state] = 1;
            if(next.x == n && next.y == m) {
                printf("%d\n",next.step);
                return;
            }
            q.push(next);
        }
    }
    printf("-1\n");
}
int main(void)
{
    int x1,y1,x2,y2,ty;
    int num;
    while(scanf("%d%d%d",&n,&m,&p) != EOF) {
        memset(mp,-1,sizeof(mp));
        memset(key,0,sizeof(key));
        scanf("%d",&num);
        while(num--) {
            scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&ty);
            mp[x1][y1][x2][y2] = ty;
            mp[x2][y2][x1][y1] = ty;
        }
        scanf("%d",&num);
        while(num--) {
            scanf("%d%d%d",&x1,&y1,&ty);
            key[x1][y1] |= (1 << (ty - 1));
        }
        BFS();
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值