NOI 题库 2727

2727   仙岛求药

描述
少年李逍遥的婶婶病了,王小虎介绍他去一趟仙灵岛,向仙女姐姐要仙丹救婶婶。叛逆但孝顺的李逍遥闯进了仙灵岛,克服了千险万难来到岛的中心,发现仙药摆在了迷阵的深处。迷阵由M×N个方格组成,有的方格内有可以瞬秒李逍遥的怪物,而有的方格内则是安全。现在李逍遥想尽快找到仙药,显然他应避开有怪物的方格,并经过最少的方格,而且那里会有神秘人物等待着他。现在要求你来帮助他实现这个目标。
下图 显示了一个迷阵的样例及李逍遥找到仙药的路线.
输入
输入有多组测试数据. 每组测试数据以两个非零整数 M 和 N 开始,两者均不大于20。M 表示迷阵行数, N 表示迷阵列数。接下来有 M 行, 每行包含N个字符,不同字符分别代表不同含义: 
1) ‘@’:少年李逍遥所在的位置;
2) ‘.’:可以安全通行的方格;
3) ‘#’:有怪物的方格;
4) ‘*’:仙药所在位置。
当在一行中读入的是两个零时,表示输入结束。
输出
对于每组测试数据,分别输出一行,该行包含李逍遥找到仙药需要穿过的最少的方格数目(计数包括初始位置的方块)。如果他不可能找到仙药, 则输出 -1。
样例输入
8 8
.@##...#
#....#.#
#.#.##..
..#.###.
#.#...#.
..###.#.
...#.*..
.#...###
6 5
.*.#.
.#...
..##.
.....
.#...
....@
9 6
.#..#. 
.#.*.# 
.####. 
..#... 
..#... 
..#... 
..#... 
#.@.## 
.#..#. 
0 0
样例输出
10
8
-1
 1 #include "bits/stdc++.h"
 2 
 3 using namespace std ;
 4 const int maxN = 1e5 + 1e3 ;
 5 const int INF = 2147483647 ; 
 6 
 7 int start_x , start_y , des_x , des_y ;
 8 const int dx [ ] = { 1 , 0 , -1 , 0 } ;
 9 const int dy [ ] = { 0 , 1 , 0 , -1 } ;
10 
11 char mp[ 55 ][ 55 ] ;
12 int pos_x[ maxN ] , pos_y [ maxN ] ,step[ maxN ] ;
13 void Scan ( const int n , const int m ) {
14         getchar ( ) ;
15         for ( int i=1 ; i<=n ; ++i ) {
16                 for ( int j=1 ; j<=m ; ++j ) {
17                         mp[ i ][ j ] = getchar ( ) ;
18                         if ( mp [ i ][ j ] == '@' ) {
19                                 start_x = i ; 
20                                 start_y = j ;
21                         }
22                         if( mp[ i ][ j ] == '*' ) {
23                                 des_x = i ;
24                                 des_y = j ; 
25                                 mp[ i ][ j ] = '.' ;
26                         } 
27                 }
28                 getchar ( ) ;
29         }
30 }
31 
32 void BFS ( const int n , const int m ) {
33         int Head = 0 , Tail = 1 ; 
34         bool Get_Target = false ;
35         pos_x[ Tail ] = start_x ;
36         pos_y[ Tail ] = start_y ;
37         step[ Tail ] = 0 ;
38         mp[ start_x ][ start_y ] = '#' ;
39         while ( Head < Tail ) {
40                 ++ Head ;
41                 for ( int i=0 ; i<4 ; ++i ) {
42                         int xx = pos_x[ Head ] + dx[ i ] ;
43                         int yy = pos_y[ Head ] + dy[ i ] ;
44                         if ( xx > 0 && xx <= n && yy >= 0 && yy <= m && ( mp[ xx ][ yy ] == '.' || mp[ xx ][ yy ] == '*' ) ) {
45                                  step[ ++ Tail ] = step[ Head ] + 1 ;
46                                  mp[ xx ][ yy ] = '#' ;
47                                  pos_x [ Tail ] = xx ;
48                                  pos_y [ Tail ] = yy ; 
49                                  if ( xx == des_x && yy == des_y ) {
50                                          Get_Target = true ;
51                                          printf ( "%d\n" , step[ Tail ] ) ;
52                                  }
53                         } 
54                 }
55         }
56         if ( !Get_Target ) { printf ( "-1\n" ) ; }
57 }
58 
59 int main ( ) {
60         int N , M ;
61         while ( scanf ( "%d%d" , &N , &M ) && N && M ) {
62                 Scan ( N , M ) ;
63                 BFS ( N , M );
64         }
65 }
View Code

 

2016-10-20 19:43:03

 

()

 

posted @ 2016-10-20 19:44  SHHHS  阅读(515)  评论(0编辑  收藏  举报