Maze HDU - 5094(BFS)

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

本质上在同一点不同钥匙的集合构成了多个不同状态,这和我之前有道升降机的题目很像,那就是 xy x , y , 钥 匙 集 合 来构成一个点了,然后做一个bfs就可以了,其实不难的,但是细节很多的。
代码:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#define maxx 55
using namespace std;

int a[maxx][maxx][maxx][maxx];
int n,m,k,p,s;
int mapp[maxx][maxx];
struct node
{
    int x,y;
    int step;
    int state;
    node(){}
    node(int _x,int _y,int _step,int _state):x(_x),y(_y),step(_step),state(_state){}
};
bool vis[maxx][maxx][1<<11];
int b[][2]={{0,1},{0,-1},{1,0},{-1,0}};
int work()
{
    queue<node> que;
    que.push(node(1,1,0,mapp[1][1]));
    vis[1][1][mapp[1][1]]=1;
    int ans;
    bool sign=true;
    while(que.size()&&sign)
    {
        node temp=que.front();
        que.pop();
        for(int i=0;i<4;i++)
        {
            int xt=temp.x+b[i][0];
            int yt=temp.y+b[i][1];
            if(xt<1||xt>n||yt<1||yt>m)continue;//越界判断
            int h=a[temp.x][temp.y][xt][yt];
            if(!h)continue;//看有没有墙
            if(h!=-1&&(temp.state&(1<<(h-1)))==0) continue;//对于这个门我有没有钥匙
            int newS=temp.state|mapp[xt][yt];//拿到新钥匙(如果有)
            if(vis[xt][yt][newS])continue;//判断有没有之前访问过
            vis[xt][yt][newS]=true;
            if(xt==n&&yt==m)
            {
                sign=false;
                ans=temp.step+1;
                break;
            }//是否到达终点
            que.push(node(xt,yt,temp.step+1,newS));//入队

        }
    }
    if(sign)return -1;
    else return ans;
}
void init()
{
    memset(a,-1,sizeof(a));
    memset(vis,false,sizeof(vis));
    memset(mapp,0,sizeof(mapp));
}
int main()
{
    int x1,y1,x2,y2,g;
    while(scanf("%d%d%d",&n,&m,&p)==3)
    {
        init();
        cin>>k;
        for(int i=0;i<k;i++)
        {
            scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&g);
            a[x1][y1][x2][y2]=g;
            a[x2][y2][x1][y1]=g;
        }
        cin>>s;
        for(int i=0;i<s;i++)
        {
            scanf("%d%d%d",&x1,&y1,&g);
            mapp[x1][y1]|=1<<(g-1);
        }
        cout<<work()<<endl;
    }

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值