zoukankan      html  css  js  c++  java
  • HDU 5025 Saving Tang Monk(状压bfs)

    Saving Tang Monk




    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 1
    K.S
    ##1
    1#T
    3 1
    K#T
    .S#
    1#.
    3 2
    K#T
    .S.
    21.
    0 0
     
     
    Sample Output
    5
    impossible
    8
     
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<queue>
     5 using namespace std;
     6 const int INF=0x3f3f3f3f;
     7 bool vis[105][105][10][32];
     8 char G[105][105];
     9 int d[4][2]={1,0,0,1,-1,0,0,-1};
    10 int sx,sy,n,m;
    11 int ans;
    12 struct node
    13 {
    14     int x,y,step;
    15     int key,S;
    16 };
    17 void bfs(int x,int y)
    18 {
    19     queue<node>q;
    20     node t1,t2;
    21     t1.x=sx,t1.y=sy,t1.step=0,t1.key=0,t1.S=0;
    22     vis[sx][sy][0][0]=1;
    23     q.push(t1);
    24     while(!q.empty())
    25     {
    26         t1=q.front(),q.pop();
    27         if(G[t1.x][t1.y]=='T'&&t1.key==m)
    28             ans=min(ans,t1.step);
    29         for(int i=0;i<4;i++)
    30         {
    31             t2.x=t1.x+d[i][0];
    32             t2.y=t1.y+d[i][1];
    33             t2.step=t1.step+1;
    34             t2.key=t1.key;
    35             t2.S=t1.S;
    36             if(t2.x<0||t2.y<0||t2.x>=n||t2.y>=n||G[t2.x][t2.y]=='#')continue;
    37             if(G[t2.x][t2.y]>='1'&&G[t2.x][t2.y]<='9')
    38             {
    39                 if(t2.key==G[t2.x][t2.y]-'0'-1)
    40                     t2.key=G[t2.x][t2.y]-'0';
    41             }
    42             else if(G[t2.x][t2.y]>='A'&&G[t2.x][t2.y]<='E')
    43             {
    44                 int t=1<<(G[t2.x][t2.y]-'A');
    45                 if(!(t2.S&t))
    46                 {
    47                     t2.S|=t;
    48                     t2.step+=1;
    49                 }
    50             }
    51             if(!vis[t2.x][t2.y][t2.key][t2.S])
    52                 q.push(t2),vis[t2.x][t2.y][t2.key][t2.S]=1;
    53         }
    54     }
    55 }
    56 int main()
    57 {
    58     while(scanf("%d%d",&n,&m),n|m)
    59     {
    60         ans=INF;
    61         memset(vis,0,sizeof(vis));
    62         int id=0;
    63         for(int i=0;i<n;i++)
    64             scanf("%s",G[i]);
    65         for(int i=0;i<n;i++)
    66             for(int j=0;j<n;j++)
    67                 if(G[i][j]=='K')
    68                     sx=i,sy=j;
    69                 else if(G[i][j]=='S')
    70                     G[i][j]='A'+id,id++;
    71                 else;
    72 
    73         bfs(sx,sy);
    74         if(ans==INF)
    75             puts("impossible");
    76         else
    77             printf("%d
    ",ans);
    78     }
    79     return 0;
    80 }
  • 相关阅读:
    java—— 字节流
    20-转>ES6转ES5的实现原理
    18-检验闭包是否真正理解?
    17-数组中去除基本数据类型和NaN的重复项(并保证原有数组顺序)
    16-实现一个Promise.all 和 Promise.race
    14-数组求和之递归方式
    13-斐波那契数列
    12-找到数组中的两项的和等于传入的指定数
    11-合并两个有序数组
    10-判断两个对象是否相等(有点类型every的实现)
  • 原文地址:https://www.cnblogs.com/homura/p/5422571.html
Copyright © 2011-2022 走看看