-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 21 May | 934. Shortest Bridge.cpp
- Loading branch information
1 parent
3730204
commit 44c6baa
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,61 @@ | ||
class Solution { | ||
public: | ||
|
||
vector<vector<int>> dir = {{0,1}, {0,-1}, {1,0}, {-1, 0}}; | ||
|
||
void dfs(vector<vector<int>>& grid, int x, int y, queue<pair<int, int>>& q){ | ||
int n = grid.size(); | ||
grid[x][y] = 2; | ||
q.push({x,y}); | ||
|
||
for(auto d : dir){ | ||
int newX = x + d[0]; | ||
int newY = y + d[1]; | ||
|
||
if(newX >= 0 && newX < n && newY >= 0 && newY < n && grid[newX][newY] == 1){ | ||
dfs(grid, newX, newY, q); | ||
} | ||
} | ||
} | ||
|
||
int shortestBridge(vector<vector<int>>& grid) { | ||
int n = grid.size(); | ||
int firstX = -1, firstY = -1; | ||
|
||
for(int i = 0; i < n; i++){ | ||
for(int j = 0; j < n; j++){ | ||
if(grid[i][j] == 1){ | ||
firstX = i; | ||
firstY = j; | ||
break; | ||
} | ||
} | ||
} | ||
queue<pair<int, int>>q; | ||
dfs(grid, firstX, firstY, q); | ||
int dist = 0; | ||
|
||
while(!q.empty()){ | ||
int sz = q.size(); | ||
|
||
while(sz--){ | ||
int currX = q.front().first, currY = q.front().second; | ||
q.pop(); | ||
|
||
for(auto d: dir){ | ||
int newX = currX + d[0]; | ||
int newY = currY + d[1]; | ||
if(newX >= 0 && newX < n && newY >= 0 && newY < n && grid[newX][newY] == 1){ | ||
return dist; | ||
} | ||
else if(newX >= 0 && newX < n && newY >= 0 && newY < n && grid[newX][newY] == 0){ | ||
grid[newX][newY] = -1; | ||
q.push({newX, newY}); | ||
} | ||
} | ||
} | ||
dist++; | ||
} | ||
return dist; | ||
} | ||
}; |