题目链接: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 }

相关文章:

  • 2021-10-13
  • 2022-03-04
  • 2021-08-23
  • 2021-09-08
  • 2022-02-24
  • 2021-09-02
  • 2021-05-24
  • 2021-09-28
猜你喜欢
  • 2021-06-25
  • 2022-02-05
  • 2021-10-07
  • 2021-09-10
  • 2021-08-16
  • 2021-09-23
  • 2022-02-13
相关资源
相似解决方案