Submission
Status:
[PPPPPPPPPPPPPPP]
Subtask/Task Score:
{100/100}
Score: 100
User: kittipos
Problemset: อัศวินขี่ม้าขาว
Language: cpp
Time: 0.062 second
Submitted On: 2026-03-09 13:24:28
#include <bits/stdc++.h>
using namespace std;
int h, w;
vector<vector<int>> space;
vector<vector<int>> dp;
int solve(int x, int y) {
if (x >= w || y >= h || x < 0 || y < 0) {
return 1e9;
}
if (x == w-1 && y == h-1) {
return max(1, 1 - space[y][x]);
}
if (dp[x][y] != -1) return dp[x][y];
int c1 = solve(x+1, y);
int c3 = solve(x, y+1);
int ans = max(1, min(c1, c3) - space[y][x]);
dp[x][y] = ans;
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> h >> w;
dp.assign(w, vector<int>(h, -1));
space.assign(h, vector<int>(w));
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> space[i][j];
}
}
int res = solve(0, 0);
cout << res;
return 0;
}