Hdu 5094 Maze(状压dp+bfs)

来源:互联网 发布:萤石云软件下载 编辑:程序博客网 时间:2024/06/05 07:19

题目链接

Maze

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


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 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
 

Source
2014上海全国邀请赛——题目重现(感谢上海大学提供题目)  

题解:用dp[i][j] 表示在第i个点,已有的钥匙的状态为j(二进制表示)的最小时间,然后bfs即可。
代码如下:
#include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>#include<vector>#include<queue>#include<map>#define nn 55using namespace std;int n,m,pp,k;int dir[4][2]={1,0,-1,0,0,1,0,-1};struct node{    int st,en,next;    int ty;}E[nn*nn*100];int p[nn*nn],num;vector<int>ys[nn*nn];void init(){    memset(p,-1,sizeof(p));    num=0;}void add(int st,int en,int ty){    E[num].en=en;    E[num].ty=ty;    E[num].next=p[st];    p[st]=num++;}map<pair<int,int>,int>ma;bool check(int x,int y){    if(x>=1&&x<=n&&y>=1&&y<=m)        return true;    return false;}int dp[nn*nn][(1<<10)+10];struct str{    int id,zt;    str(){}    str(int x,int y)    {        id=x,zt=y;    }};queue<str>que;int solve(){    while(que.size())        que.pop();    memset(dp,-1,sizeof(dp));    int i,zt,w,j;    zt=0;    for(i=0;i<(int)ys[1].size();i++)        zt=zt|(1<<ys[1][i]);    dp[1][zt]=0;    que.push(str(1,zt));    str sta;    while(que.size())    {        sta=que.front();        que.pop();        for(i=p[sta.id];i+1;i=E[i].next)        {            w=E[i].en;            if(E[i].ty==0||(sta.zt&(1<<(E[i].ty-1))))            {                zt=sta.zt;                for(j=0;j<(int)ys[w].size();j++)                    zt=zt|(1<<ys[w][j]);                if(dp[w][zt]==-1)                {                    dp[w][zt]=dp[sta.id][sta.zt]+1;                    if(w==(n-1)*m+m)                    {                        return dp[w][zt];                    }                    que.push(str(w,zt));                }            }        }    }    return -1;}int main(){    int i,e,j;    int x,y,xx,yy,g;    while(scanf("%d%d%d",&n,&m,&pp)!=EOF)    {        scanf("%d",&k);        ma.clear();        init();        int ix,fc;        while(k--)        {            scanf("%d%d%d%d%d",&x,&y,&xx,&yy,&g);            ix=(x-1)*m+y,fc=(xx-1)*m+yy;            if(g==0)            {                if(ix>fc)                    swap(ix,fc);                ma[make_pair(ix,fc)]=1;            }            else            {                add(ix,fc,g);                add(fc,ix,g);                if(ix>fc)                    swap(ix,fc);                ma[make_pair(ix,fc)]=1;            }        }        int dx,dy;        for(i=1;i<=n;i++)        {            for(j=1;j<=m;j++)            {                for(e=0;e<4;e++)                {                    dx=i+dir[e][0],dy=j+dir[e][1];                    if(check(dx,dy))                    {                        ix=(i-1)*m+j,fc=(dx-1)*m+dy;                        if(ix>fc)                            swap(ix,fc);                        if(ma.count(make_pair(ix,fc))==0)                            add((i-1)*m+j,(dx-1)*m+dy,0);                    }                }            }        }        for(i=1;i<=(n-1)*m+m;i++)            ys[i].clear();        scanf("%d",&ix);        while(ix--)        {            scanf("%d%d%d",&x,&y,&g);            g--;            ys[(x-1)*m+y].push_back(g);        }        printf("%d\n",solve());    }    return 0;}






0 0
原创粉丝点击