poj 1088

题目:http://poj.org/problem?id=1088

记忆化搜索,dp[r][c] = max(dp[r - 1][c] , dp[r + 1][c] , dp[r][c - 1] , dp[r][c + 1]) + 1 ( if (题目给的条件满足))

View Code
 1 using namespace std;
 2 typedef long long ll;
 3 const int N = 110;
 4 int map[N][N];
 5 int dp[N][N];
 6 int n,m;
 7 int dfs(int r, int c)  // 四个方向深搜
 8 {
 9     if(dp[r][c] != 0)  return dp[r][c];  // 如果不为 0 表示这个点已经算过了
10     int maxx = 1;
11     int tem;
12     if(r - 1 >= 0 && map[r][c] > map[r - 1][c])
13     {
14         tem = dfs(r - 1,c) + 1;
15         if(tem > maxx) maxx = tem;
16     }
17     if(r + 1 < n && map[r][c] > map[r + 1][c])
18     {
19         tem = dfs(r + 1,c) + 1;
20         if(tem > maxx) maxx = tem;
21     }
22     if(c - 1 >= 0 && map[r][c] > map[r][c - 1])
23     {
24         tem = dfs(r,c - 1) + 1;
25         if(tem > maxx) maxx = tem;
26     }
27     if(c + 1 < m && map[r][c] > map[r][c + 1])
28     {
29         tem = dfs(r,c + 1) + 1;
30         if(tem > maxx) maxx = tem;
31     }
32     return dp[r][c] = maxx;
33 }
34 int main()
35 {
36     int i,j;
37     //freopen("data.txt","r",stdin);
38     while(scanf("%d%d",&n,&m) != EOF)
39     {
40         for(i = 0; i < n; i++)
41         {
42             for(j = 0; j < m; j++)
43             scanf("%d",&map[i][j]);
44         }
45         _clr(dp,0);
46         int maxx = -1;
47         for(i = 0; i < n; i++)
48         {
49             for(j = 0; j < m; j++)
50             {
51                 dp[i][j] = dfs(i,j);
52                 if(maxx < dp[i][j]) maxx = dp[i][j];
53             }
54         }
55         printf("%d\n",maxx);
56     }
57     return 0;
58 }
原文地址:https://www.cnblogs.com/fxh19911107/p/2676308.html