生活随笔
收集整理的這篇文章主要介紹了
杭电1044java实现dfs bfs
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Collect More Jewels 問題描述 它寫在“夫人的書:創世之后,殘酷的神摩洛克反抗了造物主馬爾杜克的權威。摩爾從馬爾杜克那里偷走了眾神中所有神器中最強大的一件,也就是葉多爾的護身符,并且他隱藏了它在Gehennom的陰暗洞穴,現在潛伏在他身邊的Under World,并且是他的時間。
你的女神女士尋求擁有護身符,并與它一起獲得應得的尊榮,勝過其他諸神。
你是一位新接受過訓練的漫步者,從出生時就已經成為了女士的樂器。你注定要為你的神祗恢復護身符,或者在企圖中死去。你的命運已經到了。為了我們所有人:勇敢地與The Lady一起去!
如果你曾經玩過電腦游戲NETHACK,你必須熟悉上面的引用。如果你從來沒有聽說過,不要擔心。你會很快學會它(并且很喜歡它)。
在這個問題上,你是冒險家,處于危險的地下城。你被告知地牢將崩潰。您必須在給定時間內找到出口樓梯。但是,你不想空手離開地牢。地牢里有很多珍貴的珠寶。在你離開之前嘗試收集他們中的一些。一些珠寶更便宜,有些更昂貴。所以你會盡力使你的收藏最大化,更重要的是,要及時離開地下城。
輸入 標準輸入將包含多個測試用例。輸入的第一行是一個整數T(1 <= T <= 10),它是測試用例的數量。接下來是T測試用例,每個測試用例都有一個空白行。
每個測試用例的第一行包含四個整數W(1≤W≤50),H(1≤H≤50),L(1≤L≤1000000)和M(1≤M≤50) = 10)。地牢是一個矩形區域,寬度為W,高度為H。 L是您需要到達出口的時間限制。只要目標區塊位于地下城內并且不是墻壁,您就可以移動到每個時間單位上下左右的相鄰區塊之一。游戲開始時,時間從1開始。 M是地下城里珠寶的數量。一旦冒險家在這個街區,珠寶就會被收集起來。這不會花費額外的時間。
下一行包含M個整數,這是珠寶的值。
接下來的H行將包含W個字符。他們用下列符號表示地牢地圖:
[*]標志著一堵墻,你無法移動; [。]標記一個空的空間,你可以在其中移動; [@]標志著冒險家的初始位置; [<]標記出口樓梯; [A] - [J]標志著珠寶。
輸出 結果應該針對標準輸出。在一行中以“案例#:”開始每個案例,其中#是從1開始的案例編號。連續兩個案例應該由一個空行分隔。在最后的測試用例之后不應該產生空白行。
如果冒險者可以在規定的時間內到達出口樓梯,可以打印“最好成績為S”的句子,其中S是他可以沿途收集的珠寶的最大值;否則在單行上打印“不可能”一詞。 Sample Input
34 4 2 2
100 200
****
*@A*
*B<*
****4 4 1 2
100 200
****
*@A*
*B<*
****12 5 13 2
100 200
************
*B.........*
*.********.*
*@...A....<*
************
Sample Output Case 1: The best score is 200.
Case 2: Impossible
Case 3: The best score is 300. 思路:先用bfs預估記。記錄點點之間的最短距離。然后再dfs遍歷點點。不可以直接遍歷圖。圖太大會直接爆時。我處理的方式是建立dp【】【】【】三維代表xy到第m個點的距離。也可以用dp【】【】【】【】表示點的距離。因為我的m點有專門記錄。dfs開始也可以從起始點開始,遍歷到結束點后就停止。 代碼如下:
import java
. util
. ArrayDeque
; import java
. util
. Queue
; import java
. util
. Scanner
; public class 杭電
1044 { static int dx
[ ] = { 0 , 1 , 0 , - 1 } ; static int dy
[ ] = { 1 , 0 , - 1 , 0 } ; static int x1
= 0 , y1
= 0 , x2
= 0 , y2
= 0 , t
= 0 , w
= 0 , h
= 0 , m
= 0 ; static int score
= 0 ; static int m1
[ ] ; static int path
= 0 ; static char mi
[ ] [ ] = new char [ 50 ] [ 50 ] ; public static void main ( String
[ ] args
) { Scanner sc
= new Scanner ( System
. in
) ; int T
= sc
. nextInt ( ) ; for ( int T1
= 1 ; T1
< T
1 ; T1
) { sc
. nextLine ( ) ; w
= sc
. nextInt ( ) ; h
= sc
. nextInt ( ) ; t
= sc
. nextInt ( ) ; m
= sc
. nextInt ( ) ; m1
= new int [ m
1 ] ; int m2
[ ] [ ] = new int [ m
1 ] [ 2 ] ; for ( int i
= 0 ; i
< m
; i
) { m1
[ i
] = sc
. nextInt ( ) ; } sc
. nextLine ( ) ; for ( int i
= 0 ; i
< h
; i
) { String a
= sc
. nextLine ( ) ; mi
[ i
] = a
. toCharArray ( ) ; } for ( int i
= 0 ; i
< h
; i
) { for ( int j
= 0 ; j
< w
; j
) { if ( mi
[ i
] [ j
] == '@' ) { x1
= i
; y1
= j
; mi
[ i
] [ j
] = '.' ; } if ( mi
[ i
] [ j
] == '<' ) { x2
= i
; y2
= j
; mi
[ i
] [ j
] = '.' ; } if ( 'A' <= mi
[ i
] [ j
] && mi
[ i
] [ j
] <= 'A' m
) { m2
[ mi
[ i
] [ j
] - 'A' ] [ 0 ] = i
; m2
[ mi
[ i
] [ j
] - 'A' ] [ 1 ] = j
; } } } m2
[ m
] [ 0 ] = x2
; m2
[ m
] [ 1 ] = y2
; int dp
[ ] [ ] [ ] = new int [ h
] [ w
] [ m
1 ] ; if ( bfs ( x1
, y1
, x2
, y2
) ) { score
= 0 ; boolean jud
[ ] = new boolean [ m
1 ] ; dfs ( x1
, y1
, 0 , 0 , jud
, m2
, dp
) ; System
. out
. println ( "Case " T1
":" ) ; if ( T1
== T
) { System
. out
. println ( "The best score is " score
"." ) ; } else { System
. out
. println ( "The best score is " score
"." ) ; System
. out
. println ( ) ; } } else { System
. out
. println ( "Case " T1
":" ) ; if ( T1
== T
) { System
. out
. println ( "Impossible" ) ; } else { System
. out
. println ( "Impossible" ) ; System
. out
. println ( ) ; } } } } private static void dfs ( int x
, int y
, int t2
, int q
, boolean [ ] jud
, int [ ] [ ] m2
, int [ ] [ ] [ ] dp
) { if ( x
== x2
&& y
== y2
) { if ( t2
<= t
) { if ( q
> score
) { score
= q
; } } } else if ( t2 Math
. abs ( x
- x2
) Math
. abs ( y
- y2
) > t
) { } else for ( int i
= 0 ; i
< m
1 ; i
) { if ( ! jud
[ i
] && dp
[ x
] [ y
] [ i
] != - 1 ) { if ( dp
[ x
] [ y
] [ i
] == 0 ) { if ( bfs ( x
, y
, m2
[ i
] [ 0 ] , m2
[ i
] [ 1 ] ) ) { jud
[ i
] = true ; dp
[ x
] [ y
] [ i
] = path
; dfs ( m2
[ i
] [ 0 ] , m2
[ i
] [ 1 ] , t2 path
, q m1
[ i
] , jud
, m2
, dp
) ; jud
[ i
] = false ; } else { dp
[ x
] [ y
] [ i
] = - 1 ; } } else { jud
[ i
] = true ; dfs ( m2
[ i
] [ 0 ] , m2
[ i
] [ 1 ] , t2 dp
[ x
] [ y
] [ i
] , q m1
[ i
] , jud
, m2
, dp
) ; jud
[ i
] = false ; } } } } private static boolean bfs ( int a1
, int b1
, int a2
, int b2
) { path
= 0 ; Queue
< zuobiao> q1
= new ArrayDeque ( ) ; q1
. add ( new zuobiao ( a1
, b1
, 0 ) ) ; if ( Math
. abs ( a1
- a2
) Math
. abs ( b2
- b1
) > t
) { return false ; } if ( Math
. abs ( a1
- x2
) Math
. abs ( y2
- b1
) > t
) { return false ; } boolean b
[ ] [ ] = new boolean [ h
] [ w
] ; b
[ a1
] [ b1
] = true ; while ( ! q1
. isEmpty ( ) ) { zuobiao zuo
= q1
. poll ( ) ; int x
= zuo
. x
; int y
= zuo
. y
; if ( x
== a2
&& y
== b2
) { if ( zuo
. time
<= t
) { path
= zuo
. time
; return true ; } } else for ( int i
= 0 ; i
< 4 ; i
) { if ( x dx
[ i
] >= 0 && x dx
[ i
] < h
&& y dy
[ i
] >= 0 && y dy
[ i
] < w
) { if ( mi
[ x dx
[ i
] ] [ y dy
[ i
] ] != '*' && ! b
[ x dx
[ i
] ] [ y dy
[ i
] ] ) { if ( zuo
. time
1 Math
. abs ( y dy
[ i
] - y2
) Math
. abs ( x dx
[ i
] - x2
) <= t
) { q1
. add ( new zuobiao ( x dx
[ i
] , y dy
[ i
] , zuo
. time
1 ) ) ; b
[ x dx
[ i
] ] [ y dy
[ i
] ] = true ; } } } } } return false ; } static class zuobiao { int x
; int y
; int time
; int value
; public zuobiao ( int x
, int y
, int time
) { this . x
= x
; this . y
= y
; this . time
= time
; } } }
總結
以上是生活随笔 為你收集整理的杭电1044java实现dfs bfs 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。