@@ -0,0 +1,2086 @@
+/**%0A * Problem Link: https://leetcode.com/problems/surrounded-regions/%0A * %0A * DFS will cause stackoverflow, for large tables, dfs will be called under too many levels.%0A * %0A */%0Apublic class Solution %7B%0A    private boolean%5B%5D%5B%5D mark;%0A    private int n;%0A    private int m;%0A    private int%5B%5D dx = %7B0, 1, 0, -1%7D;%0A    private int%5B%5D dy = %7B1, 0, -1, 0%7D;%0A%0A    public void solve(char%5B%5D%5B%5D board) %7B%0A        n = board.length;%0A        if(n == 0) return;%0A        m = board%5B0%5D.length;%0A        mark = new boolean%5Bn%5D%5Bm%5D;%0A        %0A        for(int i=0; i%3Cn; i++) %7B%0A            if(board%5Bi%5D%5B0%5D == 'O') bfs(board, i, 0);%0A            if(board%5Bi%5D%5Bm-1%5D == 'O') bfs(board, i, m-1);%0A        %7D%0A        for(int i=0; i%3Cm; i++) %7B%0A            if(board%5B0%5D%5Bi%5D == 'O') bfs(board, 0, i);%0A            if(board%5Bn-1%5D%5Bi%5D == 'O') bfs(board, n-1, i);%0A        %7D%0A        %0A        for(int i=1; i%3Cn-1; i++) %7B%0A            for(int j=1; j%3Cm-1; j++) %7B%0A                if(board%5Bi%5D%5Bj%5D == 'O' && !mark%5Bi%5D%5Bj%5D) %7B%0A                    board%5Bi%5D%5Bj%5D = 'X';%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A    %0A    private void bfs(char%5B%5D%5B%5D board, int x, int y) %7B%0A        if(mark%5Bx%5D%5By%5D) return;%0A        mark%5Bx%5D%5By%5D = true;%0A        Queue%3CInteger%3E q = new LinkedList%3CInteger%3E();%0A        q.offer(x * m + y);%0A        while(!q.isEmpty()) %7B%0A            int sum = q.poll();%0A            x = sum / m;%0A            y = sum %25 m;%0A            for(int i=0; i%3Cdx.length; i++) %7B%0A                int xx = x + dx%5Bi%5D;%0A                int yy = y + dy%5Bi%5D;%0A                if(xx %3E=0 && xx %3C n && yy %3E=0 && yy %3C m && board%5Bxx%5D%5Byy%5D == 'O' && !mark%5Bxx%5D%5Byy%5D) %7B%0A                    mark%5Bxx%5D%5Byy%5D = true;%0A                    q.offer(xx * m + yy);%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A    %0A    private void dfs(char%5B%5D%5B%5D board, int x, int y) %7B%0A        if(mark%5Bx%5D%5By%5D) return;%0A        mark%5Bx%5D%5By%5D = true;%0A        for(int i=0; i%3Cdx.length; i++) %7B%0A            int xx = x + dx%5Bi%5D;%0A            int yy = y + dy%5Bi%5D;%0A            if(xx %3E=0 && xx %3C n && yy %3E=0 && yy %3C m && board%5Bxx%5D%5Byy%5D == 'O' && !mark%5Bxx%5D%5Byy%5D) %7B%0A                dfs(board, xx, yy);%0A            %7D %0A        %7D%0A    %7D%0A%7D%0A

Create Surrounded_Regions.java