【POJ】2352 滑雪
题目链接:http://poj.org/problem?id=1088
网上的版本评述这个题目....各种简单啊!!... 为啥我还是wa了那么多次,不知会不会有人和我一样犯二呢?
最长的路径不一定是从最高点下滑或者从最低点上滑的.... = =
记忆化搜索的简单题目,ok,不多讲了~
滑雪
1 /* 2 PRO. POJ 1088 3 AUT. yuna 4 TIM. 8.10/2012 5 */ 6 #include <iostream> 7 #include <cstdio> 8 #include <cstdlib> 9 #include <cstring> 10 using namespace std; 11 const int MAXN = 110; 12 int dp[MAXN][MAXN], matrix[MAXN][MAXN], sx[MAXN], sy[MAXN], r, c; 13 int dx[]={1,-1,0,0}, dy[]={0,0,1,-1}; 14 15 int dfs(int x, int y) 16 { 17 if(dp[x][y] != -1) return dp[x][y]; //我个人认为这里是记忆化搜索的灵魂所在!(1) 18 19 int tx, ty, i, j, tmp, maxi = 0; 20 for(i=0; i<4; ++i){ 21 tx = x+dx[i]; 22 ty = y+dy[i]; 23 if(tx<0 ||ty<0 || tx>=r || ty>=c) continue; 24 if(matrix[tx][ty] >= matrix[x][y]) continue; 25 tmp = dfs(tx,ty); 26 if(tmp>maxi) maxi = tmp; 27 } 28 dp[x][y] = maxi + 1; //我个人认为这里是记忆化搜索的灵魂所在!(2) 29 return dp[x][y]; 30 } 31 32 int main() 33 { 34 int i, j, tmax, maxi, cnt; 35 while(~scanf("%d %d",&r,&c)){ 36 tmax = 0; 37 cnt = 0; 38 for(i=0; i<r; ++i) 39 for(j=0; j<c; ++j){ 40 scanf("%d",&matrix[i][j]); 41 if(tmax<matrix[i][j]){ 42 tmax = matrix[i][j]; 43 cnt = 0; 44 sx[cnt] = i; sy[cnt++] = j; 45 } 46 else if(tmax == matrix[i][j]){ 47 sx[cnt] = i; sy[cnt++] = j; 48 } 49 } 50 51 maxi = 0; 52 tmax = 0; 53 memset(dp,-1,sizeof(dp)); 54 for(i=0; i<cnt; ++i){ 55 tmax = dfs(sx[i],sy[i]); 56 if(tmax>maxi) maxi = tmax; 57 } 58 printf("%d\n",maxi); 59 } 60 return 0; 61 }

浙公网安备 33010602011771号