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

130. Surrounded Regions

时间:2017-10-04 14:31:33      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:length   div   boa   oid   ted   board   oar   region   void   

public class Solution {
    public void solve(char[][] board) {
    	if (board.length == 0 || board[0].length == 0)
    		return;
    	int m = board.length;
    	int n = board[0].length;
        boolean[][] visited = new boolean[m][n];
        
        for (int j = 0; j < n; j ++)
        {
        	if (board[0][j] == ‘O‘)
        		fill(0,j,board,visited);
        	if (board[m-1][j] == ‘O‘)
        		fill(m-1,j,board,visited);
        }
        
        for (int i = 0; i < m; i++)
        {
        	if (board[i][0] == ‘O‘)
        		fill(i,0,board,visited);
        	if (board[i][n-1] == ‘O‘)
        		fill(i,n-1,board,visited);
        }
        
        for (int i = 0; i < m; i++)
        	for (int j = 0; j < n; j++)
        		if (visited[i][j] == false&&board[i][j] == ‘O‘)
        			board[i][j] = ‘X‘;
    }

	private void fill(int i, int j, char[][] board, boolean[][] visited) {
		if (i < 0 || i >= board.length || j < 0 || j >= board[0].length || visited[i][j] == true || board[i][j] == ‘X‘)
			return;
		visited[i][j] = true;
		if(i > 1)
			fill(i-1,j,board,visited);
		if (i < board.length - 2)
			fill(i+1,j,board,visited);
		if (j > 1)
			fill(i,j-1,board,visited);
		if (j < board[0].length-2)
			fill(i,j+1,board,visited);
	}
}

  

130. Surrounded Regions

标签:length   div   boa   oid   ted   board   oar   region   void   

原文地址:http://www.cnblogs.com/asuran/p/7625360.html

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