hdu 5025 Saving Tang Monk(dp+最短路)

来源:互联网 发布:ubuntu怎么压缩文件夹 编辑:程序博客网 时间:2024/05/16 07:16

题目链接

Saving Tang Monk

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 572    Accepted Submission(s): 214


Problem Description
《Journey to the West》(also 《Monkey》) is one of the Four Great Classical Novels of Chinese literature. It was written by Wu Cheng'en during the Ming Dynasty. In this novel, Monkey King Sun Wukong, pig Zhu Bajie and Sha Wujing, escorted Tang Monk to India to get sacred Buddhism texts.

During the journey, Tang Monk was often captured by demons. Most of demons wanted to eat Tang Monk to achieve immortality, but some female demons just wanted to marry him because he was handsome. So, fighting demons and saving Monk Tang is the major job for Sun Wukong to do.

Once, Tang Monk was captured by the demon White Bones. White Bones lived in a palace and she cuffed Tang Monk in a room. Sun Wukong managed to get into the palace. But to rescue Tang Monk, Sun Wukong might need to get some keys and kill some snakes in his way.

The palace can be described as a matrix of characters. Each character stands for a room. In the matrix, 'K' represents the original position of Sun Wukong, 'T' represents the location of Tang Monk and 'S' stands for a room with a snake in it. Please note that there are only one 'K' and one 'T', and at most five snakes in the palace. And, '.' means a clear room as well '#' means a deadly room which Sun Wukong couldn't get in.

There may be some keys of different kinds scattered in the rooms, but there is at most one key in one room. There are at most 9 kinds of keys. A room with a key in it is represented by a digit(from '1' to '9'). For example, '1' means a room with a first kind key, '2' means a room with a second kind key, '3' means a room with a third kind key... etc. To save Tang Monk, Sun Wukong must get ALL kinds of keys(in other words, at least one key for each kind).

For each step, Sun Wukong could move to the adjacent rooms(except deadly rooms) in 4 directions(north, west, south and east), and each step took him one minute. If he entered a room in which a living snake stayed, he must kill the snake. Killing a snake also took one minute. If Sun Wukong entered a room where there is a key of kind N, Sun would get that key if and only if he had already got keys of kind 1,kind 2 ... and kind N-1. In other words, Sun Wukong must get a key of kind N before he could get a key of kind N+1 (N>=1). If Sun Wukong got all keys he needed and entered the room in which Tang Monk was cuffed, the rescue mission is completed. If Sun Wukong didn't get enough keys, he still could pass through Tang Monk's room. Since Sun Wukong was a impatient monkey, he wanted to save Tang Monk as quickly as possible. Please figure out the minimum time Sun Wukong needed to rescue Tang Monk.
 

Input
There are several test cases.

For each case, the first line includes two integers N and M(0 < N <= 100, 0<=M<=9), meaning that the palace is a N×N matrix and Sun Wukong needed M kinds of keys(kind 1, kind 2, ... kind M).

Then the N × N matrix follows.

The input ends with N = 0 and M = 0.
 

Output
For each test case, print the minimum time (in minutes) Sun Wukong needed to save Tang Monk. If it's impossible for Sun Wukong to complete the mission, print "impossible"(no quotes).
 

Sample Input
3 1K.S##11#T3 1K#T.S#1#.3 2K#T.S.21.0 0
 

Sample Output
5impossible8
 

Source
2014 ACM/ICPC Asia Regional Guangzhou Online 

题意:N*N的格子,孙悟空要去救唐僧,孙悟空在K,唐僧在T。孙悟空要救唐僧需要找齐M把钥匙,编号为1,2....M,而且必须按编号从小到大的顺序收集,除了’#‘以外的点都可以走。而且有些点有妖怪S,孙悟空第一次走到该点要花1的时间打死妖怪。从一个点可以走到相邻的四个点,花费时间1。求孙悟空救出唐僧的最短时间。

