题目传送门
一开始没看到题目下面的说明(只能走直线),推了好长时间没推出来.f[i][j]表示从(0,0)到(i,j)的最大矿数.
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int n,m,a[501][501],b[501][501],ans; int f[501][501]; int main() { while(true) { memset(f,0,sizeof(f)); scanf("%d%d",&n,&m); if(n == 0 && m == 0) return 0; for(int i = 1;i <= n; i++) for(int j = 1;j <= m; j++) { scanf("%d",&a[i][j]); a[i][j] += a[i][j-1]; } for(int i = 1;i <= n; i++) for(int j = 1;j <= m; j++) { scanf("%d",&b[i][j]); b[i][j] += b[i-1][j]; } for(int i = 1;i <= n; i++) for(int j = 1;j <= m; j++) f[i][j] = max(f[i-1][j] + a[i][j],f[i][j-1] + b[i][j]); printf("%d ",f[n][m]); } return 0; }