poj 1088

http://acm.pku.edu.cn/JudgeOnline/problem?id=1088

 状态转移方程:

DP(i ,j) = max(DP(i - 1, j), DP(i  + 1,j), DP(i , j - 1), DP(i , j + 1)) +  1;

代码
//刚开始接触DP题,用起来也很不熟练,看了discuss写的
#include<stdio.h>

int row, column;
int dp[100][100]; //记录长度
int high[100][100]; //读入高度

int DP(int i, int j)
{
int max = 0;

if(dp[i][j] > 0)
return dp[i][j]; //已经处理过,直接返回

if(i-1 >= 0) //判断边界
if(high[i][j] > high[i-1][j]) //当前高度比临近点要高
if(max < DP(i-1, j))
max
= DP(i-1, j); //记录较长长度

if(i+1 < row) //注意此边界的判断
if(high[i][j] > high[i+1][j])
if(max < DP(i+1, j))
max
= DP(i+1, j);

if(j-1 >= 0)
if(high[i][j] > high[i][j-1])
if(max < DP(i, j-1))
max
= DP(i, j-1);

if(j+1 < column) //注意此边界的判断
if(high[i][j] > high[i][j+1])
if(max < DP(i, j+1))
max
= DP(i, j+1);

return dp[i][j] = max+1; //赋值给它,因为它比邻近点要高,对应dp加一
}

int main()
{
int i, j, longest;
while(scanf("%d%d", &row, &column) != EOF)
{
for(i=0; i<row; i++)
for(j=0; j<column; j++)
{
scanf(
"%d", &high[i][j]);
dp[i][j]
= 0;
}

for(i=0; i<row; i++)
for(j=0; j<column; j++)
DP(i, j);

longest
= 0;
for(i=0; i<row; i++)
for(j=0; j<column; j++)
if(longest < dp[i][j])
longest
= dp[i][j];

printf(
"%d\n", longest);
}
return 0;
}
原文地址:https://www.cnblogs.com/submarinex/p/1941253.html