题意:你可以往下走,也可以往右走,然后问你从1,1走到n,m,求路过的和最大可以为多少
思路: dp 注意初始化,有负数
代码:
1 #include2 using namespace std; 3 typedef long long ll; 4 #define mem(a) memset(a,0,sizeof(a)) 5 #define mp(x,y) make_pair(x,y) 6 const int maxn = 1e3+10; 7 const int INF = 0x3f3f3f3f; 8 const ll INFLL = 0x3f3f3f3f3f3f3f3fLL; 9 inline ll read(){10 ll x=0,f=1;char ch=getchar();11 while(ch<'0'||ch>'9'){ if(ch=='-')f=-1;ch=getchar();}12 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}13 return x*f;14 }15 //16 17 int a[maxn][maxn],dp[maxn][maxn];18 19 int main(){20 int T=read();21 for(int cas=1; cas<=T; cas++){22 int n=read(),m=read();23 for(int i=1; i<=n; i++)24 for(int j=1; j<=m; j++)25 a[i][j] = read();26 for(int i=0; i<=n; i++)27 for(int j=0; j<=m; j++)28 dp[i][j] = -INF;29 dp[0][1] = 0,dp[1][0] = 0;30 for(int i=1; i<=n; i++)31 for(int j=1; j<=m; j++){32 // if(i==1)33 // dp[i-1][j] = -INF;34 // if(j==1)35 // dp[i][j-1] = -INF;36 // if(i==1 && j==1)37 // dp[i-1][j] = 0;38 dp[i][j] = max(dp[i-1][j],dp[i][j-1]) + a[i][j];39 }40 cout << "Case " << cas << ": " << dp[n][m] << endl;41 }42 43 return 0;44 }45 46 //http://codeforces.com/gym/100500/attachments