OJ 1088 滑雪__动态规划
来源:互联网 发布:js 转动效果 编辑:程序博客网 时间:2024/06/06 17:11
描述
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更长。事实上,这是最长的一条。
输入
输入的第一行表示区域的行数R和列数C(1 <= R,C <= 100)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。
输出
输出最长区域的长度。
样例输入
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
分析
对每个点递归求解最大长度,遍历完成后最大点就出来了。
实现
#include <iostream>#include <cstring>#include <algorithm>using namespace std;int a[102][102];//状态:len[i][j]表示从(i,j)点从能走的最大长度int len[102][102];int maxLen = 1;int calcPathLength(int i, int j, int lastRow, int lastColumn){ if (len[i][j] || (i <= 0 || i >= lastRow || j <= 0 || j >= lastColumn)) { return len[i][j]; } //状态转移方程 int tempLen[4]; tempLen[0] = a[i][j] > a[i][j - 1] ? calcPathLength(i, j - 1, lastRow, lastColumn) + 1 : 1; tempLen[1] = a[i][j] > a[i - 1][j] ? calcPathLength(i - 1, j, lastRow, lastColumn) + 1 : 1; tempLen[2] = a[i][j] > a[i][j + 1] ? calcPathLength(i, j + 1, lastRow, lastColumn) + 1 : 1; tempLen[3] = a[i][j] > a[i + 1][j] ? calcPathLength(i + 1, j, lastRow, lastColumn) + 1 : 1; len[i][j] = *max_element(tempLen, tempLen + 4); if (len[i][j] > maxLen) { maxLen = len[i][j]; } return len[i][j];}int main(){// freopen("in.txt", "r", stdin); memset(len, 0, sizeof(len)); int r, c; cin >> r >> c; int lastRow = r + 1; int lastColumn = c + 1; for (int i = 1; i <= r; i++) { a[i][0] = a[i][lastColumn] = 0; for (int j = 1; j <= c; j++) { cin >> a[i][j]; } } for (int j = 0; j <= lastColumn; j++) { a[0][j] = a[lastRow][j] = 0; } for (int i = 1; i <= r; i++) { for (int j = 1; j <= c; j++) { calcPathLength(i, j, lastRow, lastColumn); } } cout << maxLen << endl; return 0;}
0 0
- OJ 1088 滑雪__动态规划
- 1088--滑雪 动态规划
- 动态规划 :POJ 1088 滑雪
- poj 1088 滑雪 动态规划
- POJ 1088 滑雪 -- 动态规划
- POJ 1088 滑雪 动态规划
- POJ-1088 滑雪 (动态规划)
- 【动态规划】[POJ 1088]滑雪
- poj 1088 滑雪 动态规划
- poj 1088 滑雪【动态规划】
- poj 1088 滑雪 动态规划
- [POJ](1088)滑雪 ---- 动态规划
- OJ 7215 简单的整数划分问题__动态规划
- OJ 7219 复杂的整数划分问题__动态规划
- Poj 1088 滑雪(经典动态规划)
- POJ 1088 滑雪问题 动态规划
- poj 1088滑雪 (动态规划)
- POJ 1088 滑雪(动态规划经典)
- C#第四章课后练习2
- Spring事务
- Win7+VisualStudio2013编译ARToolKit5.3源码
- 坐标系节点
- HDU - 1085 Holding Bin-Laden Captive!(硬币问题)
- OJ 1088 滑雪__动态规划
- Spring概述
- Pull方法解析XML
- CentOS安装jdk的三种方法
- CImage图像得到BYTE数组数据 - 8bit灰度图像
- Spring基础
- oracle高效sql
- SVPProgressHUD 与 MBProgressHUD
- UVA - 10763 Foreign Exchange