題意:傳紙條,跟方格取數一樣,但是兩條路徑不能有重複的。
思路:還是一樣的走,但是x1跟x2不能相等,包括現在跟上一個狀態。
總結:看了題解,發現題解大多數都是邏輯不正確的,更有離譜的是陣列範圍都不加特判,陣列訪問越界但是可以ac的情況,資料太爛了,放個自以為正確的思路吧,發現之前自己提交的滿分程式碼也不是邏輯完全正確,放個自以為正確的程式碼。
void solve(){
int n, m;
cin >> n >> m;
vector<vector<int>> grid(n + 1, vector<int> (m + 1, 0));
for (int i = 1; i <= n; ++i){
for (int j = 1; j <= m; ++j){
cin >> grid[i][j];
}
}
vector<vector<vector<int>>> dp(2, vector<vector<int>> (n + 1, vector<int>(m + 1, 0)));
dp[0][1][1] = grid[1][1];
vector<vector<int>> zeros(n + 1, vector<int>(m + 1, 0));
int cur = 0;
for (int s = 1; s <= n + m - 2; ++s){
cur ^= 1;
for (int x1 = 1; x1 <= min(s + 1, n); ++x1){
for (int x2 = 1; x2 <= min(s + 1, n); ++x2){
if (x1 == x2 && (s != n + m - 2)){
continue;
}
int y1 = s + 2 - x1;
int y2 = s + 2 - x2;
if (max(y1, y2) > m){
continue;
}
if (x1 > 1 && x2 > 1){
dp[cur][x1][x2] = max(dp[cur][x1][x2], dp[!cur][x1 - 1][x2 - 1] + grid[x1][y1] + grid[x2][y2]);
}
if (x1 > 1 && y2 > 1 && (x1 - 1 != x2 || (x1 == 2 && s == 1) || s == n + m - 2)){
dp[cur][x1][x2] = max(dp[cur][x1][x2], dp[!cur][x1 - 1][x2] + grid[x1][y1] + grid[x2][y2]);
}
if (y1 > 1 && x2 > 1 && (x2 - 1 != x1 || (x2 == 2 && s == 1) || s == n + m - 2)){
dp[cur][x1][x2] = max(dp[cur][x1][x2], dp[!cur][x1][x2 - 1] + grid[x1][y1] + grid[x2][y2]);
}
if (y1 > 1 && y2 > 1){
dp[cur][x1][x2] = max(dp[cur][x1][x2], dp[!cur][x1][x2] + grid[x1][y1] + grid[x2][y2]);
}
}
}
}
// for (int i = 1; i <= n; ++i){
// for (int j = 1; j <= m; ++j){
// cout <<dp[cur][i][j] << " \n"[j == m];
// }
//}
cout << dp[cur][n][m] << '\n';
}