題目**:滑雪
滑雪time limit:1000ms
memory limit:65536k
total submissions:98595
accepted:37445
description
michael喜歡滑雪百這並不奇怪, 因為滑雪的確很刺激。可是為了獲得速度,滑的區域必須向下傾斜,而且當你滑到坡底,你不得不再次走上坡或者等待公升降機來載你。michael想知道載乙個區域中最長底滑坡。區域由乙個二維陣列給出。陣列的每個數字代表點的高度。下面是乙個例子
1 2 3 4 516 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 5sample output1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
25source
shtsc 2002
思路:
記憶化搜尋,假如我們以每個點為起點進行一遍搜尋,一定會超時。我們可以發現從某乙個點出發得到的最長路我們會反覆算好多遍,所以我們想辦法讓他算一遍就ok了,
所以我們開乙個dp陣列記錄以這個點為起點的最長路。我們從每個點進行一次搜尋求得乙個最大值賦給dp,搜到乙個點就標記乙個點以後就不需要在搜了可以直接用這個點的值,每個點會有四個方向的路,我們從這四條路中挑出來一條最長的賦給dp返回出去(相信大家也發現了這裡用到了dp)。我們從這四種情況中挑乙個最大值加上當前點的值賦給dp[當前點]就ok了。
//#include#include#includeusing namespace std;
const int maxn = 105;
const int inf = 0x3f3f3f3f;
const int dx = ;
const int dy = ;
int n, m, a[maxn][maxn], dp[maxn][maxn], ans;
int dfs(int x, int y)
}dp[x][y] = max + 1;
return dp[x][y];
}int main()
}int ans = 0;
for (int i = 0; i < n; i++)
}printf("%d\n", ans);
return 0;}/*
5 51 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
*/
POJ 1088 滑雪 記憶化搜尋
滑雪 time limit 1000ms memory limit 65536k total submissions 79619 accepted 29637 description michael喜歡滑雪百這並不奇怪,因為滑雪的確很刺激。可是為了獲得速度,滑的區域必須向下傾斜,而且當你滑到坡底,你...
POJ 1088 滑雪 記憶化搜尋
description michael喜歡滑雪百這並不奇怪,因為滑雪的確很刺激。可是為了獲得速度,滑的區域必須向下傾斜,而且當你滑到坡底,你不得不再次走上坡或者等待公升降機來載你。michael想知道載乙個區域中最長底滑坡。區域由乙個二維陣列給出。陣列的每個數字代表點的高度。下面是乙個例子 1 2 ...
POJ 1088 滑雪 記憶化搜尋
滑雪 time limit 1000ms memory limit 65536k total submissions 84463 accepted 31618 description michael喜歡滑雪百這並不奇怪,因為滑雪的確很刺激。可是為了獲得速度,滑的區域必須向下傾斜,而且當你滑到坡底,你...