Maze HDU - 5094

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
#include<bits/stdc++.h>
using namespace std;

const int maxn=55;
int n,m,p;
int mp[maxn][maxn][5];
/*
这道题是真的坑,难点主要是
1.建图  表达出点与边,需要三维建图,一个点需要表达出来5个信息,当前点存的值,四个方向上的情况
2.状压  p大约是10,所以用位操作来做就好了
3.坑点  有可能(1,1)是钥匙;有可能一个点有好几把钥匙(真是小可爱) 
*/

struct node{
    int x,y;
    int tag;
    int w;
    node(int _x=1,int _y=1,int _w=0,int _tag=0){
        x=_x,y=_y,w=_w,tag=_tag;
    }
}now,nxt;

int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
int vis[maxn][maxn][5050];
int get_ans(){
    queue<node> q;
    now=node(1,1,0,0);

    int tmp=0;
    if(mp[1][1][0]!=-1){
        tmp|=mp[1][1][0];
    }
    q.push(node(1,1,0,tmp));

    while(!q.empty()){
        now=q.front();
        q.pop();
        if(now.x==n&&now.y==m){
            return now.w;
        }
        for(int i=0;i<4;i++){
            int nx=now.x+dir[i][0],ny=now.y+dir[i][1];
            if(nx<1||nx>n||ny<1||ny>m)continue;
            if(!mp[now.x][now.y][i+1])continue;
            if(mp[now.x][now.y][i+1]!=-1){
                int key=(1<<(mp[now.x][now.y][i+1]));
                if(!(key&now.tag))continue;
            }
            if(vis[nx][ny][now.tag])continue;
            int tmp=now.tag;
            if(mp[nx][ny][0]!=-1){
                tmp|=mp[nx][ny][0];
            }
            vis[nx][ny][tmp]=1;
            q.push(node(nx,ny,now.w+1,tmp));
        }
    }
    return -1;
}

int main(){
    while(scanf("%d %d %d",&n,&m,&p)==3){
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                mp[i][j][0]=0;
                for(int k=1;k<5;k++){
                    mp[i][j][k]=-1;
                }
            }
        }

        int t1,t2;
        scanf("%d",&t1);
        for(int i=1;i<=t1;i++){
            int x1,y1,x2,y2,t;
            scanf("%d %d %d %d %d",&x1,&y1,&x2,&y2,&t);
            if(x1==x2+1&&y2==y1){
                mp[x1][y1][1]=mp[x2][y2][3]=t;
            }
            else if(x1==x2&&y2==y1+1){
                mp[x1][y1][2]=mp[x2][y2][4]=t;
            }
            else if(x1==x2-1&&y1==y2){
                mp[x1][y1][3]=mp[x2][y2][1]=t;
            }
            else{
                mp[x1][y2][4]=mp[x2][y2][2]=t;
            }
        }
        scanf("%d",&t2);
        for(int i=1;i<=t2;i++){
            int x,y,t;
            scanf("%d %d %d",&x,&y,&t);
            mp[x][y][0]|=(1<<t);
        } 

        int ans=get_ans();
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36424540/article/details/81069909