【leetcode】Surrounded Regions

來源:互聯網
上載者:User

Question :

Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.

A region is captured by flipping all 'O's into 'X's in that surrounded region .

For example,

X X X XX O O XX X O XX O X X

After running your function, the board should be:

X X X XX X X XX X X XX O X X

Anwser 1 :       DFS

class Solution {public:    void DFS(vector<vector<char> > &board, int i, int j){        if(i >= board.size() || j >= board[0].size()) return;                if(board[i][j] == 'O'){            board[i][j] = '#';                    DFS(board, i-1, j);  // to left            DFS(board, i+1, j);  // to right            DFS(board, i, j-1);  // to up            DFS(board, i, j+1);  // to down        }    }    void solve(vector<vector<char>> &board) {        // Start typing your C/C++ solution below        // DO NOT write int main() function                if(board.empty()) return;                int len = board[0].size();  // one row length                for(int i = 1; i < len - 1; i++){            DFS(board, i, 0);        // left            DFS(board, i, len-1);    // right        }                for(int j = 0; j < len; j++){            DFS(board, 0, j);        // up            DFS(board, len-1, j);    // down        }                //change 'O' to 'X'        for(int i = 0; i < len; i++){            for(int j = 0; j < len; j++){                if(board[i][j] == 'O') board[i][j] = 'X';                if(board[i][j] == '#') board[i][j] = 'O';            }        }    }};

Anwser 2 :  BFS

class Solution {public:    void enQ(vector<vector<char>> &board, int row, int col, int len, queue<int> &qu){        if(row < 0 || row >= len || col < 0 || col >= len) return;        if(board[row][col] == 'O'){            board[row][col] = '#';            qu.push(row * len + col);        }    }    void BFS(vector<vector<char> > &board, int row, int col, int len, queue<int> &qu){        enQ(board, row, col, len, qu);                while(!qu.empty()){            int val = qu.front();            qu.pop();                        int row = val / len;            int col = val % len;                    BFS(board, row-1, col, len, qu);  // to left            BFS(board, row+1, col, len, qu);  // to right            BFS(board, row, col-1, len, qu);  // to up            BFS(board, row, col+1, len, qu);  // to down        }    }    void solve(vector<vector<char>> &board) {        // Start typing your C/C++ solution below        // DO NOT write int main() function                if(board.empty()) return;                int len = board[0].size();  // one row length        queue<int> Q;                for(int i = 1; i < len - 1; i++){            BFS(board, i, 0, len, Q);        // left            BFS(board, i, len-1, len, Q);    // right        }                for(int j = 0; j < len; j++){            BFS(board, 0, j, len, Q);        // up            BFS(board, len-1, j, len, Q);    // down        }                //change 'O' to 'X'        for(int i = 0; i < len; i++){            for(int j = 0; j < len; j++){                if(board[i][j] == 'O') board[i][j] = 'X';                if(board[i][j] == '#') board[i][j] = 'O';            }        }    }};

Anwser 3 :   BFS  in Java

public class Solution {    int m,n;    char[][] board;    Queue<Integer> queue=new LinkedList<Integer>();        void fill(int x, int y){        if(x<0 || x>=m || y<0 || y>=n || board[x][y]!='O') return;        queue.offer(x*m+y);        board[x][y]='D';    }        void bfs(int x, int y){        fill(x,y);                while(!queue.isEmpty()){            int curr=queue.poll();            int i=curr/n;            int j=curr%n;                        fill(i-1,j);            fill(i+1,j);            fill(i,j-1);            fill(i,j+1);        }    }        public void solve(char[][] board){        if(board==null || board.length==0) return;        this.board=board;        m=board.length;        n=board[0].length;                for(int j=0;j<n;j++){            bfs(0,j);            bfs(m-1,j);        }                for(int i=1;i<m-1;i++){            bfs(i,0);            bfs(i,n-1);        }                for(int i=0;i<m;i++)            for(int j=0;j<n;j++){                if(board[i][j]=='O') board[i][j]='X';                else if(board[i][j]=='D') board[i][j]='O';            }    }}

注意點:

1) 方法1通過,方法2不通過,方法3有時通過有時不通過(不通過與方法2編譯錯誤一致)

2) 查了int在c++和java中類型,int在c++兩個位元組,在java佔四個位元組,這可能會導致push(int)值過大,但在方法2(c++)中把int改為long(4位元組),編譯仍不通過

3) 上面這個問題研究了很長一段時間,目前還沒搞明白,大家看到這篇部落格歡迎來探討、指正

參考推薦:

Leetcode 130: Surrounded Regions

Surrounded Regions

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.