C++实现,使用BFS:

struct POS
{
int x;
int y;
POS(int newx, int newy): x(newx), y(newy) {}
}; class Solution {
public:
void solve(vector<vector<char>> &board) {
if(board.empty() || board[].empty())
return;
int m = board.size();
int n = board[].size();
for(int i = ; i < m; i ++)
{
for(int j = ; j < n; j ++)
{
if(board[i][j] == 'O')
{
if(i == || i == m- || j == || j == n-)
{// remain 'O' on the boundry
bfs(board, i, j, m, n);
}
}
}
}
for(int i = ; i < m; i ++)
{
for(int j = ; j < n; j ++)
{
if(board[i][j] == 'O')
board[i][j] = 'X';
else if(board[i][j] == '*')
board[i][j] = 'O';
}
}
}
void bfs(vector<vector<char>> &board, int i, int j, int m, int n)
{
stack<POS*> stk;
POS* pos = new POS(i, j);
stk.push(pos);
board[i][j] = '*';
while(!stk.empty())
{
POS* top = stk.top();
if(top->x > && board[top->x-][top->y] == 'O')
{
POS* up = new POS(top->x-, top->y);
stk.push(up);
board[up->x][up->y] = '*';
continue;
}
if(top->x < m- && board[top->x+][top->y] == 'O')
{
POS* down = new POS(top->x+, top->y);
stk.push(down);
board[down->x][down->y] = '*';
continue;
}
if(top->y > && board[top->x][top->y-] == 'O')
{
POS* left = new POS(top->x, top->y-);
stk.push(left);
board[left->x][left->y] = '*';
continue;
}
if(top->y < n- && board[top->x][top->y+] == 'O')
{
POS* right = new POS(top->x, top->y+);
stk.push(right);
board[right->x][right->y] = '*';
continue;
}
stk.pop();
}
}
};

补充一个python的实现,使用DFS:

 class Solution:
def dfs(self,board,i,j,m,n,visited,direction):
if i < 0 or i >= m or j < 0 or j >= n or visited[i][j] == 1:
return
if board[i][j] == 'O':
board[i][j] = '*'
visited[i][j] = 1 for dirt in direction:
if dirt[0] == 0 and dirt[1] == 0:
continue
x = i + dirt[0]
y = j + dirt[1]
self.dfs(board,x,y,m,n,visited,direction) def solve(self, board: 'List[List[str]]') -> None:
m = len(board)#行
if m == 0:
return
n = len(board[0])#列
if n == 0:
return
visited = [[0 for _ in range(n)]for _ in range(m)]
direction = [[-1,0],[1,0],[0,-1],[0,1]]
for i in range(m):
for j in range(n):
if board[i][j] == 'O' and (i == 0 or i == m-1 or j == 0 or j == n-1):
self.dfs(board,i,j,m,n,visited,direction) for i in range(m):
for j in range(n):
if board[i][j] == 'O':
board[i][j] = 'X'
elif board[i][j] == '*':
board[i][j] = 'O'

leetcode130的更多相关文章

  1. LeetCode130:Surrounded Regions

    题目: Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is capt ...

  2. [Swift]LeetCode130. 被围绕的区域 | Surrounded Regions

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  3. 【1】【leetcode-130】 被围绕的区域

    (DFS思路对,写复杂了) 给定一个二维的矩阵,包含 'X' 和 'O'(字母 O). 找到所有被 'X' 围绕的区域,并将这些区域里所有的 'O' 用 'X' 填充. 示例: X X X X X O ...

  4. leetCode130. Surrounded Regions--广度优先遍历算法

    Problem: Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by ' ...

随机推荐

  1. PyCharm首次使用(Hell world!)

    作为PyCharm编辑器的起步,我们理所当然的先写一个Hello word,并运行它.(此文献给对IDE不熟悉的初学者) 1,新建一个项目 File --> New Project... 2,新 ...

  2. JPEG文件格式

    格式:JFIF(JPEG档的交换格式)压缩:JPEG(灰阶影像压缩比约为10:1:彩色影像约为20:1)以JPEG文件格式保存的图像实际上是2个不同格式的混合物:JPEG格式规范本身,用来定义图像的压 ...

  3. mac上textstudio在系统升级以后不能编译

    不能工作的最主要原因为:os x改变了文件存放的路径.因此,需要修改setting中的command的路径,将原来的改为: /usr/local/texlive/2015/bin/x86_64-dar ...

  4. SQL Server MERGE

    Merge关键字是一个神奇的DML关键字.它在SQL Server 2008被引入,它能将Insert,Update,Delete简单的并为一句.MSDN对于Merge的解释非常的短小精悍:”根据与源 ...

  5. Linux性能测试工具安装全集

    stress 下载地址:http://people.seas.harvard.edu/~apw/stress/ 一.stress工具安装:1.获取stress源码安装包(stress-1.0.4.ta ...

  6. Oracle ADDM性能诊断利器及报告解读

    性能优化是一个永恒的话题,性能优化也是最具有价值,最值得花费精力深入研究的一个课题,因为资源是有限的,时间是有限的.在Oracle数据库中,随着Oracle功能的不断强大和完善,Oralce数据库在性 ...

  7. async(await)知识点

    async 函数是 Generator 函数的语法糖. async 函数对 Generator 函数的改进体现在: async 内置执行器. Generator 函数的执行必须靠执行器,需要调用 ne ...

  8. linux 知识点

    关于登录Linux时,/etc/profile.~/.bash_profile等几个文件的执行过程. 在登录Linux时要执行文件的过程如下: 在刚登录Linux时,首先启动 /etc/profile ...

  9. PHP查找中文字符的解决方案

    在PHP中查找中文字符,有两种方案.1.中文字符是gbk(gb2312)有两种解决方法第一种:将PHP保存为ASCII编码,然后使用strpos查找,如:strpos($curl_res, ‘哈哈’) ...

  10. Android自动化测试中AccessibilityService获取控件信息(1)

    Android自动化测试中AccessibilityService获取控件信息(1) 分类: android自动化测试2014-03-24 15:31 3455人阅读 评论(16) 收藏 举报 and ...