题目大意:给定一个nXm的矩阵,对于矩阵中的每个点1,下一个可以走的点2为点1的四个方向,且点2小于点1对应的数,求在矩阵中最多可以走多少步
解题思路:记忆化搜索每一个点,从该点开始的最大步长
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <queue>
#include <vector>
#include <list>
#include <stack>
#include <map>
#include <set>
using namespace std;
const int maxn = 100+10;
const int dir[4][2] = {{-1,0},{1,0},{0,1},{0,-1}};
int n,m;
int a[maxn][maxn],sum[maxn][maxn];
int dfs(int x,int y)
{
if(sum[x][y]>1) return sum[x][y];
for(int i = 0; i < 4; i++)
{
int xx = x+dir[i][0];
int yy = y+dir[i][1];
if(xx>=1 && xx<=n && yy>=1 && yy<=m && a[xx][yy] < a[x][y])
{
sum[x][y] = max(sum[x][y],dfs(xx,yy)+1);
}
}
return sum[x][y];
}
int main()
{
int ans;
while(scanf("%d%d",&n,&m) != EOF)
{
memset(sum,0,sizeof(sum));
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
scanf("%d",&a[i][j]);
ans = 0;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
ans = max(ans,dfs(i,j));
printf("%d\n",ans+1);
}
return 0;
}