poj 1088

一道很经典的带记忆的搜索题目。。。。。。

/* poj 1088 Wrote by Dream Chen*/
#include <iostream>
using namespace std;

int g_row = 0;
int g_col = 0;

int DSearch(int i, int j);
int a[110][110];
int cnt[110][110];
int main()
{
	cin >> g_row >> g_col;
	for (int i = 0; i < g_row; ++i)
		for (int j = 0; j < g_col; ++j)
		{
			cin >> a[i][j];
			cnt[i][j] = 0;
		}
		int max_num = 0;
		for (int i = 0; i < g_row; ++i)
			for (int j = 0; j < g_col; ++j)
			{
				cnt[i][j] = DSearch(i,j);
			}
		for (int i = 0; i < g_row; ++i)
			for (int j = 0; j < g_col; ++j)
			{
				if (max_num < cnt[i][j])
				{
					max_num = cnt[i][j];
				}
			}
		cout << max_num;	
		return 0;
}


int DSearch(int i, int j)
{
	if (cnt[i][j] > 0)
	{
		return cnt[i][j];
	}
	int temp_max = 0;
	if (i-1 >= 0 && a[i-1][j] < a[i][j])
	{
		int res1 = DSearch(i-1,j);
		if (res1 > temp_max)
		{
			temp_max = res1;
		}
	}
	if (i+1 < g_row && a[i+1][j] < a[i][j])
	{
		int res2 = DSearch(i+1,j);
		if (res2 > temp_max)
		{
			temp_max = res2;
		}
	}
	if (j-1 >= 0 && a[i][j-1] < a[i][j])
	{
		int res3 = DSearch(i,j-1);
		if (res3 > temp_max)
		{
			temp_max = res3;
		}
	}
	if (j+1 < g_col && a[i][j+1] < a[i][j])
	{
		int res4 = DSearch(i,j+1);
		if (res4 > temp_max)
		{
			temp_max = res4;
		}
	}
	return cnt[i][j] = temp_max+1;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值