生活随笔
收集整理的這篇文章主要介紹了
问题 C: 逃离机场
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目描述
小明聽說機場是一個很肥的地方,所以想跳一波機場,看看到底有多肥。不過機場雖然肥,但是跳的人也多。小明第一次跳機場,剛跳下來就到處都是槍聲,小明嚇得快要哭出來了,想逃離機場,emmm,還是打野比較適合他。 現(xiàn)在把機場看作一個二維平面,’.‘代表可以走的空地,’@'代表小明當前的位置,'x’代表這里是個障礙物,'o’代表這里有個敵人,并且手里有槍,敵人可以攻擊上下左右四個方向,小明只要走到或者一開始就在敵人可以攻擊的位置,就會死亡(機場個個都是98K爆頭dalao),子彈不會穿過障礙物,敵人不會移動。小明只能往上下左右走,每走一步需要1秒,只要小明移動到機場的邊緣再走一步就算逃離了機場,現(xiàn)在小明請你幫他找一條最快逃離機場的線路,輸出這個時間,如果怎么都逃不出去,輸出"no zuo no die!"(不含引號)。
輸入格式
多組測試數(shù)據(jù),首先第一行一個整數(shù)T,代表測試數(shù)據(jù)組數(shù)。1≤T≤100。 每組測試數(shù)據(jù)有n,m(1≤n,m≤200),代表機場是一個n×m的方陣,接下來是一個由’.’,’@’,‘x’,‘o’構(gòu)成的n×m的方陣,’@'只有一個。
輸出格式
對于每組數(shù)據(jù)輸出一個數(shù)代表最快需要多少時間逃出機場,或者"no zuo no die!"。
輸入樣例
3 5 5 .x.x. .x.x. … …@… .o.o. 3 3 @… xxx o.x 3 3 o.o .@. .x.
題目思路:1.只要小明落在敵人上下左右任意一個方向都會死。因此先對敵人四個方向進行“#“填充,填充過程一旦遇到小明,則游戲結(jié)束2.對小明位置展開BFS,尋找最短路徑。 代碼如下:
#include <iostream>
#include <queue>
#include <cstdio>
using namespace std
;
struct node
{ int x
; int y
; int step
;
} ;
node s
;
char mapp
[ 500 ] [ 500 ] ;
int dir
[ 4 ] [ 2 ] = { 1 , 0 , - 1 , 0 , 0 , 1 , 0 , - 1 } ;
int n
, m
;
void book ( int x
, int y
, int & flag
)
{ mapp
[ x
] [ y
] = '#' ; for ( int i
= x
+ 1 ; i
< n
; i
++ ) { if ( mapp
[ i
] [ y
] != '.' ) { if ( mapp
[ i
] [ y
] == '@' ) flag
= 1 ; else if ( mapp
[ i
] [ y
] == '#' ) continue ; break ; } else if ( mapp
[ i
] [ y
] == '.' ) mapp
[ i
] [ y
] = '#' ; } for ( int i
= x
- 1 ; i
>= 0 ; i
-- ) { if ( mapp
[ i
] [ y
] != '.' ) { if ( mapp
[ i
] [ y
] == '@' ) flag
= 1 ; else if ( mapp
[ i
] [ y
] == '#' ) continue ; break ; } else if ( mapp
[ i
] [ y
] == '.' ) mapp
[ i
] [ y
] = '#' ; } for ( int i
= y
- 1 ; y
>= 0 ; y
-- ) { if ( mapp
[ x
] [ i
] != '.' ) { if ( mapp
[ x
] [ i
] == '@' ) flag
= 1 ; else if ( mapp
[ x
] [ i
] == '#' ) continue ; break ; } else if ( mapp
[ x
] [ i
] == '.' ) mapp
[ x
] [ i
] = '#' ; } for ( int i
= y
+ 1 ; i
< m
; i
++ ) { if ( mapp
[ x
] [ i
] == != '.' ) { if ( mapp
[ x
] [ i
] == '@' ) flag
= 1 ; else if ( mapp
[ x
] [ i
] == '#' ) continue ; break ; } else if ( mapp
[ x
] [ i
] == '.' ) mapp
[ x
] [ i
] = '#' ; } } int bfs ( node s1
) { queue
< node
> temp
; node u1
, t1
; mapp
[ s1
. x
] [ s1
. y
] = 'X' ; temp
. push ( s1
) ; while ( ! temp
. empty ( ) ) { u1
= temp
. front ( ) ; temp
. pop ( ) ; if ( u1
. x
+ 1 >= n
|| u1
. x
- 1 < 0 || u1
. y
+ 1 >= m
|| u1
. y
- 1 < 0 ) return 1 + u1
. step
; for ( int i
= 0 ; i
< 4 ; i
++ ) { t1
. x
= u1
. x
+ dir
[ i
] [ 0 ] ; t1
. y
= u1
. y
+ dir
[ i
] [ 1 ] ; if ( mapp
[ t1
. x
] [ t1
. y
] == '.' ) { t1
. step
= u1
. step
+ 1 ; mapp
[ t1
. x
] [ t1
. y
] = 'X' ; temp
. push ( t1
) ; } } } return - 1 ; } int main ( )
{ int N
; while ( cin
>> N
) { while ( N
-- ) { int flag
= 0 ; cin
>> n
>> m
; for ( int i
= 0 ; i
< n
; i
++ ) scanf ( "%s" , mapp
[ i
] ) ; for ( int i
= 0 ; i
< n
; i
++ ) { for ( int j
= 0 ; j
< m
; j
++ ) { if ( mapp
[ i
] [ j
] == '@' ) { s
. x
= i
; s
. y
= j
; s
. setp
= 0 ; } else if ( mapp
[ i
] [ j
] == 'o' ) { book ( i
, j
, flag
) ; } } } if ( flag
== 1 ) cout
<< "no zuo no die!" << endl
; else { int sum
; sum
= bfs ( s
) ; if ( sum
== ( - 1 ) ) cout
<< "no zuo no die!" << endl
; else cout
<< sum
<< endl
; } } } return 0 ;
}
總結(jié)
以上是生活随笔 為你收集整理的问题 C: 逃离机场 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。