Leetcode Surrounded Regions 解题报告

44 篇文章 0 订阅
43 篇文章 0 订阅

http://oj.leetcode.com/problems/surrounded-regions/
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

题意说明:输入一个二维矩阵,把所有被X包围的O都变成X,但如果某个O在边(角)上,则所有与这个O相连的O都不能变成X。
解题思路:从边上的O开始便利,所有与边上O相连的O都不能变成X(可暂时变为Y),之后再重新遍历一遍矩阵把所有O变成X,所有Y变成O。
需要注意的是,本题使用DFS也会出现栈溢出,只能使用BFS。从队列中取出一个节点,把该节点变为Y,如果该节点的四周有还未变为Y的O,则这些节点放入队列。
AC代码:

class Pair {
    int x;
    int y;
    Pair(int x,int y) {
        this.x = x;
        this.y = y;
    }
}

public class Solution {

    public void solve(char[][] board) {
        if(board==null||board.length==0) {
            return;
        }
        Queue queue = new LinkedList<Pair>();
	//对所有在边上的O节点进行BFS
        for(int i=0;i<board.length;i++) {
            for(int j=0;j<board[0].length;j++) {
                if(i==0||i==(board.length-1)||j==0||j==(board[0].length-1)) {
                    if(board[i][j]=='O') {
                        Pair position = new Pair(i,j);
                        queue.add(position);
                    }
                }
            }
        }
        int x1,y1;
        //BFS
        while(!queue.isEmpty()) {
            Pair position = (Pair)queue.poll();
            x1 = position.x;
            y1 = position.y;

            if(board[x1][y1]=='Y') {
                continue;
            }
	    //四个方向查找,找到为'O'的节点放入队列中。
            //left
            board[x1][y1] = 'Y';
            int index = x1 - 1;
            if(index>=0&&board[index][y1]=='O') {
                queue.add(new Pair(index,y1));
            }
            //right
            index = x1 + 1;
            if(index<board.length&&board[index][y1]=='O') {
                queue.add(new Pair(index,y1));
            }
            //up
            index = y1 + 1 ;
            if(index<board[0].length&&board[x1][index]=='O') {
                queue.add(new Pair(x1,index));
            }
            //down
            index = y1 - 1;
            if(index>=0&&board[x1][index]=='O') {
                queue.add(new Pair(x1,index));
            }
        }
        
        
        for(int i=0;i<board.length;i++) {
            for(int j=0;j<board[0].length;j++) {
                if(board[i][j]=='Y') {
                    board[i][j] = 'O';
                } else {
                    if(board[i][j]=='O') {
                        board[i][j] = 'X';
                    }
                }
            }
        }
        return;
    }
}



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值