From b3fde2141625e3f600247a4cb1ac576858a4897e Mon Sep 17 00:00:00 2001 From: eat-swap Date: Mon, 16 May 2022 15:49:45 +0800 Subject: [PATCH] add: 220516 [cpp] --- cpp/2205/220516.cpp | 52 +++++++++++++++++++++++++++++++++++++++++ cpp/2205/CMakeLists.txt | 2 +- 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 cpp/2205/220516.cpp diff --git a/cpp/2205/220516.cpp b/cpp/2205/220516.cpp new file mode 100644 index 0000000..de2e23e --- /dev/null +++ b/cpp/2205/220516.cpp @@ -0,0 +1,52 @@ +#include +#include + +/** + * 1091. Shortest Path in Binary Matrix + * Given an n x n binary matrix grid, return the length of the shortest clear path in the matrix. If there is no clear path, return -1. + * + * A clear path in a binary matrix is a path from the top-left cell (i.e., (0, 0)) to the bottom-right cell (i.e., (n - 1, n - 1)) such that: + * + * All the visited cells of the path are 0. + * All the adjacent cells of the path are 8-directionally connected (i.e., they are different and they share an edge or a corner). + * The length of a clear path is the number of visited cells of this path. + */ + +class Solution { +public: + static int shortestPathBinaryMatrix(const std::vector>& grid) { + const int n = grid.size(); + if (grid[0][0]) + return -1; + + std::vector vis(n * n); + int queue[10003]; // Array emulated + int front = 0, rear = 1; // queue + + while (rear > front) { + int pos = queue[front++]; + // pos = x * n + y; + int depth = pos >> 16; + pos &= 0xFFFF; + int y = pos % n, x = pos / n; + + if (x == n - 1 && y == n - 1) { + return depth + 1; + } + + for (int i = -1; i <= 1; ++i) { + for (int j = -1; j <= 1; ++j) { + if ((!i && !j) || (x + i < 0 || x + i >= n || y + j < 0 || y + j >= n) || vis[pos + j + i * n] || grid[x + i][y + j]) + continue; + vis[pos + j + i * n] = true; + queue[rear++] = (pos + j + i * n) | ((1 + depth) << 16); + } + } + } + return -1; + } +}; + +int main() { + std::cout << Solution::shortestPathBinaryMatrix({{0,0,0},{1,1,0},{1,1,0}}); +} diff --git a/cpp/2205/CMakeLists.txt b/cpp/2205/CMakeLists.txt index e314c85..af82855 100644 --- a/cpp/2205/CMakeLists.txt +++ b/cpp/2205/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2205) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2205 220515.cpp) +ADD_EXECUTABLE(2205 220516.cpp)