码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]Surrounded Regions,解题报告

时间:2015-03-20 20:29:23      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:dfs   bfs   lee   

目录


前言

最近这两天为了解决Android Rom适配一个底层的问题,天天熬夜到2点,导致原定了LeetCode刷题计划都受到了影响。昨晚终于熬夜搞定,补充一道LeetCode解题报告。


题目

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 X
X O O X
X X O X
X O X X

After running your function, the board should be:

X X X X
X X X X
X X X X
X O X X


思路1_DFS

这道题目给出的提示是用广度优先搜索(BFS),但是我憋了半天没思路,因为平时我一般都是用BFS解决一些迷宫的题目。

于是,我还是想到换个思路,既然BFS不行,那就DFS(深度优先搜索)。思路如下:

  1. 最外层如果有‘O’的存在,那它一定不会被‘X’包围的。这时需要遍历board二维数组的最外层,需要4次for循环。
  2. 每次最外层遍历的过程中,如果发现有‘O’,那我们可以尝试进行上下左右四个方向的遍历,如果再次发现有’O’,那当前的‘O’也是不会被‘X’包围的,将这种‘O’转换为‘K’。DFS的过程。
  3. 4次遍历完成后,需要再次遍历board数组。数组元素不为‘K’的都应该是被‘X’包围的,因此统一改为‘X’。为‘K’的不要忘记要改回为‘O’。

DFS代码如下:

public class Solution {
    private static int[][] directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

    public static void solve(char[][] board) {
        int row = board.length;
        if (row < 2) {
            return ;
        }

        int column = board[0].length;
        if (column < 2) {
            return;
        }


        for (int j = 0; j < board[0].length; j ++) {
            if (board[0][j] == ‘O‘) {
                dfsBoard(board, 0, j);
            }
        }

        for (int i = 1; i < board.length; i ++) {
            if (board[i][column - 1] == ‘O‘) {
                dfsBoard(board, i, column - 1);
            }
        }

        for (int j = column - 2; j >= 0; j --) {
            if (board[row - 1][j] == ‘O‘) {
                dfsBoard(board, row - 1, j);
            }
        }

        for (int i = row - 2; i >= 0; i --) {
            if (board[i][0] == ‘O‘) {
                dfsBoard(board, i, 0);
            }
        }


        for (int i = 0; i < row; i ++) {
            for (int j = 0; j < column; j ++) {             
                board[i][j] = board[i][j] == ‘K‘ ? ‘O‘ : ‘X‘;
            }
        }
    }

    private static void dfsBoard(char[][] board, int x, int y) {        
        board[x][y] = ‘K‘;
        for (int i = 0; i < directions.length; i ++) {
            int new_x = x + directions[i][0];
            int new_y = y + directions[i][1];
            if (new_x < 0 || new_x >= board.length || new_y < 0 || new_y >= board[0].length || board[new_x][new_y] != ‘O‘) {
                continue;
            }
            dfsBoard(board, new_x, new_y);
        }
    }
}

本来以为结果会TLE,没想到出乎我的意料,结果是Runtime Error,递归栈被爆掉了。

技术分享


思路2_BFS

DFS爆栈充分说明题目的提示还是很管用的,必须要用BFS啊。但是,上面DFS的方法给我们提供了很好的思路。爆栈的原因是:DFS栈深度不够,那我们直接将这里的DFS遍历换成BFS不就可以了。

思路更改的地方:

  1. 每次最外层遍历的过程中,如果发现有‘O’,那我们可以尝试进行上下左右四个方向的遍历,如果再次发现有’O’,将当前节点放入队列中。放入队列中的‘O’也是不会被‘X’包围的,将这种‘O’转换为‘K’即可。(ps:其实就是将栈实现改为队列实现)

直接上AC代码了:

public class Solution {
    private static class BoardPoint {
        private int x, y;

        public BoardPoint(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public int getX() {
            return x;
        }

        public int getY() {
            return y;
        }
    }

    private static int[][] directions = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };

    public static void solve(char[][] board) {
        int row = board.length;
        if (row < 2) {
            return;
        }

        int column = board[0].length;
        if (column < 2) {
            return;
        }

        for (int j = 0; j < board[0].length; j++) {
            if (board[0][j] == ‘O‘) {
                bfsBoard(board, 0, j, row, column);
            }
        }

        for (int i = 1; i < board.length; i++) {
            if (board[i][column - 1] == ‘O‘) {
                bfsBoard(board, i, column - 1, row, column);
            }
        }

        for (int j = column - 2; j >= 0; j--) {
            if (board[row - 1][j] == ‘O‘) {
                bfsBoard(board, row - 1, j, row, column);
            }
        }

        for (int i = row - 2; i >= 0; i--) {
            if (board[i][0] == ‘O‘) {
                bfsBoard(board, i, 0, row, column);
            }
        }

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                board[i][j] = board[i][j] == ‘K‘ ? ‘O‘ : ‘X‘;
            }
        }
    }

    private static void bfsBoard(char[][] board, int x, int y, int row, int col) {
        ArrayDeque<BoardPoint> queue = new ArrayDeque<BoardPoint>();
        queue.push(new BoardPoint(x, y));
        while (!queue.isEmpty()) {
            BoardPoint point = queue.poll();
            board[point.getX()][point.getY()] = ‘K‘;

            for (int i = 0; i < directions.length; i++) {
                int new_x = point.getX() + directions[i][0];
                int new_y = point.getY() + directions[i][1];

                if (new_x >= 0 && new_x < row && new_y >= 0 && new_y < col && board[new_x][new_y] == ‘O‘) {
                    queue.push(new BoardPoint(new_x, new_y));
                }
            }
        }
    }

}

[LeetCode]Surrounded Regions,解题报告

标签:dfs   bfs   lee   

原文地址:http://blog.csdn.net/wzy_1988/article/details/44494807

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!