Skip to content
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

130. Surrounded Regions #496

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
| 743 | [Network Delay Time](https://leetcode.com/problems/network-delay-time/) | [C++](./C++/Network-delay-time.cpp) | _O(V+E))_ | O(V) | Medium | BFS |
| 111 | [Min Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/) | [JavaScript](./JavaScript/111-minimum-depth-of-binary-tree.js) | O(nlogn) | O(nlogn) | Easy | BFS |
| 100 | [Same Tree](https://leetcode.com/problems/same-tree/) | [C++](https://github.com/codedecks-in/LeetCode-Solutions/blob/master/C%2B%2B/100_Same_Tree.cpp) | O(N) | O(N) | Easy | BFS |

| 130 | [Surrounded Regions](https://leetcode.com/problems/surrounded-regions/) | [C++](./C++/kth-smallest-element-in-a-bst.cpp) | _O(m*n)_ | _O(m*n)_ | Medium | DFS | |

<br/>
<div align="right">
Expand Down Expand Up @@ -515,6 +515,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if
| [Shrimadh V Rao](https://github.com/Shrimadh) <br> <img src="https://avatars.githubusercontent.com/u/64469917?v=4" width="100" height="100"> | India | C++ | [GitHub](https://github.com/Shrimadh)
| [Shreyas Shrawage](https://github.com/shreyventure) <br> <img src = "https://avatars.githubusercontent.com/u/55741087?v=4" width="100" height="100"> | India | Python | [CodeChef](https://www.codechef.com/users/shreyventure)<br/>[LeetCode](https://leetcode.com/shreyventure/)<br/>[HackerRank](https://www.hackerrank.com/shreyas_shrawage)
| [Surbhi Mayank](https://github.com/surbhi2408) <br> <img src="https://avatars.githubusercontent.com/u/58289829?s=400&u=68fd396819b927ec4d8820d87d6d1e311c3abd01&v=4" width="100" height="100"> | India | C++ | [GitHub](https://github.com/surbhi2408)
| [Nick H](https://github.com/n1i9c9k9) <br> <img src="https://github.com/n1i9c9k9.png" width="100" height="100"> | Germany | C++ | [GitHub](https://github.com/n1i9c9k9)
<div align="right">
<b><a href="#algorithms">⬆️ Back to Top</a></b>
</div>
Expand Down
64 changes: 64 additions & 0 deletions surrounded-regions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <vector>
#include <queue>

class Solution {
public:
void solve(std::vector<std::vector<char>>& board) {
if (board.empty() || board[0].empty()) return;

int m = board.size();
int n = board[0].size();

// Directions for moving up, down, left, right
std::vector<std::pair<int, int>> directions = {
{1, 0}, // down
{0, 1}, // right
{0, -1}, // left
{-1, 0} // up
};

// BFS function to mark all connected 'O's
auto bfs = [&](int startY, int startX) {
std::queue<std::pair<int, int>> q;
q.push({startY, startX});
board[startY][startX] = 'E'; // Mark as visited with 'E'

while (!q.empty()) {
auto [y, x] = q.front();
q.pop();

for (const auto& dir : directions) {
int newY = y + dir.first;
int newX = x + dir.second;

if (newY >= 0 && newY < m && newX >= 0 && newX < n && board[newY][newX] == 'O') {
board[newY][newX] = 'E'; // Mark as visited
q.push({newY, newX}); // Add to queue
}
}
}
};

// Check all edge 'O's
for (int i = 0; i < n; ++i) {
if (board[0][i] == 'O') bfs(0, i); // Top row
if (board[m - 1][i] == 'O') bfs(m - 1, i); // Bottom row
}

for (int j = 1; j < m - 1; ++j) {
if (board[j][0] == 'O') bfs(j, 0); // Left column
if (board[j][n - 1] == 'O') bfs(j, n - 1); // Right column
}

// Update the board: 'O's not connected to the edges become 'X', and 'E's back to 'O'
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (board[i][j] == 'O') {
board[i][j] = 'X'; // Flip 'O's to 'X's
} else if (board[i][j] == 'E') {
board[i][j] = 'O'; // Restore 'E's back to 'O's
}
}
}
}
};