hdu5094

来源:互联网 发布:浙江软件协会 编辑:程序博客网 时间:2024/06/08 05:48
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 991 2 1 3 21 2 2 2 02 1 2 2 02 1 3 1 02 3 3 3 02 4 3 4 13 2 3 3 03 3 4 3 04 3 4 4 022 1 24 2 1

Sample Output

14

题意:有一个N*M的迷宫,你要从(1,1)走到(N,M),给出一些相邻坐标点之间的信息,0表示墙,数字表示门,一种钥匙配一种门,给出钥匙所在地的信息,问至少走多少不才能到达目的地

状态压缩钥匙,进行bfs,注意一个点可能有多把钥匙

ac代码:

#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <math.h>#include <queue>using namespace std;bool vis[51][51][2500];struct node{    int x,y,key;    long long cost;    node ()    {        ;    }    node(int x_,int y_,int key_,int cost_)    {        x=x_;        y=y_;        key=key_;        cost=cost_;    }};int n,m,key_num;int c[51][51][51][51];int mp[55][55];int dx[4]= {1,0,-1,0};int dy[4]= {0,1,0,-1};bool check(node now,int x,int y){    if(c[now.x][now.y][x][y]==-1)        return true;    if(c[now.x][now.y][x][y]==0)        return false;    if(c[now.x][now.y][x][y]>0)    {        int key=c[now.x][now.y][x][y];            if( (now.key&(1<<key) ) )                return true;            else                return false;        }}long long bfs(){    queue<node>que;    que.push(node(1,1,(1<<mp[1][1]),0));    vis[1][1][0]=true;    while(!que.empty())    {        node now=que.front();        que.pop();        if(now.x==n&&now.y==m)            return now.cost;        int nx,ny;        for(int i=0; i<4; i++)        {            nx=dx[i]+now.x;            ny=dy[i]+now.y;            if(nx>=1&&nx<=n&&ny>=1&&ny<=m)            {                if(check(now,nx,ny)&&!vis[nx][ny][now.key])                {                   // cout<<"dd"<<endl;                    node temp=now;                    temp.x=nx;                    temp.y=ny;                    //cout<<"key= "<<now.key<<endl;                    if(mp[nx][ny])                        temp.key=(temp.key|(mp[nx][ny]));                  // cout<<"key= "<<temp.key<<endl;                    temp.cost++;                    que.push(temp);                    vis[nx][ny][temp.key]=true;                }            }        }    }    return -1;}int main(){    while(scanf("%d%d%d",&n,&m,&key_num)!=-1)    {        int k,kk;        int x1,y1,x2,y2,tp;        scanf("%d",&k);        memset(c,-1,sizeof(c));        for(int i=0; i<k; i++)        {            scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&tp);            c[x1][y1][x2][y2]=tp;            c[x2][y2][x1][y1]=tp;        }        memset(vis,0,sizeof(vis));        scanf("%d",&kk);        memset(mp,0,sizeof(mp));        for(int i=0; i<kk; i++)        {            scanf("%d%d%d",&x1,&y1,&tp);            mp[x1][y1]= (mp[x1][y1]|(1<<tp));        }        printf("%lld\n",bfs());    }    return 0;}


0 0
原创粉丝点击