POJ 1088 - 滑雪(动态规划)

点击打开链接(NOI省选题)

记忆化搜索

WA了一次:边界时return d[x][y] = 1; 没有跟ans比较,导致特殊情况ans等于0。

改法是将ans比较放到solve里跟最后dfs返回值比较。ans = max ( ans, dfs ( i, j ) );


状态转移:d[x][y] = max ( d[x][y], dfs ( nx, ny ) + 1 );


//	poj 1088
//  [6/11/2014 wind]
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAX_RC = 100;
int row, cow;
int a[MAX_RC][MAX_RC];

int d[MAX_RC][MAX_RC];

int dx[] = { -1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
int ans = 0;

int inline in ( int x, int y )
{
	return 0 <= x && x < row && 0 <= y && y < cow;
}

int dfs ( int x, int y )
{
	if ( d[x][y] )
	{
		return d[x][y];
	}

	bool hasNext = false;

	for ( int i = 0; i < 4; i++ )
	{
		int nx = x + dx[i], ny = y + dy[i];
		if ( in ( nx, ny )  && a[nx][ny] < a[x][y] )
		{
			d[x][y] = max ( d[x][y], dfs ( nx, ny ) + 1 );
			//ans = max ( ans, d[x][y] );
			hasNext = true;
		}
	}

	if ( !hasNext )
		return d[x][y] = 1;

	return d[x][y];
}

void solve()
{
	for ( int i = 0; i < row; i++ )
	{
		for ( int j = 0; j < cow; j++ )
		{
			ans = max ( ans, dfs ( i, j ) );
		}
	}
}

int main()
{
	//freopen ( "in.txt", "r", stdin );
	scanf ( "%d%d", &row, &cow );

	for ( int i = 0; i < row; i++ )
	{
		for ( int j = 0; j < cow; j++ )
		{
			scanf ( "%d", &a[i][j] );
		}
	}

	solve();

	printf ( "%d\n", ans );

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值