1015. 摘花生


https://www.acwing.com/problem/content/description/1017/

二维地图的dp,对于某一个而言,只能向右,或者向下,除了边界,每一个都继承最优的选项即可。

 1 #include
 2 using namespace std;
 3 typedef long long ll;
 4 ll t,r,c;
 5 const ll N=1e3+520;
 6 ll a[N][N];
 7 int main()
 8 {
 9     scanf("%lld",&t);
10     while(t--)
11     {
12         scanf("%lld%lld",&r,&c);
13         for(int i=1;i<=r;i++)
14         {
15             for(int j=1;j<=c;j++)
16             {
17                 scanf("%lld",&a[i][j]);
18                 if(i==1) a[i][j]+=a[1][j-1];
19                 else if(j==1) a[i][j]+=a[i-1][j];
20                 else a[i][j]+=max(a[i-1][j],a[i][j-1]); 
21             }
22         }
23         cout<'\n';
24     }
25     return 0;
26 }