生活随笔
收集整理的這篇文章主要介紹了
最长的滑坡
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
問題描述
小袁非常喜歡滑雪, 因?yàn)榛┖艽碳ぁ榱双@得速度,滑的區(qū)域必須向下傾斜,
而且當(dāng)你滑到坡底,你不得不再次走上坡或者等待升降機(jī)來載你。
小袁想知道在某個(gè)區(qū)域中最長(zhǎng)的一個(gè)滑坡
。區(qū)域由一個(gè)二維數(shù)組給出。數(shù)組的每個(gè)數(shù)字代表點(diǎn)的高度。如下:
一個(gè)人可以從某個(gè)點(diǎn)滑向上下左右相鄰四個(gè)點(diǎn)之一,當(dāng)且僅當(dāng)高度減小。
在上面的例子中,一條可滑行的滑坡為24-17-16-1。當(dāng)然25-24-23-…-3-2-1更長(zhǎng)。
事實(shí)上,這是最長(zhǎng)的一條。
你的任務(wù)就是找到最長(zhǎng)的一條滑坡,并且將滑坡的長(zhǎng)度輸出。
滑坡的長(zhǎng)度定義為經(jīng)過點(diǎn)的個(gè)數(shù),例如滑坡24-17-16-1的長(zhǎng)度是4。
輸入格式
輸入的第一行表示區(qū)域的行數(shù)R和列數(shù)C(1<=R, C<=10)。下面是R行,
每行有C個(gè)整數(shù),依次是每個(gè)點(diǎn)的高度h(0<= h <=10000)。
出格式
只有一行,為一個(gè)整數(shù),即最長(zhǎng)區(qū)域的長(zhǎng)度。
樣例輸入
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 最長(zhǎng)滑雪道
{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;}}
總結(jié)
以上是生活随笔為你收集整理的最长的滑坡的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。