Submission

Status:

[PP-SSSSSSSSSSSS]

Subtask/Task Score:

{0/100}

Score: 0

User: kittipos

Problemset: อัศวินขี่ม้าขาว

Language: cpp

Time: 0.002 second

Submitted On: 2026-03-09 13:21:32

#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 INT_MAX / 2;
    }
    if (x == w-1 && y == h-1) {
        return (min(0, space[y][x]) * -1) + 1;
    }

    if (dp[x][y] != -1) return dp[x][y];
    
    int c1 = solve(x+1, y);
    int c3 = solve(x, y+1);

    int ans = min({c1, c3}) + (-1) * 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 + 1;

    return 0;
}