码迷,mamicode.com
首页 > 其他好文 > 详细

hdu1078FatMouse and Cheese

时间:2017-03-30 17:29:32      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:scan   amp   target   max   return   for   cstring   ret   lan   

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1078

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 const int maxn=1010;
 6 int dp[maxn][maxn];
 7 int p[maxn][maxn];
 8 int n,k;
 9 int dir[4][2]={0,1,0,-1,1,0,-1,0};
10 
11 int dfs(int x,int y)
12 {
13     int ans=0;
14     int xx,yy;
15     if(!dp[x][y])
16     {
17         for(int i=1;i<=k;i++)   //最多走k步
18         {
19             for(int j=0;j<4;j++)
20             {
21                  xx=x+dir[j][0]*i;
22                  yy=y+dir[j][1]*i;
23                 if(xx>=1&&yy>=1&&xx<=n&&yy<=n&&p[xx][yy]>p[x][y]) 
24                     ans=max(ans,dfs(xx,yy));
25             }
26         }
27         dp[x][y]=ans+p[x][y];
28     }
29     return dp[x][y];
30 }
31 int main()
32 {
33     while(scanf("%d%d",&n,&k)&&(n!=-1&&k!=-1))
34     {
35         for(int i=1;i<=n;i++)
36             for(int j=1;j<=n;j++)
37             scanf("%d",&p[i][j]);
38         memset(dp,0,sizeof(dp));
39         printf("%d\n",dfs(1,1));
40     }
41 }

 

hdu1078FatMouse and Cheese

标签:scan   amp   target   max   return   for   cstring   ret   lan   

原文地址:http://www.cnblogs.com/yijiull/p/6646772.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!