/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 336.0 KiB
#2 Wrong Answer 39ms 620.0 KiB
#3 Wrong Answer 113ms 65.5 MiB

Code

#include <bits/stdc++.h>

using namespace std;

// #include "debug.h"

#define int int64_t

const int inf = 1e16;

void solve() {
        int n, m, x, y;
        cin >> n >> m >> x >> y;

        vector<vector<char>> s(n, vector<char>(m));
        for (int i = 0; i < n; ++i) {
                for (int j = 0; j < m; ++j) {
                        cin >> s[i][j];
                }
        }

        vector<vector<array<int, 2>>> dp(n, vector<array<int, 2>>(m, {inf, inf}));
        if (s[0][0] != '#') {
                dp[0][0][0] = -1; // 0 means water from left
                dp[0][0][1] = -1; // 1 means water from top
        }

        for (int i = 0; i < n; ++i) {
                for (int j = 0; j < m; ++j) {
                        if (s[i][j] == '#') {
                                continue;
                        }
                        if (i - 1 >= 0) {
                                dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j][1] + x);
                                dp[i][j][1] = min(dp[i][j][1], dp[i - 1][j][0] + y);
                        }
                        if (j - 1 >= 0) {
                                dp[i][j][0] = min(dp[i][j][0], dp[i][j - 1][1] + y);
                                dp[i][j][0] = min(dp[i][j][0], dp[i][j - 1][0] + x);
                        }
                }
        }

        int ans = min(dp[n - 1][m - 1][0], dp[n - 1][m - 1][1]);

        cout << (ans >= inf ? -1 : ans) << '\n';
}

int32_t main() {
        ios_base::sync_with_stdio(false);
        cin.tie(0);

        int tc = 1;
        cin >> tc;

        while (tc--) {
                solve();
        }
}

Information

Submit By
Type
Submission
Problem
P1156 Low cost water management
Contest
Happy New Year 2025
Language
C++17 (G++ 13.2.0)
Submit At
2025-01-02 15:26:16
Judged At
2025-01-02 15:26:16
Judged By
Score
1
Total Time
113ms
Peak Memory
65.5 MiB