详细见:leetcode.com/problems/surrounded-regions
Java Solution: github
package leetcode;
/*
Given a 2D board containing 'X' and 'O' (the letter 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
*/
/**
* @author zxwtry
* @email zxwtry@qq.com
* @project OJ
* @package leetcode
* @file P130_SurroundedRegions.java
* @type P130_SurroundedRegions
* @date 2017年5月10日 上午11:20:47
* @details Solution: AC 4ms 95.29%
*/
public class P130_SurroundedRegions {
static class Solution {
int rn = 0, cn = 0;
char[][] b = null;
public void solve(char[][] b) {
int i = 0, j = 0;
this.b = b;
rn = b == null ? 0 : b.length;
if (rn < 3) return;
cn = b[0] == null ? 0 : b[0].length;
if (cn < 3) return;
for (i = 0; i < rn; i ++) {
search(i, 0);
search(i, cn-1);
}
for (j = 1; j < cn-1; j ++) {
search(0, j);
search(rn-1, j);
}
for (i = 0; i < rn; i ++) {
for (j = 0; j < cn; j ++) {
if (b[i][j] == 'O') {
b[i][j] = 'X';
} else if (b[i][j] == '1') {
b[i][j] = 'O';
}
}
}
}
void search(int i, int j) {
if (b[i][j] != 'O') return;
b[i][j] = '1';
if(i > 1)search(i-1, j);
if(i+1 < rn)search(i+1, j);
if(j > 1)search(i, j-1);
if(j+1 < cn)search(i, j+1);
}
}
}
C Solution: github
/*
url: leetcode.com/problems/surrounded-regions
*/
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <set>
using namespace std;
int rn = 0;
int cn = 0;
vector<vector<char > >* t;
class Solution {
public:
void solve(vector<vector<char > >& b) {
rn = b.size();
if (rn < 3) return;
cn = b[0].size();
if (cn < 3) return;
t = &b;
int i = 0, j = 0;
for (i = 0; i < rn; i ++) {
search(i, 0);
search(i, cn - 1);
}
for (j = 1; j < cn-1; j ++) {
search(0, j);
search(rn-1, j);
}
for (i = 0; i < rn; i ++) {
for (j = 0; j < cn; j ++) {
if (b[i][j] == 'O') {
b[i][j] = 'X';
} else if (b[i][j] == '1') {
b[i][j] = 'O';
}
}
}
}
void search(int i, int j) {
if ((*t)[i][j] != 'O') return;
int x, y;
queue<int > qx;
queue<int > qy;
qx.push(i);
qy.push(j);
while (! qx.empty()) {
x = qx.front();
y = qy.front();
(*t)[x][y] = '1';
qx.pop();
qy.pop();
if (x > 0 && (*t)[x-1][y] == 'O') {
qx.push(x-1);
qy.push(y);
}
if (x+1 < rn && (*t)[x+1][y] == 'O') {
qx.push(x+1);
qy.push(y);
}
if (y > 0 && (*t)[x][y-1] == 'O') {
qx.push(x);
qy.push(y-1);
}
if (y+1 < cn && (*t)[x][y+1] == 'O') {
qx.push(x);
qy.push(y+1);
}
}
}
};
int main() {
char* b[4] = {"XXXX", "XOOX", "XXOX", "XOXX"};
int rn = 4;
int cn = 4;
int i = 0, j = 0;
char** c = (char**) malloc(sizeof(char*) * rn);
for (i = 0; i < rn; i ++) {
c[i] = (char*) malloc(sizeof(char) * (cn+1));
c[i][cn] = '\0';
for (j = 0; j < cn; j ++) {
c[i][j] = b[i][j];
}
}
for (i = 0; i < rn; i ++) {
printf("%s\n", c[i]);
}
return 0;
}
Python Solution: github
#coding=utf-8
'''
url: leetcode.com/problems/surrounded-regions
@author: zxwtry
@email: zxwtry@qq.com
@date: 2017年5月13日
@details: Solution: 146ms 80.52%
'''
class Solution(object):
def solve(self, b):
"""
:type b: List[List[str]]
:rtype: void Do not return anything, modify b in-place instead.
"""
rn = 0 if b == None else len(b)
if rn < 3: return
cn = 0 if b[0] == None else len(b[0])
if cn < 3: return
save = [ij for k in range(rn+cn) for ij in ((0, k), (rn-1, k), (k, 0), (k, cn-1))]
while save:
i, j = save.pop()
if 0 <= i < rn and 0 <= j < cn and b[i][j] == 'O':
b[i][j] = '1'
save += (i, j-1), (i, j+1), (i-1, j), (i+1, j)
for i in range(rn):
for j in range(cn):
if b[i][j] == '1':
b[i][j] = 'O'
elif b[i][j] == 'O':
b[i][j] = 'X'