一道很经典的带记忆的搜索题目。。。。。。
/* 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;
}