• HDU 5025 Saving Tang Monk


     

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


    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

    这条题是国赛广州网络赛的一条题 ,这场比赛我有打 , 但是我们队的名次到不了前两百 ,也是因为这条题吧。

    这条题当时也是我写的 , 然后出各种问题 。

    首先在矩阵上bfs的话 要先考虑要不要用优先队列 , 状态 - > 下个状态  ( 花费多数递增的话 ) 通常只用普通队列就可以了 。

    然后有号码(即使不是你现在需要的号码)的地方, 还有T所在的地方你也是可以经过的 。

    然后很裸的BFS一次就可以了~

    
    
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    using namespace std;
    const int  N = 110;
    
    int n , m ;
    bool vis[10][N][N][65];
    int sn[N][N];
    char mp[N][N];
    
    int dx[] = { 0 , 0 , -1 , 1 };
    int dy[] = { -1 , 1 , 0 , 0 };
    
    struct node
    {
        int x,y,st,k,cost;
        node(){};
    };
    
    bool inside (node v){
        if( v.x < 0 || v.x >= n ) return false ;
        if( v.y < 0 || v.y >= n ) return false ;
        if( mp[v.x][v.y] == '#' ) return false ;
        return true;
    }
    
    int bfs( node u ){
        int ans = -1 ;
        node v ;
        memset ( vis , false , sizeof vis );
        queue< node >que;
        que.push( u ) ;
    
        vis[u.k][u.x][u.y][u.st] = 1;
    
        while( ! que.empty() ){
    
            u = que.front(); que.pop();
    
            if(  ans != -1 && ans <= u.cost )  {  continue; }
    
    //        cout<<u.x<<' '<<u.y << ' '<<u.k << ' ' <<u.st << ' '<< u.cost <<endl;
    
            for(  int i = 0 ; i < 4 ; ++ i ){
                v = u , v.x += dx[i] , v.y += dy[i] ;
    
                if( ! inside( v ) ) continue ;
    
                if( mp[v.x][v.y] == 'S' ){
                    if( (  u.st & ( 1 << sn[v.x][v.y] ) ) == 0 ) { v.cost ++ ; v.st |= ( 1<<sn[v.x][v.y] ); }
                }
                else if( mp[v.x][v.y] >= '1' && mp[v.x][v.y] <='9' ){
                    int key = mp[v.x][v.y] - '0' ;
                    if( u.k + 1 == key ){ v.k ++ ; }
                }
                else if( mp[v.x][v.y] == 'T' ){
                    if( u.k == m ){
                        if( ans == -1 || ans > u.cost + 1 ){   ans = u.cost + 1  ; continue;   }
                    }
                }
                v.cost ++ ;
    
                if( !vis[v.k][v.x][v.y][v.st]  ) { vis[v.k][v.x][v.y][v.st] =1 , que.push(v); }
            }
        }
        return ans ;
    }
    
    
    void run()
    {
        node s ;
        int tot = 0 ;
        for( int i = 0 ; i < n ;++i ){
            scanf("%s",mp[i]);
            for( int j = 0 ; j < n ; ++j ){
                if( mp[i][j] == 'K') { s.x = i , s.y = j , s.k = 0 ,s.cost = 0 , s.st = 0 ;}
                else if( mp[i][j] == 'S' ){ sn[i][j] = tot++ ; }
            }
        }

    int ans = bfs(s ); if( ans == -1 ) puts ("impossible"); else printf("%d ",ans); } int main() { #ifdef LOCAL freopen("in.txt","r",stdin); #endif // LOCAL ios::sync_with_stdio(false); while( scanf("%d%d",&n ,&m ) != EOF ){ if( !n && !m )break; run(); } return 0 ; }
     
    only strive for your goal , can you make your dream come true ?
  • 相关阅读:
    分治法解决寻找数组中最大最小值的问题
    bootstrap动画进度条
    关于bootstrap中css样式与自己设置的css样式相撞问题
    css引入外部字体
    清除浮动
    四叶玫瑰数
    水仙花数
    nginx 配置文件服务器
    springboot 自定义拦截器 防止恶意请求
    springboot 自定义异常
  • 原文地址:https://www.cnblogs.com/hlmark/p/3987381.html
Copyright © 2020-2023  润新知