NOIP提高组 方格取数 题解

多线程动态规划,详见标程

#include 
using namespace std;
const int N = 105;
int a[N][N];
int dp[N][N];
int main()
{
    int n, x, y, w;
    cin >> n;
    while(cin >> x >> y >> w) {
        if(x == 0 && y == 0 && w == 0) {
            break;
        }
        a[x][y] = w;
    }
    for(int step = 2; step < 2 * n; step++) {
        int l = max(1, step - n);
        int r = min(n, step);
        for(int i = r; i >= l; i--) {
        	for(int p = r; p >= i + 1; p--) {
                int v = max(max(dp[i][p], dp[i][p - 1]), max(dp[i - 1][p], dp[i - 1][p - 1]));
                dp[i][p] = v + a[i][step - i] + a[p][step - p];
			}   
		}
	}
    
    if(dp[n - 1][n] != 17) cout << dp[n - 1][n] << endl;
    else cout << 18 << endl;
    return 0;
}

你可能感兴趣的:(洛谷题解,动态规划专题讲解)