#include<bits/stdc++.h>
#define inf 99999999999
using namespace std;
typedef long long ll;
ll arr[109][109];
ll dp[1009][109];
ll row,col;
ll solve(ll i,ll j)
{
if (i>=0&&i<row&&j>=0&&j<col)
{
if (dp[i][j]!=-1)
return dp[i][j];
ll ret=-inf;
ret=max(ret,solve(i+1,j)+arr[i][j]);
ret=max(ret,solve(i+1,j-1)+arr[i][j]);
ret=max(ret,solve(i+1,j+1)+arr[i][j]);
return dp[i][j]=ret;
}
else
return 0;
}
int main()
{
ll i,j,k,l,m,n,test;
cin>>test;
for(k=1;k<=test;k++)
{
cin>>row>>col;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
cin>>arr[i][j];
}
}
ll mx=-99999999;
for(i=0;i<col;i++)
{
memset(dp,-1,sizeof(dp));
mx=max(mx,solve(0,i));
}
cout<<mx<<endl;
}
return 0;
}
#define inf 99999999999
using namespace std;
typedef long long ll;
ll arr[109][109];
ll dp[1009][109];
ll row,col;
ll solve(ll i,ll j)
{
if (i>=0&&i<row&&j>=0&&j<col)
{
if (dp[i][j]!=-1)
return dp[i][j];
ll ret=-inf;
ret=max(ret,solve(i+1,j)+arr[i][j]);
ret=max(ret,solve(i+1,j-1)+arr[i][j]);
ret=max(ret,solve(i+1,j+1)+arr[i][j]);
return dp[i][j]=ret;
}
else
return 0;
}
int main()
{
ll i,j,k,l,m,n,test;
cin>>test;
for(k=1;k<=test;k++)
{
cin>>row>>col;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
cin>>arr[i][j];
}
}
ll mx=-99999999;
for(i=0;i<col;i++)
{
memset(dp,-1,sizeof(dp));
mx=max(mx,solve(0,i));
}
cout<<mx<<endl;
}
return 0;
}
No comments:
Post a Comment