hdu 1253 勝利大逃亡

2022-03-12 23:21:28 字數 1946 閱讀 5437

因為要求"最少"之類的,所以很容易想到廣搜,沒錯,這是 bfs 的裸題,只是它的狀態數是個三維陣列,轉移的狀態(方向)也從二維陣列的4個變成了6個而已,但原理是一樣的,直接廣搜即可,只是這題的坑未免也太不人性化了,我 wa了幾發看了討論區後才知道原來起點不管是不是牆都不用管(繼續搜),只需看終點即可。

1 #include2 #include3 #include4 #include5

using

namespace

std;

6#define for(i,s,t) for(int i=s; i7

const

int inf= 0x2fffffff;8

inta,b,c,t;

9int f[60][60][60], dp[60][60][60

];10

11int dir[8][3]= ,,,,,};

1213

struct

node

16};

1718

void

solve()

23 for(i,1,a+1) for(j,1,b+1) for(k,1,c+1

)24 scanf("

%d",f[i][j]+k);

2526

//如果終點是牆就永遠出不了,(這題挺坑的,起點不管是不是牆都不要緊)

27//

或者從起點到終點的最短直線距離比 t大的話也出不了,是個很重要的剪枝

28if(f[a][b][c]==1 || a+b+c-3 > t)

2930 queueq;

31 q.push(node(1,1,1

));32 dp[1][1][1]= 0;33

while

(q.size())41}

42}43//

算出到達終點的時間後,還需判斷是否滿足題意

44if(dp[a][b][c] > t) puts("-1"

);45

else printf("

%d\n

",dp[a][b][c]);46}

4748

intmain()

view code

1 #include2 #include3 #include4 #include5 #include6

using

namespace

std;

7#define for(i,s,t) for(int i=s; i8

const

int inf= 0x2fffffff;9

inta,b,c,t;

10int f[60][60][60

];11

12int dir[8][3]= ,,,,,};

1314

struct

node

17bool

operator ==(const node &n2) const

20};

2122

void

solve()

3233

node ed(a,b,c);

34 queueq;

35 q.push(node(1,1,1,0

));36

while

(q.size())45}

46}47//

算出到達終點的時間後,還需判斷是否滿足題意

48if(ed.t > t) puts("-1"

);49

else printf("

%d\n

",ed.t);50}

5152

intmain()

view code

方向陣列 dir 果然還是把 x,y,z 封裝在一維裡更好一些~~

hdu 1253 勝利大逃亡

先前在次處漏掉了,could come的判斷,結果一直wa。if step t could come include include struct q 150000 int head,tail,step,add step flag int input 52 52 52 int end x,end y...

HDU 1253 勝利大逃亡

這個題比較坑的bfs吧,注意如果判邊界的話可能會超時,最好先人為加邊界,然後進行搜尋,搜尋最好是隊尾判斷,因為可能出現起點和終點相同的資料 wa了很久 include include includeusing namespace std const int maxn 52 struct node q...

HDU 1253 勝利大逃亡

ignatius被魔王抓走了,有一天魔王出差去了,這可是ignatius逃亡的好機會.魔王住在乙個城堡裡,城堡是乙個a b c的立方體,可以被表示成a個b c的矩陣,剛開始ignatius被關在 0,0,0 的位置,離開城堡的門在 a 1,b 1,c 1 的位置,現在知道魔王將在t分鐘後回到城堡,i...