poj 滑雪(记忆性动态规划)

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

从任意一点出发,可以滑行的最长距离.(从高往低走)

思路:列举每一个点 a(i,j) 初始时设置len[i][j] = 0

        从这个点上下左右四个方向走一下,找到最大的那个+1,就是我的最大距离了.如果这个点周围都比我大,我就是1,将这个点初始化好,若遇到这个点的len[i][j] != 0 直接返回值就好


 

// poj 滑雪.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;

const int maxn = 110;
int h[maxn][maxn];
int len[maxn][maxn];
int r,c;
int s[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};

void dp(int x,int y){

	if(len[x][y] != 0){
		return ;
	}

	int maxlen = 0;

	for(int i = 0;i<4;i++){
		int tx = x+ s[i][0];
		int ty = y+ s[i][1];

		if(tx<0 || tx>=r || ty < 0 || ty >= c)
			continue;

		if(h[tx][ty] < h[x][y] ){
			maxlen = max(maxlen,len[tx][ty]);
		}else
		    continue;

	}

	len[x][y] = maxlen+1;
}

int _tmain(int argc, _TCHAR* argv[])
{
	
	scanf("%d%d",&r,&c);
	fill(h[0],h[0]+r*c,0);
	fill(len[0],len[0]+r*c,0);

	for(int i = 0;i<r;i++){
		for(int j = 0;j<c;j++){
			scanf("%d",&h[i][j]);
		}
	}

	int maxl = 0;
	for(int i = 0;i<r;i++){
		for(int j = 0;j<c;j++){
			maxl = max (maxl,len[i][j]);
		}
	}
    printf("%d\n",maxl);
	system("pause");
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值