forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 1
[pull] master from wisdompeak:master #320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c3774ff
Create 3568.Minimum-Moves-to-Clean-the-Classroom.cpp
wisdompeak d5e011b
Update Readme.md
wisdompeak bc78433
Create Readme.md
wisdompeak cfe262c
Create 3553.Minimum-Weighted-Subgraph-With-the-Required-Paths-II.cpp
wisdompeak 0f3873e
Update Readme.md
wisdompeak ca0ce46
Update Readme.md
wisdompeak ea16c13
Create Readme.md
wisdompeak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
BFS/3568.Minimum-Moves-to-Clean-the-Classroom/3568.Minimum-Moves-to-Clean-the-Classroom.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using state = tuple<int,int,int,int>; | ||
class Solution { | ||
public: | ||
int minMoves(vector<string>& grid, int energy) { | ||
int m = grid.size(), n = grid[0].size(); | ||
|
||
pair<int,int>start; | ||
vector<pair<int,int>>litter_pos; | ||
vector<vector<int>>litter_idx(m, vector<int>(n,-1)); | ||
|
||
for (int i=0; i<m; i++) | ||
for (int j=0; j<n; j++) { | ||
if (grid[i][j]=='S') | ||
start = {i,j}; | ||
else if (grid[i][j]=='L') | ||
{ | ||
litter_pos.push_back({i,j}); | ||
litter_idx[i][j] = litter_pos.size()-1; | ||
} | ||
} | ||
int L = litter_pos.size(); | ||
|
||
vector<vector<vector<vector<bool>>>> visited( | ||
m, vector<vector<vector<bool>>>( | ||
n, vector<vector<bool>>(energy + 1, vector<bool>(1 << L, false)))); | ||
|
||
visited[start.first][start.second][energy][0] = true; | ||
|
||
vector<pair<int,int>>dir={{0,1},{0,-1},{1,0},{-1,0}}; | ||
|
||
queue<state> q; | ||
q.push({start.first, start.second, energy, 0}); | ||
int step = 0; | ||
|
||
while (!q.empty()) | ||
{ | ||
int len = q.size(); | ||
while (len--) | ||
{ | ||
auto [x,y,e,mask] = q.front(); | ||
q.pop(); | ||
if (mask==(1<<L)-1) return step; | ||
|
||
for (int k=0; k<4; k++) | ||
{ | ||
int nx = x+dir[k].first; | ||
int ny = y+dir[k].second; | ||
if (nx<0||nx>=m||ny<0||ny>=n) continue; | ||
|
||
char cell = grid[nx][ny]; | ||
if (cell=='X') continue; | ||
|
||
int newEnergy = e-1; | ||
if (newEnergy < 0) continue; | ||
if (cell == 'R') newEnergy = energy; | ||
|
||
int newMask = mask; | ||
if (grid[nx][ny]=='L') | ||
{ | ||
int idx = litter_idx[nx][ny]; | ||
newMask |= (1<<idx); | ||
} | ||
if (!visited[nx][ny][newEnergy][newMask]) { | ||
visited[nx][ny][newEnergy][newMask] = true; | ||
q.emplace(nx, ny, newEnergy, newMask); | ||
} | ||
} | ||
} | ||
step++; | ||
} | ||
|
||
return -1; | ||
} | ||
}; |
5 changes: 5 additions & 0 deletions
BFS/3568.Minimum-Moves-to-Clean-the-Classroom/Readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
### 3568.Minimum-Moves-to-Clean-the-Classroom | ||
|
||
典型的BFS,但是我们需要定义一个composite state来避免重复。本题中因为最有路径可能需要重复经过相同的cell,所以我们还需要记录每一步时当前的energy,已经访问过的litter。这样所有的状态种类有`20*20*50*2^10=2e7`,复杂度勉强够用。 | ||
|
||
具体的做法就是常规的BFS。每次从队列中弹出[x,y,energy,mask],其中mask是一个二进制数,用bit位来记录哪些编号的垃圾已经被清理。从当前[x,y]往四个方向尝试移动,每次移动需要更新energy(减一,或者遇到R就reset),也可能需要更新mask(即遇到L)。在将新状态加入队列之前,查看一下这个state之前是否已经加入过队列(即可以在更早的时间被访问到)以避免重复搜索。 |
80 changes: 80 additions & 0 deletions
...-With-the-Required-Paths-II/3553.Minimum-Weighted-Subgraph-With-the-Required-Paths-II.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using ll = long long; | ||
const int MAXN = 100000; | ||
const int LOGN = 17; | ||
class Solution { | ||
public: | ||
vector<pair<int,int>> adj[MAXN]; | ||
int up[MAXN][LOGN+1]; | ||
int depth[MAXN]; | ||
ll distRoot[MAXN]; | ||
|
||
void dfs(int cur, int parent) | ||
{ | ||
up[cur][0] = parent; | ||
for(auto &[v,w]: adj[cur]) | ||
{ | ||
if(v == parent) continue; | ||
depth[v] = depth[cur] + 1; | ||
distRoot[v] = distRoot[cur] + w; | ||
dfs(v, cur); | ||
} | ||
} | ||
|
||
int lca(int a, int b) | ||
{ | ||
if(depth[a] < depth[b]) swap(a,b); | ||
int diff = depth[a] - depth[b]; | ||
for(int k = 0; k <= LOGN; k++){ | ||
if(diff & (1<<k)) a = up[a][k]; | ||
} | ||
if(a == b) return a; | ||
for(int k = LOGN; k >= 0; k--){ | ||
if(up[a][k] != up[b][k]){ | ||
a = up[a][k]; | ||
b = up[b][k]; | ||
} | ||
} | ||
return up[a][0]; | ||
} | ||
|
||
ll dist(int a, int b) | ||
{ | ||
int c = lca(a,b); | ||
return distRoot[a] + distRoot[b] - 2*distRoot[c]; | ||
} | ||
|
||
vector<int> minimumWeight(vector<vector<int>>& edges, vector<vector<int>>& queries) | ||
{ | ||
int n = edges.size()+1; | ||
|
||
for (int i = 0; i < n-1; i++) | ||
{ | ||
int u = edges[i][0], v = edges[i][1], w = edges[i][2]; | ||
adj[u].push_back({v,w}); | ||
adj[v].push_back({u,w}); | ||
} | ||
|
||
depth[0] = 0; | ||
distRoot[0] = 0; | ||
dfs(0, 0); | ||
|
||
for(int k = 1; k <= LOGN; k++) { | ||
for(int v = 0; v < n; v++) { | ||
up[v][k] = up[up[v][k-1]][k-1]; | ||
} | ||
} | ||
|
||
vector<int>rets; | ||
for (auto& q: queries) | ||
{ | ||
int u = q[0], v = q[1], w = q[2]; | ||
ll d_uv = dist(u,v); | ||
ll d_vw = dist(v,w); | ||
ll d_uw = dist(u,w); | ||
ll ans = (d_uv + d_vw + d_uw) / 2; | ||
rets.push_back(ans); | ||
} | ||
|
||
return rets; | ||
} | ||
}; |
3 changes: 3 additions & 0 deletions
Binary_Search/3553.Minimum-Weighted-Subgraph-With-the-Required-Paths-II/Readme.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
### 3553.Minimum-Weighted-Subgraph-With-the-Required-Paths-II | ||
|
||
本题的第一个知识点是:在一棵树里,联通u,v,w三个节点的最小子树的权重和,就是`[dist(u,v)+dist(u,w)+dist(v,w)]/2`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.