生活随笔
收集整理的這篇文章主要介紹了
最长的滑坡
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
問題描述
小袁非常喜歡滑雪, 因為滑雪很刺激。為了獲得速度,滑的區域必須向下傾斜,
而且當你滑到坡底,你不得不再次走上坡或者等待升降機來載你。
小袁想知道在某個區域中最長的一個滑坡
。區域由一個二維數組給出。數組的每個數字代表點的高度。如下:
一個人可以從某個點滑向上下左右相鄰四個點之一,當且僅當高度減小。
在上面的例子中,一條可滑行的滑坡為24-17-16-1。當然25-24-23-…-3-2-1更長。
事實上,這是最長的一條。
你的任務就是找到最長的一條滑坡,并且將滑坡的長度輸出。
滑坡的長度定義為經過點的個數,例如滑坡24-17-16-1的長度是4。
輸入格式
輸入的第一行表示區域的行數R和列數C(1<=R, C<=10)。下面是R行,
每行有C個整數,依次是每個點的高度h(0<= h <=10000)。
出格式
只有一行,為一個整數,即最長區域的長度。
樣例輸入
5 5
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
樣例輸出
25
import java
.util
.Scanner
;
public class 最長滑雪道
{static int m
,n
;static int[][] map
;static int[][] maker
;static int cnt
= 0;static int[][] dir
= {{-1,0},{1,0},{0,1},{0,-1}};static int h3
=0;static int h4
=0;static int res
=-1;public static void main(String
[] args
) {Scanner sc
= new Scanner(System
.in
);m
= sc
.nextInt();n
= sc
.nextInt();map
= new int[m
+1][n
+1];maker
= new int[m
+1][n
+1];for(int i
=0;i
<m
;i
++){for(int j
= 0;j
<n
;j
++){int temp
= sc
.nextInt();map
[i
][j
] = temp
; }}for(int i
= 0;i
<m
*n
;i
++){dfs1(i
/m
,i
-n
*(i
/m
),1);}System
.out
.println(res
);sc
.close();}private static void dfs1(int x
, int y
,int sum
) {if(sum
>res
){res
= sum
;}for(int i
= 0;i
<4;i
++){int tx
= x
+dir
[i
][0];int ty
= y
+dir
[i
][1];if(in(tx
, ty
))continue;if(maker
[tx
][ty
]==0&&map
[tx
][ty
]<map
[x
][y
]){maker
[tx
][ty
] = 1;dfs1(tx
, ty
, sum
+1);maker
[tx
][ty
] = 0;}}return;}private static boolean in(int tx
, int ty
) {if(tx
<0||ty
<0||tx
>=m
||ty
>=n
)return true;return false;}}
總結
以上是生活随笔為你收集整理的最长的滑坡的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。