From dc41a3c4d4d3257ca91441e26ae12534c9e67d54 Mon Sep 17 00:00:00 2001 From: Fenghe Xu Date: Wed, 1 Jun 2022 12:41:04 +0800 Subject: [PATCH 1/4] Create 473.Matchsticks-to-Square_v2.cpp --- .../473.Matchsticks-to-Square_v2.cpp | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 DFS/473.Matchsticks-to-Square/473.Matchsticks-to-Square_v2.cpp diff --git a/DFS/473.Matchsticks-to-Square/473.Matchsticks-to-Square_v2.cpp b/DFS/473.Matchsticks-to-Square/473.Matchsticks-to-Square_v2.cpp new file mode 100644 index 000000000..b7e13b405 --- /dev/null +++ b/DFS/473.Matchsticks-to-Square/473.Matchsticks-to-Square_v2.cpp @@ -0,0 +1,38 @@ +class Solution { + bool divide2(vector& sticks, int target) { + int n = sticks.size(); + for(int state = 0; state < (1<>i)&1) cur += sticks[i]; + } + if(cur == target) return true; + } + return false; + } +public: + bool makesquare(vector& matchsticks) { + int total = accumulate(matchsticks.begin(), matchsticks.end(), 0); + if(total % 4 != 0) return false; + + int n = matchsticks.size(); + + for(int state = 0; state < (1<>i)&1) cur += matchsticks[i]; + } + if(cur == total / 2) { + vector v1, v2; + for(int i = 0; i < n; ++i) { + if((state>>i)&1) v1.push_back(matchsticks[i]); + else v2.push_back(matchsticks[i]); + } + + if(divide2(v1, total / 4) && divide2(v2, total / 4)) return true; + } + } + + return false; + } +}; From 60dd7927b5329e28e16451307b3de680c0f63eb6 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Wed, 1 Jun 2022 23:13:57 -0700 Subject: [PATCH 2/4] Create 2290.Minimum-Obstacle-Removal-to-Reach-Corner.cpp --- ...nimum-Obstacle-Removal-to-Reach-Corner.cpp | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 BFS/2290.Minimum-Obstacle-Removal-to-Reach-Corner/2290.Minimum-Obstacle-Removal-to-Reach-Corner.cpp diff --git a/BFS/2290.Minimum-Obstacle-Removal-to-Reach-Corner/2290.Minimum-Obstacle-Removal-to-Reach-Corner.cpp b/BFS/2290.Minimum-Obstacle-Removal-to-Reach-Corner/2290.Minimum-Obstacle-Removal-to-Reach-Corner.cpp new file mode 100644 index 000000000..10b6f1bb8 --- /dev/null +++ b/BFS/2290.Minimum-Obstacle-Removal-to-Reach-Corner/2290.Minimum-Obstacle-Removal-to-Reach-Corner.cpp @@ -0,0 +1,88 @@ +class Solution { + vector>dir = {{1,0},{-1,0},{0,1},{0,-1}}; +public: + int minimumObstacles(vector>& grid) + { + int m = grid.size(); + int n = grid[0].size(); + if (m==1 && n==1) return 0; + vector>visited(m, vector(n,0)); + + queue>q; + q.push({0,0}); + visited[0][0] = 1; + + int step = 0; + while (!q.empty()) + { + int len = q.size(); + while (len--) + { + auto [x, y] = q.front(); + q.pop(); + + for (auto [dx, dy]: dir) + { + int i = x+dx; + int j = y+dy; + + if (i<0||i>=m||j<0||j>=n) continue; + if (visited[i][j]==1) continue; + if (grid[i][j] == 1) + { + visited[i][j] = 1; + q.push({i,j}); + } + else + { + for (auto [ii, jj]: travel(grid, visited, i, j)) + { + if (ii==m-1 && jj==n-1) + return step; + q.push({ii,jj}); + } + } + } + } + step++; + } + return 0; + } + + vector>travel(vector>& grid, vector>& visited, int x0, int y0) + { + int m = grid.size(); + int n = grid[0].size(); + + if (x0==m-1 && y0==n-1) + return {{x0, y0}}; + + queue>q; + q.push({x0,y0}); + visited[x0][y0] = 1; + + vector>rets; + while (!q.empty()) + { + auto [x, y] = q.front(); + q.pop(); + + for (auto [dx, dy] : dir) + { + int i = x+dx; + int j = y+dy; + if (i<0||i>=m||j<0||j>=n) continue; + if (visited[i][j]==1) continue; + visited[i][j] = 1; + if (i==m-1 && j==n-1) + rets.push_back({i,j}); + else if (grid[i][j]==1) + rets.push_back({i,j}); + else + q.push({i,j}); + } + } + + return rets; + } +}; From 2677ece1fee0aeb54a5c1efa66d7c7128e9f4d81 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Thu, 2 Jun 2022 00:56:54 -0700 Subject: [PATCH 3/4] Update Readme.md --- Readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Readme.md b/Readme.md index be65c8a21..9944d615a 100644 --- a/Readme.md +++ b/Readme.md @@ -487,6 +487,7 @@ [2045.Second-Minimum-Time-to-Reach-Destination](https://github.com/wisdompeak/LeetCode/tree/master/BFS/2045.Second-Minimum-Time-to-Reach-Destination) (M+) [2101.Detonate-the-Maximum-Bombs](https://github.com/wisdompeak/LeetCode/tree/master/BFS/2101.Detonate-the-Maximum-Bombs) (M+) [2258.Escape-the-Spreading-Fire](https://github.com/wisdompeak/LeetCode/tree/master/BFS/2258.Escape-the-Spreading-Fire) (H+) +[2290.Minimum-Obstacle-Removal-to-Reach-Corner](https://github.com/wisdompeak/LeetCode/tree/master/BFS/2290.Minimum-Obstacle-Removal-to-Reach-Corner) (M+) * ``Multi State`` [847.Shortest-Path-Visiting-All-Nodes](https://github.com/wisdompeak/LeetCode/tree/master/BFS/847.Shortest-Path-Visiting-All-Nodes) (H-) [864.Shortest-Path-to-Get-All-Keys](https://github.com/wisdompeak/LeetCode/tree/master/BFS/864.Shortest-Path-to-Get-All-Keys) (H-) From ce47ebc33f466cb16ca8bbb3f919495b1ef24703 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: Thu, 2 Jun 2022 01:23:13 -0700 Subject: [PATCH 4/4] Create Readme.md --- BFS/2290.Minimum-Obstacle-Removal-to-Reach-Corner/Readme.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 BFS/2290.Minimum-Obstacle-Removal-to-Reach-Corner/Readme.md diff --git a/BFS/2290.Minimum-Obstacle-Removal-to-Reach-Corner/Readme.md b/BFS/2290.Minimum-Obstacle-Removal-to-Reach-Corner/Readme.md new file mode 100644 index 000000000..8c97b2875 --- /dev/null +++ b/BFS/2290.Minimum-Obstacle-Removal-to-Reach-Corner/Readme.md @@ -0,0 +1,5 @@ +### 2290.Minimum-Obstacle-Removal-to-Reach-Corner + +本题的本质就是从起点到终点,采用层级BFS,最少需要穿越几个回合的障碍。而障碍与障碍之间的空气,可以忽略不计。也就是说,某个障碍与空气相邻的话,下一个回合可以通过空气到达其他的障碍。 + +在实现过程中,除了常规的层级BFS之外,我们还需要有一个travelAir的函数。travelAir以某个空格子为起点,遍历所有能"隔空"访问的障碍物。这些障碍物需要加入下一回合BFS的队列中去。

AltStyle によって変換されたページ (->オリジナル) /