[LeetCode][Java] 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

题意:

给定一个2维平面包含‘X‘ 和 ‘O‘,填充所有的被‘X‘包围的区域.

比如,

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

运行完你的函数后,这个平面变为:

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

算法分析:

* 典型的BFS题目。遍历每个字符,如果是“O”,则从当前字符开始BFS遍历,如果周围也是“O”则加入当前遍历的队列,

* 直到遍历完所有相邻的“O”,于此同时,判断每个O是否是被包围的,只要由一个O是没有被包围的,

* 则当前遍历的O的集合都是没有被包围的,因为这些O都是相连的。

AC代码:

<span style="font-family:Microsoft YaHei;font-size:12px;">public class Solution
{
	 public void solve(char[][] board)
	 {
    	if(board==null||board.length==0)
    		return;
        int row=board.length;
        int col=board[0].length;

        boolean visited[][] = new boolean[row][col] ;//遍历标记数组
        for(int i=0;i<row;i++)
        {
        	for(int j=0;j<col;j++)
        	{
        	    if(board[i][j]=='O'&&(!visited[i][j]))
        	    {
                    bfs(board,i,j,visited);//若未遍历过,则对该节点进行广度优先搜索
            	}
        	}
        }
	 }
	private void bfs(char[][] board,int i,int j,boolean visited[][])
	{
   	    ArrayList<Integer> list =new ArrayList<Integer>();
   		Queue<Integer> queue = new LinkedList<Integer>();
		boolean label=true;
        int row=board.length;
        int col=board[0].length;
		int temjudge,temi,temj;

	    queue.add(i*col+j);//将数组位置二维转化为一维
		visited[i][j]=true;

		while(!queue.isEmpty())
		{
		    temjudge=queue.poll();
			list.add(temjudge);
			temj=temjudge%col;//列位置
			temi=(temjudge-temj)/col;//行位置
			if(temi==0||temj==0||temi==row-1||temj==col-1)
			    label=false;//若该节点位于边界处,这是没有被围,所以遍历到的所有节点都不变化,这里用label记录一下

			if(temj>0&&board[temi][temj-1]=='O'&&!visited[temi][temj-1])//左
			{
					queue.add(temi*col+temj-1);
					visited[temi][temj-1]=true;
			}
			if(temi>0&&board[temi-1][temj]=='O'&&!visited[temi-1][temj])//上
			{
					queue.add((temi-1)*col+temj);
					visited[temi-1][temj]=true;
			}
			if(temj<board[0].length-1&&board[temi][temj+1]=='O'&&!visited[temi][temj+1])//右
			{
					queue.add(temi*col+temj+1);
					visited[temi][temj+1]=true;
			}
			if(temi<board.length-1&&board[temi+1][temj]=='O'&&!visited[temi+1][temj])//下
			{
					queue.add((temi+1)*col+temj);
					visited[temi+1][temj]=true;
			}
		}

		if(label)//遍历到的所有的节点都没有处于边界,这是对这些节点进行变化
		{
			for(int k=0;k<list.size();k++)
			{
				temjudge=list.get(k);
				temj=temjudge%col;
			    temi=(temjudge-temj)/col;
				board[temi][temj]='X';
			}
		}
    }
}</span>

版权声明:本文为博主原创文章,转载注明出处

时间: 2024-10-05 14:11:18

[LeetCode][Java] Surrounded Regions的相关文章

LeetCode OJ - Surrounded Regions

我觉得这道题和传统的用动规或者贪心等算法的题目不同.按照题目的意思,就是将被'X'围绕的'O'区域找出来,然后覆盖成'X'. 那问题就变成两个子问题: 1. 找到'O'区域,可能有多个区域,每个区域'O'都是相连的: 2. 判断'O'区域是否是被'X'包围. 我采用树的宽度遍历的方法,找到每一个'O'区域,并为每个区域设置一个value值,为0或者1,1表示是被'X'包围,0则表示不是.是否被'X'包围就是看'O'区域的边界是否是在2D数组的边界上. 下面是具体的AC代码: class Boar

leetcode --day12 Surrounded Regions &amp; Sum Root to Leaf Numbers &amp; Longest Consecutive Sequence

1.  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 fu

[LeetCode][JavaScript]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

【LeetCode】 Surrounded Regions (BFS &amp;&amp; DFS)

题目:Surrounded Regions 广搜和深搜都能解决,但是LeetCode上使用深搜时会栈溢出 DFS: <span style="font-size:18px;">/*LeetCode Surrounded Regions * 题目:给定一个字符数组,由'X'和'O'组成,找到所有被x包围的o并将其替换为x * 思路:只要替换被包围的o就行,如果有一个o是边界或者上下左右中有一个是o且这个o不会被替换,则该点也不会被替换 * 从四条边开始,因为在这4周的一定不是

【leetcode】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

Java for LeetCode 130 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 XX O O XX X O XX O X X After running your function, the board should be:

LeetCode[BFS]: 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

leetcode 130 Surrounded Regions(BFS)

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

【leetcode】Surrounded Regions(middle)☆

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