poj 1088 小白演算法練習 滑雪 dfs記憶化

2021-08-04 08:39:33 字數 1837 閱讀 7969

滑雪

time limit:1000ms

memory limit:65536k

total submissions:98816

accepted:37530

description

michael喜歡滑雪百這並不奇怪, 因為滑雪的確很刺激。可是為了獲得速度,滑的區域必須向下傾斜,而且當你滑到坡底,你不得不再次走上坡或者等待公升降機來載你。michael想知道載乙個區域中最長底滑坡。區域由乙個二維陣列給出。陣列的每個數字代表點的高度。下面是乙個例子 

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

乙個人可以從某個點滑向上下左右相鄰四個點之一,當且僅當高度減小。在上面的例子中,一條可滑行的滑坡為24-17-16-1。當然25-24-23-...-3-2-1更長。事實上,這是最長的一條。

input

輸入的第一行表示區域的行數r和列數c(1 <= r,c <= 100)。下面是r行,每行有c個整數,代表高度h,0<=h<=10000。

output

輸出最長區域的長度。

sample input

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

sample output

25
**:

#include

#include

using

namespace std;

int ski[

101][

101];

int r,c;

int sum=0;

int maximal=-1;

void

dfs(

int i,

int j,

int sum)

if(ski[i+1

][j]if(ski[i][j-1

]if(ski[i][j+1

]if(sum>maximal)

}int

main()}

for(

int i=

1;i<=r;i++)

} cout<<}

//記憶化索搜
#include

#include

using

namespace std;

int ski[

101][

101];

int mid[

101][

101];

int dx[4]=

;int dy[4]=

;int r,c;

int maximal=-1;

int sum=0;

boolok(

int i,

int j)

intdfs

(int i,

int j)

}return mid[i][j];

}int

main()}

for(

int i=

1;i<=r;i++)}}

cout<<}

poj 1088 滑雪問題

這題剛開始我想到的是搜尋,但是超時了,所以要進行優化,聽說這是dp經典題,當時就瘋了,dp才初學,想到的是 dp i,j max dp 四個方向 但寫出來不太理想,主要是不懂的遞迴間數值的變化,導致一直出不來答案!後來看題解啊,改啊,搞啊,許久才出來個能a的 並且還0ms了,看來還得多熟悉下 inc...

poj 1088 滑雪問題

題目大意 中文。不解釋 解題思路 動態規劃,這裡要求從乙個點往上下左右移動,要滿足高度差,我們可以把每個位置記錄儲存好,按照高度,從小到大排列,然後利用動態規劃往乙個高度增長的方向就可以處理,轉換為類似於最長遞增子串行問題 include include include include using ...

POJ 1088 滑雪 題解

poj1062 昂貴的聘禮 michael喜歡滑雪百這並不奇怪,因為滑雪的確很刺激。可是為了獲得速度,滑的區域必須向下傾斜,而且當你滑到坡底,你不得不再次走上坡或者等待公升降機來載你。michael想知道載乙個區域中最長底滑坡。區域由乙個二維陣列給出。陣列的每個數字代表點的高度。下面是乙個例子 1 ...