生活随笔
收集整理的這篇文章主要介紹了
Jelly
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
鏈接:
時間限制:C/C++ 1秒,其他語言2秒 空間限制:C/C++ 131072K,其他語言262144K 64bit IO Format:%lld
題目描述
Nancy喜歡吃果凍! Nancy鉆進了一個n \times n \times nn×n×n的果凍里,她想從(1,1,1)一路上、下、左、右、前、后六個方向吃到(n,n,n)。 但果凍畢竟是有許多口味的,標記為*的口味是Nancy不愿意吃的,其余的果凍均標記為.。 Nancy不想吃壞肚子,于是她想盡可能少的吃果凍。 下面給出果凍的情況,請你幫忙計算一下她能吃多少塊果凍叭!
輸入描述:
第一行:一個整數n。 接下來n層,每組n行,每行n列,表示果凍(i,j,k)的情況(如題目描述所述)。
數據滿足:1≤n≤100,保證果凍(1,1,1)不是Nancy不愿意吃的。 輸出描述: 如果可以到達(n,n,n),請輸出路上吃的果凍數量,否則請輸出-1。 示例1 輸入
2
. *
. .
* .
. .
輸出
4
題解: 三維版dfs 方向向量: int dx[]={0,1,0,-1,0,0}; int dy[]={1,0,-1,0,0,0}; int dz[]={0,0,0,0,1,-1}; dis用于記錄步數 for循環枚舉方向向量,從(1,1,1)開始分別向上、下、左、右、前、后六個方向探索,如果不是 * 就加一 最后輸出dis[n][n][n] 代碼:
#include <bits/stdc++.h>
using namespace std
;
const int maxn
= 104 ;
char a
[ maxn
] [ maxn
] [ maxn
] ; int dx
[ ] = { 0 , 1 , 0 , - 1 , 0 , 0 } ;
int dy
[ ] = { 1 , 0 , - 1 , 0 , 0 , 0 } ;
int dz
[ ] = { 0 , 0 , 0 , 0 , 1 , - 1 } ;
struct edge
{ int x
, y
, z
;
} ;
queue
< edge
> q
;
int dis
[ maxn
] [ maxn
] [ maxn
] ; int main ( )
{ int n
; cin
>> n
; char ch
= getchar ( ) ; for ( int i
= 1 ; i
<= n
; i
++ ) { for ( int j
= 1 ; j
<= n
; j
++ ) { for ( int k
= 1 ; k
<= n
; k
++ ) { cin
>> a
[ i
] [ j
] [ k
] ; } ch
= getchar ( ) ; } } if ( a
[ n
] [ n
] [ n
] == '*' ) { cout
<< - 1 ; return 0 ; } q
. push ( ( edge
) { 1 , 1 , 1 } ) ; dis
[ 1 ] [ 1 ] [ 1 ] = 1 ; while ( ! q
. empty ( ) ) { edge now
= q
. front ( ) ; q
. pop ( ) ; int x
, y
, z
; for ( int i
= 0 ; i
< 6 ; i
++ ) { x
= now
. x
+ dx
[ i
] ; y
= now
. y
+ dy
[ i
] ; z
= now
. z
+ dz
[ i
] ; if ( x
&& y
&& z
&& x
<= n
&& y
<= n
&& z
<= n
&& a
[ x
] [ y
] [ z
] != '*' && dis
[ x
] [ y
] [ z
] == 0 ) { dis
[ x
] [ y
] [ z
] = dis
[ now
. x
] [ now
. y
] [ now
. z
] + 1 ; q
. push ( ( edge
) { x
, y
, z
} ) ; } } } cout
<< dis
[ n
] [ n
] [ n
] ; }
總結
以上是生活随笔 為你收集整理的Jelly 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。