openjudge-NOI 2.6-2728 摘花生

题目链接:http://noi.openjudge.cn/ch0206/2728/

题解:
  某一个点只能从其左边或者上边走过来

  f[i][j]存储(i,j)这个点上的结果,即f[i][j]=max(f[i-1][j],f[i][j-1])+a[i][j]

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 #define N 110
 6 int a[N][N],f[N][N],n,m;
 7 int main()
 8 {
 9     int t;
10     scanf("%d",&t);
11     while(t--)
12     {
13         memset(f,0,sizeof(f));
14         scanf("%d%d",&n,&m);
15         for(int i=1;i<=n;i++)
16         {
17             for(int j=1;j<=m;j++)
18             {
19                 scanf("%d",&a[i][j]);
20             }
21         }
22         for(int i=1;i<=n;i++)
23         {
24             for(int j=1;j<=m;j++)
25             {
26                 if(i==1)f[i][j]=f[i][j-1]+a[i][j];
27                 else if(j==1)f[i][j]=f[i-1][j]+a[i][j];
28                 else f[i][j]=max(f[i-1][j],f[i][j-1])+a[i][j];
29             }
30         }
31         printf("%d\n",f[n][m]);
32     }
33     return 0;
34 }
posted @ 2016-10-20 18:49  xqmmcqs  阅读(353)  评论(0编辑  收藏  举报