标签:记忆化搜索
#include <cstdio> #include <iostream> #include <algorithm> #include <queue> #include <stack> #include <cstdlib> #include <cmath> #include <set> #include <map> #include <vector> #include <cstring> #define INF 100000000 using namespace std; int v[][2] = {{1,0},{0,1},{-1,0},{0,-1}}; int dp[105][105]; int ma[105][105]; int fun(int x,int y){ if(dp[x][y]) return dp[x][y]; dp[x][y] = 1; for(int i = 0;i < 4;i++){ int vx = x + v[i][0]; int vy = y + v[i][1]; if(ma[vx][vy] != -1 && ma[vx][vy] < ma[x][y]){ dp[x][y] = max(fun(vx,vy)+1,dp[x][y]); } } return dp[x][y]; } int main(){ int n,m; while(cin >> n >> m){ for(int i = 0;i <= n+1;i++){ for(int j = 0;j <= m+1;j++){ ma[i][j] = -1; } } for(int i = 1;i <= n;i++){ for(int j = 1;j <= m;j++){ scanf("%d",&ma[i][j]); } } memset(dp,0,sizeof(dp)); int ans = 0; for(int i = 1;i <= n;i++){ for(int j = 1;j <= m;j++){ ans = max(ans,fun(i,j)); } } cout << ans << endl; } return 0; }
标签:记忆化搜索
原文地址:http://blog.csdn.net/qq_24667639/article/details/45486835