codevs 2152 滑雪

mac2022-06-30  21

题目描述 Description trs喜欢滑雪。他来到了一个滑雪场,这个滑雪场是一个矩形,为了简便,我们用r行c列的矩阵来表示每块地形。为了得到更快的速度,滑行的路线必须向下倾斜。 例如样例中的那个矩形,可以从某个点滑向上下左右四个相邻的点之一。例如24-17-16-1,其实25-24-23…3-2-1更长,事实上这是最长的一条。

输入描述 Input Description 输入文件 第1行: 两个数字r,c(1<=r,c<=100),表示矩阵的行列。 第2..r+1行:每行c个数,表示这个矩阵。

输出描述 Output Description 输出文件 仅一行: 输出1个整数,表示可以滑行的最大长度。

样例输入 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<iostream> #include<cstdio> #include<cstring> using namespace std; int dt[233][233]; int r, c; int dpss[233][233]; const int fk = 214748364; int search(int x, int y, int s) { if(dpss[x][y] && dt[x][y] != fk) return dpss[x][y]; if(dt[x][y] > dt[x-1][y]) dpss[x][y] = max(dpss[x][y], s + search(x-1, y, 1)); if(dt[x][y] > dt[x][y-1]) dpss[x][y] = max(dpss[x][y], s + search(x, y-1, 1)); if(dt[x][y] > dt[x+1][y]) dpss[x][y] = max(dpss[x][y], s + search(x+1, y, 1)); if(dt[x][y] > dt[x][y+1]) dpss[x][y] = max(dpss[x][y], s + search(x, y+1, 1)); return dpss[x][y]; } int main() { scanf("%d%d", &r, &c); for(int i = 0; i <= r+1; i++) for(int j = 0; j <= c+1; j++) dt[i][j] = fk; for(int i = 1; i <= r; i++) for(int j = 1; j <= c; j++) { scanf("%d", &dt[i][j]); } int ans = 0; for(int i = 1; i <= r; i++) for(int j = 1; j <= c; j++) { if(!dpss[i][j]) dpss[i][j] = search(i, j, 1); ans = max(ans, dpss[i][j]); } printf("%d", ans + 1); return 0; }

转载于:https://www.cnblogs.com/Loi-Vampire/p/6017065.html

最新回复(0)