Compile Error
foo.cc:5:10: fatal error: debug.h: No such file or directory 5 | #include "debug.h" | ^~~~~~~~~ compilation terminated.
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];
}
}
if (s[0][0] == '#') {
cout << -1 << '\n';
return;
}
vector<vector<array<int, 3>>> dp(n, vector<array<int, 3>>(m, {inf, inf, inf}));
if (s[0][1] != '#') {
dp[0][1][0] = 0;
}
if (s[1][0] != '#') {
dp[1][0][2] = 0;
}
queue<array<int, 3>> Q;
Q.push({0, 1, 0});
Q.push({1, 0, 2});
while (!Q.empty()) {
auto [i, j, k] = Q.front();
Q.pop();
if (s[i][j] == '#') {
continue;
}
if (k == 0) {
// down or right
if (i + 1 < n && dp[i + 1][j][2] > dp[i][j][k] + y) {
dp[i + 1][j][2] = dp[i][j][k] + y;
Q.push({i + 1, j, 2});
}
if (j + 1 < m && dp[i][j + 1][0] > dp[i][j][k] + x) {
dp[i][j + 1][0] = dp[i][j][k] + x;
Q.push({i, j + 1, 0});
}
} else if (k == 1) {
// down or left
if (i + 1 < n && dp[i + 1][j][2] > dp[i][j][k] + y) {
dp[i + 1][j][2] = dp[i][j][k] + y;
Q.push({i + 1, j, 2});
}
if (j - 1 >= 0 && dp[i][j - 1][1] > dp[i][j][k] + x) {
dp[i][j - 1][1] = dp[i][j][k] + x;
Q.push({i, j - 1, 1});
}
} else {
// down, left, right
if (i + 1 < n && dp[i + 1][j][2] > dp[i][j][k] + x) {
dp[i + 1][j][2] = dp[i][j][k] + x;
Q.push({i + 1, j, 2});
}
if (j - 1 >= 0 && dp[i][j - 1][1] > dp[i][j][k] + y) {
dp[i][j - 1][1] = dp[i][j][k] + y;
Q.push({i, j - 1, 1});
}
if (j + 1 < m && dp[i][j + 1][0] > dp[i][j][k] + y) {
dp[i][j + 1][0] = dp[i][j][k] + y;
Q.push({i, j + 1, 0});
}
}
}
int ans = min(dp[n - 1][m - 1][0], dp[n - 1][m - 1][2]);
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++11 (G++ 13.2.0)
- Submit At
- 2025-01-02 16:49:13
- Judged At
- 2025-01-02 16:49:13
- Judged By
- Score
- 0
- Total Time
- 0ms
- Peak Memory
- 0 Bytes