HDOJ 题目5094 Maze(BFS+状压)

Maze

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


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 x i1, y i1, x i2, y i2, g i; when g i >=1, represents there is a gate of type gi between location (x i1, y i1) and (x i2, y i2); when g i = 0, represents there is a wall between location (x i1, y i1) and (x i2, y i2), ( | x i1 - x i2 | + | y i1 - y i2 |=1, 0<= g i <=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 x i1, y i1 and q i respectively. That means the key type of q i locates on location (x i1, y i1), (1<= q i<=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
 

Recommend
hujie   |   We have carefully selected several similar problems for you:   5379  5378  5377  5376  5375 
题目大意:从1 1走到n m,第一行输入n m p,p表示p个要是,在输入一个k,后边k行,每行两个坐标,一个g,g是0说明两个坐标之间有墙,否则有g号门
再输入个s,后边s行,每行一个左边,一个q,表示q号钥匙在这个坐标上
这个题好坑啊,,,比赛错了7次,,起点可能是钥匙,,,而且每个点可能有多把钥匙,,,
ac代码
Problem : 5094 ( Maze )     Judge Status : Accepted
RunId : 14490146    Language : C++    Author : lwj1994
Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta
#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
using namespace std;
int map[55][55][55][55],key[55][55][55],keynum[55][55];
int vis[1<<11][55][55],v[55][55][55];
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
int n,m,q;
struct s
{
    int x,y,step,cnt;
    friend bool operator < (s a,s b)
    {
        return a.step>b.step;
    }
}a,temp;
int jud(struct s a,struct s b)
{
    if(b.x<=0||b.x>n||b.y<=0||b.y>m)
        return 0;
    if(map[a.x][a.y][b.x][b.y]==0)
        return 0;
    //if(vis[b.cnt][b.x][b.y])
    //    return 0;
    return 1;
}
int bfs()
{
    a.x=1;
    a.y=1;
    a.step=0;
    a.cnt=0;
    int i,j;
    priority_queue<struct s>q;
    memset(vis,0,sizeof(vis));
    if(key[1][1][0])
    {
        for(i=0;i<keynum[1][1];i++)
        {
            int cc=1<<key[1][1][i];
            a.cnt=a.cnt|cc;
        }
    }
    q.push(a);
    vis[a.cnt][1][1]=1;
    while(!q.empty())
    {
        a=q.top();
        q.pop();
        for(i=0;i<4;i++)
        {
            temp.x=a.x+dx[i];
            temp.y=a.y+dy[i];
            temp.step=a.step+1;
            temp.cnt=a.cnt;
            if(!jud(a,temp))
                continue;
            if(map[a.x][a.y][temp.x][temp.y]>0)
            {
                int cc=temp.cnt;
                if(((cc>>map[a.x][a.y][temp.x][temp.y])&1)==0)
                {
                    continue;
                }
            }
            if(key[temp.x][temp.y][0])
            {
                for(j=0;j<keynum[temp.x][temp.y];j++)
                {
                    int cc=1<<(key[temp.x][temp.y][j]);
                    temp.cnt=temp.cnt|cc;
                }
            }
            if(!vis[temp.cnt][temp.x][temp.y])
            {
                if(temp.x==n&&temp.y==m)
                    return temp.step;
                vis[temp.cnt][temp.x][temp.y]=1;
                q.push(temp);
            }
        }
    }
    return -1;
}
int main()
{
    while(scanf("%d%d%d",&n,&m,&q)!=EOF)
    {
        memset(map,-1,sizeof(map));
        int i;
        int nm;
        scanf("%d",&nm);
        for(i=0;i<nm;i++)
        {
            int x1,x2,y1,y2,k;
            scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&k);
            map[x1][y1][x2][y2]=k;
            map[x2][y2][x1][y1]=k;
        }
        int num;
        scanf("%d",&num);
        memset(key,0,sizeof(key));
        memset(keynum,0,sizeof(keynum));
        memset(v,0,sizeof(v));
        while(num--)
        {
            int x,y,k;
            scanf("%d%d%d",&x,&y,&k);
            if(v[x][y][k])
                continue;
            v[x][y][k]=1;
            key[x][y][keynum[x][y]++]=k;
        }
        if(n==1&&m==1)
        {
            printf("0\n");
            continue;
        }
        printf("%d\n",bfs());
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值