题目如下:(来源于http://acm.pku.edu.cn/JudgeOnline/problem?id=1088) 滑雪 DescriptionMichael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。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 Input5 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 Output25 SourceDon't know 这是一道很简单的题,用DP很容易就AC了。 以下是我的代码:#include <iostream> using namespace std; int height[100][100],maxLen[100][100]; int row,col; int maxLength(int i,int j) { if (maxLen[i][j] != 0)return maxLen[i][j]; int left,right,up,down,maxLR,maxUD; left = right = up = down = 1; if (j-1 >= 0) if (height[i][j-1] > height[i][j]) if (maxLen[i][j-1] != 0) left = maxLen[i][j-1]+1; else left = maxLength(i,j-1)+1; if (j+1 < col) if (height[i][j+1] > height[i][j]) if (maxLen[i][j+1] != 0) right = maxLen[i][j+1]+1; else right = maxLength(i,j+1)+1; if (i-1 >= 0) if (height[i-1][j] > height[i][j]) if (maxLen[i-1][j] != 0) up = maxLen[i-1][j]+1; else up = maxLength(i-1,j)+1; if (i+1 < row) if (height[i+1][j] > height[i][j]) if (maxLen[i+1][j] != 0) down = maxLen[i+1][j]+1; else down = maxLength(i+1,j)+1; maxLR = left > right ? left : right; maxUD = up > down ? up : down; return maxLen[i][j] = (maxLR > maxUD ? maxLR : maxUD); } int main() { int i,j,max = 0,temp; cin >> row >> col; for (i = 0; i < row; i++) for (j = 0; j < col; j++) cin >> height[i][j]; for (i = 0; i < row; i++) for (j = 0; j < col; j++) if (max < (temp = maxLength(i,j)) ) max = temp; cout << max << endl; return 0; }初涉DP,望高手指教!

评论