CODE 3: Surrounded Regions

原创 2013年09月09日 21:30:24

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

我的代码如下:

	public void solve(char[][] board) {
		if (null == board || board.length == 0) {
			return;
		}
		int m = board.length;
		int n = board[0].length;
		Queue<Integer> queue = new LinkedList<Integer>();

		for (int i = 0; i < m; i++) {
			if (board[i][0] == 'O') {
				queue.offer(i * m);
			}

			if (board[i][n - 1] == 'O') {
				queue.offer(i * m + n - 1);
			}
		}

		for (int j = 1; j < n - 1; j++) {
			if (board[0][j] == 'O') {
				queue.offer(j);
			}

			if (board[m - 1][j] == 'O') {
				queue.offer((m - 1) * m + j);
			}
		}

		while (!queue.isEmpty()) {
			int all = queue.poll();
			int x = all / m;
			int y = all % m;
			board[x][y] = 'L';
			for (int i = -1; i <= 1; i++) {
				for (int j = -1; j <= 1; j++) {
					if (x + i < 0 || x + i > m - 1 || y + j < 0
							|| y + j > n - 1 || (Math.abs(i + j) != 1)) {
						continue;
					}
					int tmpAll = (x + i) * m + y + j;
					if (board[x + i][y + j] == 'O') {
						queue.offer(tmpAll);
					}
				}
			}
		}

		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				board[i][j] = board[i][j] == 'L' ? 'O' : 'X';
			}
		}
	}

主要思考如下:

1. 这道题首先使用了dfs完成,但这种方法使用了递归所以没有通过大数据集中大数据的测试;

2. 然后使用了bfs完成,但是同样的判断语句出现了很多次,也会导致运行时间过长,具体的问题代码如下:

	public void solve(char[][] board) {
		if (null == board || board.length == 0) {
			return;
		}
		int m = board.length;
		int n = board[0].length;
		Queue<Integer> queue = new LinkedList<Integer>();

		for (int i = 0; i < m; i++) {
			if (board[i][0] == 'O') {
				queue.offer(i * m);
			}

			if (board[i][n - 1] == 'O') {
				queue.offer(i * m + n - 1);
			}
		}

		for (int j = 1; j < n - 1; j++) {
			if (board[0][j] == 'O') {
				queue.offer(j);
			}

			if (board[m - 1][j] == 'O') {
				queue.offer((m - 1) * m + j);
			}
		}

		while (!queue.isEmpty()) {
			int all = queue.poll();
			int x = all / m;
			int y = all % m;
			board[x][y] = 'L';
			if (x > 0 && board[x - 1][y] == 'O') {
				queue.offer((x - 1) * m + y);
			}
			if (x < m - 1 && board[x + 1][y] == 'O') {
				queue.offer((x + 1) * m + y);
			}
			if (y > 0 && board[x][y - 1] == 'O') {
				queue.offer(x * m + y - 1);
			}
			if (y < n - 1 && board[x][y + 1] == 'O') {
				queue.offer(x * m + y + 1);
			}
		}

		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				board[i][j] = board[i][j] == 'L' ? 'O' : 'X';
			}
		}
	}
具体是在while循环中有过多的if判断,这样导致运算次数多,若是将这些判断语句提到最前面则会减少运算次数;

3. 在leetcode上貌似直接使用dfs和bfs是不行了,必须改进,比如改为非递归或者剪枝等等。

颜色英文代码全集

redgreenbluemagentayellowchocolateblackaquamarinelimefuchsiabrassazurebrownbronzedeeppinkalicebluegr...
  • denal
  • denal
  • 2003-03-09 11:10:00
  • 668

130 Surrounded Regions

题目链接:https://leetcode.com/problems/surrounded-regions/题目:Given a 2D board containing 'X' and 'O', ca...
  • ChilseaSai
  • ChilseaSai
  • 2015-12-21 22:43:22
  • 1143

[leetcode-130]Surrounded Regions(java)

问题描述: Given a 2D board containing ‘X’ and ‘O’, capture all regions surrounded by ‘X’.A region is ca...
  • zdavb
  • zdavb
  • 2015-08-12 20:39:51
  • 910

【LeetCode-面试算法经典-Java实现】【130-Surrounded Regions(环绕区域)】

【130-Surrounded Regions(环绕区域)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a 2D board containing 'X' ...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-15 06:14:08
  • 2450

(LeetCode 130) Surrounded Regions(BFS)

Q: Given a 2D board containing ‘X’ and ‘O’, capture all regions surrounded by ‘X’.A region is captu...
  • u014616233
  • u014616233
  • 2016-04-20 16:45:54
  • 862

LeetCode-130. Surrounded Regions (JAVA)(环绕区域)

LeetCode-130. Surrounded Regions (JAVA)(环绕区域)
  • mine_song
  • mine_song
  • 2017-04-17 12:16:17
  • 425

Leetcode Surrounded Regions 解题报告

http://oj.leetcode.com/problems/surrounded-regions/ Given a 2D board containing 'X' and 'O', captur...
  • worldwindjp
  • worldwindjp
  • 2014-02-15 20:57:18
  • 4716

Surrounded Regions -- LeetCode

原题链接: http://oj.leetcode.com/problems/surrounded-regions/  这个题目用到的方法是图形学中的一个常用方法:Flood fill算法,其实就...
  • linhuanmars
  • linhuanmars
  • 2014-04-04 01:12:07
  • 15279

Surrounded Regions BFS遍历

Surrounded Regions   Given a 2D board containing 'X' and 'O', capture all regions surrounded by ...
  • u012605629
  • u012605629
  • 2015-05-08 22:13:15
  • 274

LeetCode(130) Surrounded Regions

题目 Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by ...
  • fly_yr
  • fly_yr
  • 2016-08-15 22:04:38
  • 1128
收藏助手
不良信息举报
您举报文章:CODE 3: Surrounded Regions
举报原因:
原因补充:

(最多只允许输入30个字)