标签:des style blog http io color ar os for
Description
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
Input
Output
Sample Input
5 5 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9
Sample Output
25
小白上的记忆化搜索
int d(int i, int j)
{
if(d[i][j] >= 0)
return d[i][j];
return d[i][j] = a[i][j] + (i == n ? 0 : d(i+1, j) >? d[i+1][j+1]);
}
同本题很想啊
弄一个dis[][]数组保存距离,然后看它4个方向能否继续走下去,能走下去,就一直递归下去
1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #include <algorithm> 5 6 const int MAXN = 110; 7 int arr[MAXN][MAXN]; 8 int dis[MAXN][MAXN]; 9 int mov[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; 10 int line, col; 11 12 bool test(int x, int y) 13 { 14 if(x >= 0 && x < line && y >= 0 && y < col) 15 return true; 16 return false; 17 } 18 19 int d(int x, int y) 20 { 21 int tmp; 22 if(dis[x][y]) 23 return dis[x][y]; 24 25 for(int i = 0; i < 4; ++i) 26 { 27 int xx = x + mov[i][0]; 28 int yy = y + mov[i][1]; 29 if(test(xx, yy)) 30 { 31 if(arr[x][y] > arr[xx][yy]) 32 { 33 tmp = d(xx, yy); 34 dis[x][y] = dis[x][y] > tmp ? dis[x][y] : tmp + 1; 35 } 36 } 37 } 38 return dis[x][y]; 39 } 40 41 int main() 42 { 43 44 while(scanf("%d %d", &line, &col) != EOF) 45 { 46 for(int i = 0; i < line; i++) 47 for(int j = 0; j < col; j++) 48 scanf("%d", &arr[i][j]); 49 50 memset(dis, 0, sizeof(dis)); 51 52 int ans = 0, tmp; 53 for(int i = 0; i < line; i++) 54 { 55 for(int j = 0; j < col; j++) 56 { 57 tmp = d(i, j); 58 ans = ans > tmp ? ans : tmp; 59 } 60 } 61 62 printf("%d\n", ans + 1); 63 } 64 return 0; 65 }
标签:des style blog http io color ar os for
原文地址:http://www.cnblogs.com/ya-cpp/p/4075252.html