【HDU】 1078 FatMouse and Cheese
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078
和poj的滑雪本质是一样的,增加了一点难度之处在于:fatmouse可以走的路径多了... 但这个的处理还是很简单的。
具体见代码:
FatMouse and Cheese
1 /* PRO: FatMouse and Cheese 2 TIME: 8.18/2012 3 记忆化深搜... 4 */ 5 #include <iostream> 6 #include <cstdio> 7 #include <cstdlib> 8 #include <cstring> 9 #include <map> 10 using namespace std; 11 const int MAX = 1000; 12 int matrix[MAX][MAX], dp[MAX][MAX]; 13 int dx[]={1,-1,0,0}, dy[]={0,0,1,-1}; 14 int n, k; 15 16 int dfs(int x, int y) 17 { 18 if(dp[x][y]) //灵魂!路径记录后,可减少重复递归 19 return dp[x][y]; 20 int tx, ty, i, j, p, tmp, maxi = 0; 21 for(p=0; p<4; ++p){ 22 for(i=1; i<=k; ++i){ 23 tx = x+dx[p]*i; 24 ty = y+dy[p]*i; 25 if(tx<0 || ty<0 || tx>=n || ty>=n) continue; 26 if(matrix[tx][ty]<=matrix[x][y]) continue; 27 tmp = dfs(tx,ty); 28 if(maxi<tmp) maxi = tmp; 29 } 30 } 31 dp[x][y] = maxi+matrix[x][y]; 32 return dp[x][y]; 33 } 34 35 int main() 36 { 37 int i, j; 38 while(scanf("%d %d",&n,&k)!=EOF, n!=-1 && k!=-1){ 39 for(i=0; i<n; ++i){ 40 for(j=0; j<n; ++j) 41 scanf("%d",&matrix[i][j]); 42 } 43 memset(dp,0,sizeof(dp)); 44 printf("%d\n",dfs(0,0)); 45 } 46 return 0; 47 }

浙公网安备 33010602011771号