思路:dp[i][j]表示从(i,j)到(h,w)中当前减去对手的最大值,显然答案为(1,1),那么当前已经得到了a[i][j],那么对手一定会往大取,显然dp[i][j] = -max(dp[i][j+1],dp[i+1][j]) + a[i][j]
参考了青烟大佬的题解
代码如下:
#include <bits/stdc++.h> using namespace std; const int N = 505; int h,w; typedef long long ll; int a[N][N]; int f[N][N]; void solve(){ cin >> h >> w; memset(f,0x3f,sizeof f); //memset(vis,0,sizeof vis); for(int i=1;i<=h;i++){ for(int j=1;j<=w;j++) { cin >> a[i][j]; } } f[h][w] = a[h][w]; for(int i=h;i>=1;i--) for(int j=w;j>=1;j--) { if(i == h && j == w) continue; if(i != h) f[i][j] = min(f[i][j],a[i][j] - f[i+1][j]); if(j != w) f[i][j] = min(f[i][j],a[i][j] - f[i][j+1]); } cout << f[1][1] << endl; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while(t--){ solve(); } return 0; }