diff --git a/cpp/2204/220428.cpp b/cpp/2204/220428.cpp new file mode 100644 index 0000000..f06af47 --- /dev/null +++ b/cpp/2204/220428.cpp @@ -0,0 +1,122 @@ +#include +#include +#include + +// This is wrong. +// It has nothing to do with Dijkstra. +class SolutionWrong { +public: + static int minimumEffortPath(const std::vector>& heights) { + const std::size_t m = heights.size(), n = heights.front().size(), total = m * n; + + std::vector d(total, 0x7FFFFFFF); + std::vector> G(total); + std::vector vis(total); + for (int i = 0; i < m * n; ++i) { + // Build graph + if (i + n < total) { + // Down + G[i].push_back(i + n); + G[i + n].push_back(i); + } + if ((i + 1) % n) { + // Right + G[i].push_back(i + 1); + G[i + 1].push_back(i); + } + } + + // Pair + std::priority_queue, std::vector>, std::greater<>> q; + q.push({0, 0}); + d[0] = 0; + + // Dijkstra + while (!q.empty()) { + const auto [dist, idx] = q.top(); + q.pop(); + + if (vis[idx]) + continue; + vis[idx] = true; + + for (int idxNext : G[idx]) { + int length = std::abs(heights[idx / n][idx % n] - heights[idxNext / n][idxNext % n]); + if (d[idxNext] > d[idx] + length) { + d[idxNext] = d[idx] + length; + q.push({d[idxNext], idxNext}); + } + } + } + + return d[total - 1]; + } +}; + +/** + * 1631. Path With Minimum Effort + * You are a hiker preparing for an upcoming hike. You are given heights, a 2D array of size rows x columns, where heights[row][col] represents the height of cell (row, col). You are situated in the top-left cell, (0, 0), and you hope to travel to the bottom-right cell, (rows-1, columns-1) (i.e., 0-indexed). You can move up, down, left, or right, and you wish to find a route that requires the minimum effort. + * A route's effort is the maximum absolute difference in heights between two consecutive cells of the route. + * Return the minimum effort required to travel from the top-left cell to the bottom-right cell. + * + * I was wrong again, it IS Dijkstra, but with some modification. + * d[idx] + length --> std::max(d[idx], length) + */ + +class Solution { +public: + static int minimumEffortPath(const std::vector>& heights) { + const std::size_t m = heights.size(), n = heights.front().size(), total = m * n; + + std::vector d(total, 0x7FFFFFFF); + std::vector> G(total); + std::vector vis(total); + for (int i = 0; i < m * n; ++i) { + // Build graph + if (i + n < total) { + // Down + G[i].push_back(i + n); + G[i + n].push_back(i); + } + if ((i + 1) % n) { + // Right + G[i].push_back(i + 1); + G[i + 1].push_back(i); + } + } + + // Pair + std::priority_queue, std::vector>, std::greater<>> q; + q.push({0, 0}); + d[0] = 0; + + // Dijkstra + while (!q.empty()) { + const auto [dist, idx] = q.top(); + q.pop(); + + if (vis[idx]) + continue; + vis[idx] = true; + + for (int idxNext : G[idx]) { + int length = std::abs(heights[idx / n][idx % n] - heights[idxNext / n][idxNext % n]); + if (d[idxNext] > std::max(d[idx], length)) { + d[idxNext] = std::max(d[idx], length); + q.push({d[idxNext], idxNext}); + } + } + } + + return d[total - 1]; + } +}; + +int main() { + std::vector> args { + {1, 2, 2}, + {3, 8, 2}, + {5, 3, 5} + }; + std::cout << Solution::minimumEffortPath(args) << std::endl; +} diff --git a/cpp/2204/CMakeLists.txt b/cpp/2204/CMakeLists.txt index ab3b438..bec29b7 100644 --- a/cpp/2204/CMakeLists.txt +++ b/cpp/2204/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2204) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2204 220428-CN.cpp) +ADD_EXECUTABLE(2204 220428.cpp)