题解:因为妖怪的数量最多5个,所以我们可以用dp[ i ][ j ][ k ][ g ]表示在点(i,j),收集了前K把钥匙,已经打死了的妖怪的状态为g(用5位二进制表示妖怪的状态),的状态下的最短时间。在图上怎么走就怎么转移,可以用spfa的方式实现。也可以把一个状态看成一个点,最后就是求一个最短路,用spfa即可。代码如下:
//#pragma comment(linker, "/STACK:102400000,102400000")#include<stdio.h>#include<queue>#include<iostream>#include<algorithm>#include<string.h>#include<string>#include<math.h>#include<stack>#define nn 110#define mod 1000#define inff 0x3ffffffftypedef __int64 LL;using namespace std;int n,m;int dir[4][2]={0,-1,-1,0,0,1,1,0};char tu[nn][nn];int dp[nn][nn][15][35];bool inque[nn][nn][15][35];int kx,ky,tx,ty;int se[nn][nn];int she;int ans;struct node{    int x,y,ys,yg;}sta,tem;queue<node>que;bool check(int x,int y){    if(x<0||x>=n||y<0||y>=n)        return false;    if(tu[x][y]=='#')        return false;    return true;}void solve(){    int i,j,k,g,dx,dy;    for(i=0;i<n;i++)    {        for(j=0;j<n;j++)        {            for(k=0;k<=m;k++)            {                for(g=0;g<(1<<she);g++)                {                    dp[i][j][k][g]=inff;                    inque[i][j][k][g]=false;                }            }        }    }    dp[kx][ky][0][0]=0;    tem.x=kx,tem.y=ky,tem.yg=tem.ys=0;    que.push(tem);    int x,y,ys,yg;    int fei,ix;    while(que.size())    {        sta=que.front();        que.pop();        //cout<<sta.x<<" "<<sta.y<<" "<<sta.ys<<" "<<sta.yg<<endl;        inque[sta.x][sta.y][sta.ys][sta.yg]=false;        if(tu[sta.x][sta.y]=='T'&&sta.ys==m)        {            ans=min(ans,dp[sta.x][sta.y][sta.ys][sta.yg]);            continue;        }        for(i=0;i<4;i++)        {            dx=dir[i][0]+sta.x,dy=dir[i][1]+sta.y;            if(check(dx,dy))            {                x=dx,y=dy;                ys=sta.ys,yg=sta.yg;                fei=1;                if(tu[dx][dy]=='S')                {                    if(!(yg&(1<<(se[dx][dy]))))                    {                        yg=yg+(1<<se[dx][dy]);                        fei++;                    }                }                if(tu[dx][dy]>='1'&&tu[dx][dy]<='9')                {                    ix=tu[dx][dy]-'0';                    if(ix<=m&&ix==ys+1)                        ys++;                }                if(dp[x][y][ys][yg]>dp[sta.x][sta.y][sta.ys][sta.yg]+fei)                {                    dp[x][y][ys][yg]=dp[sta.x][sta.y][sta.ys][sta.yg]+fei;                    if(!inque[x][y][ys][yg])                    {                        inque[x][y][ys][yg]=true;                        tem.x=x,tem.y=y,tem.yg=yg,tem.ys=ys;                        que.push(tem);                    }                }            }        }    }}int main(){    int i,j;    while(scanf("%d%d",&n,&m)&&(n+m))    {        she=0;        ans=inff;        for(i=0;i<n;i++)        {            scanf("%s",tu[i]);            for(j=0;j<n;j++)            {                if(tu[i][j]=='K')                {                    kx=i,ky=j;                }                if(tu[i][j]=='T')                {                    tx=i,ty=j;                }                if(tu[i][j]=='S')                {                    se[i][j]=she++;                }            }        }        solve();        if(ans==inff)            puts("impossible");        else            printf("%d\n",ans);    }    return 0;}


0 0
原创粉丝点击