首页 > 代码库 > Surrounded Regions

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

答案

class IntPair
{
    int row;

    int col;

    IntPair(int row, int col)
    {
        this.row = row;
        this.col = col;
    }
}


public class Solution
{
    boolean[][] checked;

    char[][] board;

    int ROW;

    int COL;

    boolean isSourrend(int row, int col)
    {
        boolean result = true;
        LinkedList<IntPair> nodes = new LinkedList<IntPair>();
        nodes.add(new IntPair(row, col));
        checked[row][col] = true;
        while (nodes.size() > 0)
        {
            IntPair p = nodes.get(0);
            if (p.row == 0 || p.row == ROW - 1 || p.col == 0 || p.col == COL - 1)
            {
                result = false;
            }
            else
            {
                if (p.row > 0 && !checked[p.row - 1][p.col] && board[p.row - 1][p.col] == 'O')
                {
                    nodes.add(new IntPair(p.row - 1, p.col));
                    checked[p.row - 1][p.col] = true;
                }
                if (p.row < ROW - 1 && !checked[p.row + 1][p.col]
                    && board[p.row + 1][p.col] == 'O')
                {
                    nodes.add(new IntPair(p.row + 1, p.col));
                    checked[p.row + 1][p.col] = true;
                }
                if (p.col > 0 && !checked[p.row][p.col - 1] && board[p.row][p.col - 1] == 'O')
                {
                    nodes.add(new IntPair(p.row, p.col - 1));
                    checked[p.row][p.col - 1] = true;
                }
                if (p.col < COL - 1 && !checked[p.row][p.col + 1]
                    && board[p.row][p.col + 1] == 'O')
                {
                    nodes.add(new IntPair(p.row, p.col + 1));
                    checked[p.row][p.col + 1] = true;
                }
            }
            //System.out.println(p.row + "\t" + p.col);

            nodes.remove(0);
        }
        return result;
    }
    public void fill(int row, int col)
    {
        if (row < 0 || row >= ROW || col < 0 || col >= COL || board[row][col] == 'X')
        {
            return;
        }
        board[row][col]='X';
        int minRow;
        int maxRow;
        int minCol;
        int maxCol;
        for(minRow=row-1;minRow>0&&board[minRow][col]=='O';minRow--)
        {
            board[minRow][col]='X';
        }
        for(maxRow=row+1;maxRow>0&&board[maxRow][col]=='O';maxRow++)
        {
            board[maxRow][col]='X';
        }
        for(minCol=col-1;minCol<COL&&board[row][minCol]=='O';minCol--)
        {
            board[row][minCol] = 'X';
        }
        for(maxCol=col+1;maxCol<COL&&board[row][maxCol]=='O';maxCol++)
        {
            board[row][maxCol] = 'X';
        }
        for(int k=minRow+1;k<maxRow;k++)
        {
            fill(k,col-1);
            fill(k,col+1);
        }
        for(int k=minCol+1;k<maxCol;k++)
        {
            fill(row-1,k);
            fill(row+1,k);
        }
    }
    public void solve(char[][] board)
    {
        if (board == null || board.length <= 1 || board[0].length <= 1)
        {
            return;
        }
        ROW = board.length;
        COL = board[0].length;
        checked = new boolean[ROW][COL];
        this.board = board;
        for (int row = 0; row < ROW; row++ )
        {
            for (int col = 0; col < COL; col++ )
            {
                checked[row][col] = false;
            }
        }
        for (int row = 1; row < ROW - 1; row++ )
        {
            for (int col = 1; col < COL - 1; col++ )
            {
                if (!checked[row][col] && board[row][col] == 'O')
                {
                    if (isSourrend(row, col))
                    {
                        fill(row, col);
                    }
                }
            }
        }
    }
}


Surrounded Regions