hdu 5094 Maze 状压 bfs

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


 状压 bfs

#include<map>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

typedef long long ll;
const int inf = 0x3f3f3f3f;
const int M = 1e6 + 10;

int n,m;
int maps[53][53][53][53];//地图
bool vis[55][55][1<<11];
int keys[55][55];
int k1,k2;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
struct node
{
    int x,y,step,key;
};
bool check(int x,int y)
{
    if(x<1||x>n||y<1||y>m)
        return 0;
    else
        return 1;
}
int qq;
queue<node>q;
int bfs()
{
    while(q.size())
                q.pop();
    memset(vis,0,sizeof(vis));
    node k;
    k.x=1;k.y=1;k.step=0;k.key=keys[1][1];
    int x=k.x,y=k.y,step=k.step,key=k.key;
    //cout<<x<<" "<<y<<" "<<step<<" "<<key<<endl;
    q.push(k);
    vis[1][1][k.key]=1;
    while(q.size())
    {
        k=q.front();
        q.pop();
        x=k.x,y=k.y,step=k.step,key=k.key;
        //cout<<x<<" "<<y<<" "<<step<<" "<<key<<endl;
        if(x==n&&y==m){
            return step;
        }
        for(int i=0;i<4;i++)
        {
            int xx=x+dx[i],yy=y+dy[i];
            if(!check(xx,yy))
                continue;
            int ks=key|keys[xx][yy];//当前的钥匙
            int t=maps[xx][yy][x][y];
            int w=1<<(t-1);
            if((t==-1||(t!=0&&(ks&w)))&& !vis[xx][yy][ks])
            {
                vis[xx][yy][ks]=1;
                k.x=xx,k.y=yy,k.step=step+1,k.key=ks;
                q.push(k);
            }
        }
    }
    return -1;
}
int main()
{
    while(~scanf("%d%d%d",&n,&m,&qq))
    {
        int x,y,xx,yy,opt;
        scanf("%d",&k1);
        memset(keys,0,sizeof(keys));
        memset(maps,-1,sizeof(maps));
        for(int i=0;i<k1;i++)
        {
            scanf("%d%d%d%d%d",&x,&y,&xx,&yy,&opt);
            maps[x][y][xx][yy]=maps[xx][yy][x][y]=opt;//从(x,y)与(xx,yy)之间是opt
        }
        scanf("%d",&k2);
        for(int i=0;i<k2;i++)
        {
            scanf("%d%d%d",&x,&y,&opt);
            opt--;
            //cout<<x<<y<<opt<<endl;
            keys[x][y]|=(1<<opt);//(x,y)位置的钥匙种类
        }
        int ans=bfs();
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/liluoyu_1016/article/details/81316765
今日推荐