Uva10285 Longest Run on a Snowboard

记忆化搜索模板题.

仔细想想搜索时会重复的地方, 然后把重复的地方记下来就好啦!

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <cstring>
 5 using namespace std;
 6 const int MAXC = 100 + 10;
 7 const int INF = 0x3f3f3f3f;
 8 
 9 inline int read()
10 {
11     int x = 0; char ch = getchar();
12     while(!isdigit(ch)) ch = getchar();
13     while(isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
14     return x;
15 }
16 
17 int M[MAXC][MAXC], d[MAXC][MAXC], dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; 
18 int R, C;
19 
20 int dfs(int x, int y)
21 {
22     if(d[x][y] != -1) return d[x][y];
23 
24     d[x][y] = 0;
25     for(int i = 0; i < 4; i++)
26         if(M[x + dx[i]][y + dy[i]] < M[x][y])
27             d[x][y] = max(dfs(x + dx[i], y + dy[i]), d[x][y]);
28 
29     return ++d[x][y];
30 }
31 
32 int main()
33 {
34     //freopen("10285.in", "r", stdin);
35     int N;
36     cin>>N;
37 
38     string s;
39 
40     while(N--)
41     {
42         cin>>s;
43         R = read(), C = read();
44 
45         memset(M, 0, sizeof(M));
46         memset(d, -1, sizeof(d));
47         for(int i = 1; i <= R; i++)
48             for(int j = 1; j <= C; j++)
49                 M[i][j] = read();
50 
51         for(int i = 1; i <= R; i++)
52             {for(int j = 1; j <= C; j++)
53                 cout<<M[i][j]<<" ";
54             cout<<endl;}
55 
56         int ans = -INF;
57         for(int i = 1; i <= R; i++)
58             for(int j = 1; j <= C; j++)
59                 ans = max(dfs(i, j), ans);
60 
61         cout<<s<<": "<<--ans<<endl;
62     }
63     return 0;
64 }

做水题真好玩!

 

posted @ 2018-05-11 15:26  俺是小程  阅读(143)  评论(0编辑  收藏  举报