博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 5025 Saving Tang Monk
阅读量:6892 次
发布时间:2019-06-27

本文共 5341 字,大约阅读时间需要 17 分钟。

 

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 
#include
#include
#include
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.cost <
= '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\n",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 ;}
 

转载于:https://www.cnblogs.com/hlmark/p/3987381.html

你可能感兴趣的文章
OpenCV与QT联合编译 分类: Eye_Detection ...
查看>>
LNMP架构介绍
查看>>
洛谷3986
查看>>
ssh服务
查看>>
Eclipse的基本使用
查看>>
构建之法 第五章 团队和流程
查看>>
(转)如何在eclipse的配置文件里指定jdk路径
查看>>
如何将atom侧边栏显示在右侧
查看>>
Block介绍(一)基础
查看>>
COLORREF的结构和用法
查看>>
《c程序设计语言》读书笔记--统计换行数,空格数,tab数
查看>>
【14】代理模式(Proxy Pattern)
查看>>
C# 16进制与字符串、字节数组之间的转换
查看>>
Visual Studio 2013 为C#类文件添加版权信息
查看>>
[考试]20151105
查看>>
排序一:冒泡以及三种优化
查看>>
NodeJS Stream 五:双工流
查看>>
Visual Studio 2010中配置SharpPcap
查看>>
记一次由于缺少外键索引导致的血案
查看>>
css基础之 图片瀑布流布局:用CSS+DIV等宽格子堆砌瀑布流效果 (一)
查看>>