From fdb2d87fb1414eab8f0f53e4e6c64b9e62a1d761 Mon Sep 17 00:00:00 2001 From: Evan Ferrao Date: Sat, 14 Feb 2026 19:41:40 +0530 Subject: [PATCH] surrounded regions --- Graphs/SurroundedRegions.java | 141 ++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 Graphs/SurroundedRegions.java diff --git a/Graphs/SurroundedRegions.java b/Graphs/SurroundedRegions.java new file mode 100644 index 0000000..d11e7c0 --- /dev/null +++ b/Graphs/SurroundedRegions.java @@ -0,0 +1,141 @@ +// https://leetcode.com/problems/surrounded-regions/ + +import java.util.*; +import java.io.*; + +class Main { + + static class Node{ + int row; + int col; + + Node(int row, int col){ + this.row = row; + this.col = col; + } + } + + // DFS + public static void dfs(Node node, char board[][], boolean doNotTouch[][]){ + + int m = board.length; + int n = board[0].length; + + int r = node.row; + int c = node.col; + + doNotTouch[r][c] = true; + + int dr[] = {-1, 0, 1, 0}; + int dc[] = {0, 1, 0, -1}; + + for(int k=0;k<4;k++){ + int nr = r + dr[k]; + int nc = c + dc[k]; + + if(nr>=0 && nc>=0 && nr q = new LinkedList<>(); + q.add(start); + doNotTouch[start.row][start.col] = true; + + int dr[] = {-1, 0, 1, 0}; + int dc[] = {0, 1, 0, -1}; + + while(!q.isEmpty()){ + Node cur = q.poll(); + + for(int k=0;k<4;k++){ + int nr = cur.row + dr[k]; + int nc = cur.col + dc[k]; + + if(nr>=0 && nc>=0 && nr