15分求助
查看原帖
15分求助
162196
伟大的王夫子楼主2020/11/14 19:07
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m;
ll a[1010][1010];
ll f[1010][1010][2], s[1010][1010];
ll calc(int x, int y) {
	return max(f[x][y][0], f[x][y][1]);
}
int main() {
	cin >> n >> m;
	for (register int i = 1; i <= n; ++i)
		for (register int j = 1; j <= m; ++j)
			cin >> a[i][j], f[i][j][0] = f[i][j][1] = -1e12;
	f[1][1][1] = a[1][1];
	for (register int j = 1; j <= m; ++j) {
		for (register int i = 1; i <= n; ++i) s[i][j] = s[i - 1][j] + a[i][j];
		if (j == 1) {
			for (register int i = 1; i <= n; ++i) f[i][j][0] = s[i][j];
			continue;
		}
		for (register int i = 1; i <= n; ++i) {
			f[i][j][0] = f[i - 1][j][0] + a[i][j];
			f[i][j][0] = calc(i, j - 1) + a[i][j];
			for (register int k = 1; k <= n; ++k) {
				f[i][j][0] = max(f[i][j][0], f[k][j - 1][1] + s[k - 1][j - 1] + s[i][j]);
			}
		}
		for (register int i = n; i >= 1; --i) {
			f[i][j][1] = f[i + 1][j][1] + a[i][j];
			f[i][j][1] = calc(i, j - 1) + a[i][j];
			for (register int k = 1; k <= n; ++k)
				f[i][j][1] = max(f[i][j][1], f[k][j - 1][0] + s[n][j - 1] - s[k][j - 1] + s[n][j] - s[i - 1][j]);
		}
		
	}
//	for (register int i = 1; i <= n; ++i) {
//		for (register int j = 1; j <= m; ++j)
//			if (f[i][j][0] != 0xcfcfcfcf) cout << f[i][j][0] << ' ';
//			else cout << "  ";
//		puts("");
//	}
//	for (register int i = 1; i <= n; ++i) {
//		for (register int j = 1; j <= m; ++j)
//			if (f[i][j][1] != -3472328296227680305) cout << f[i][j][1] << ' ';
//			else cout << "  ";
//		puts("");
//	}	
//	cout << f[n][m][0] << ' ' << f[n][m][1];
	cout << max(f[n][m][0], f[n][m][1]);
}
2020/11/14 19:07
加载中...