leetcode130
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的更多相关文章
- LeetCode130:Surrounded Regions
题目: Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is capt ...
- [Swift]LeetCode130. 被围绕的区域 | Surrounded Regions
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- 【1】【leetcode-130】 被围绕的区域
(DFS思路对,写复杂了) 给定一个二维的矩阵,包含 'X' 和 'O'(字母 O). 找到所有被 'X' 围绕的区域,并将这些区域里所有的 'O' 用 'X' 填充. 示例: X X X X X O ...
- leetCode130. Surrounded Regions--广度优先遍历算法
Problem: Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by ' ...
随机推荐
- proc文件系统漫谈
1. /proc/buddyinfo:/proc/buddyinfo是linuxbuddy系统管理物理内存的debug信息. 在linux中使用buddy算法解决物理内存的外碎片问题,其把所有空闲的内 ...
- MySQL Disk--SSD磁盘性能抖动问题
============================================================= SSD性能 空盘性能:SSD出厂时磁盘没有任何数据情况下的性能 稳态性能:当 ...
- smarty学习——基本概念
学习一种框架,我们最基本的就是掌握框架的思想,同时了解框架的基本语法. 1.对于定界符的了解 有的smarty模板标签都被加上了定界符. 默认情况下是 { 和},但它们是可被改变的.例如,我们假定你在 ...
- 2014华为机试西安地区A组试题
2014华为机试西安地区A组试题 题目一.分苹果 M个同样苹果放到N个同样篮子里有多少种放法,同意有篮子不放. 1<=M<=10.1<=N<=10 比如5个苹果三个篮子,3,1 ...
- JS 中 this 的用法
this是JavaScript语言中的一个关键字 他是函数运行时,在函数体内部自动生成的一个对象, 只能在函数体内部使用. 在不同function中, this有不同的值. 1. 纯粹的函数调用. f ...
- 【转】每天一个linux命令(13):less 命令
原文网址:http://www.cnblogs.com/peida/archive/2012/11/05/2754477.html less 工具也是对文件或其它输出进行分页显示的工具,应该说是lin ...
- DBLinq (MySQL exactly) Linq To MySql
http://blog.csdn.net/feihu_guest/article/details/7346948 DBLinq (MySQL exactly) Linq To MySql http:/ ...
- codevs 1131 统计单词数
#include<iostream> #include<string> using namespace std; int main() { string s, s0; getl ...
- Zigbee 的 mesh功能设置
1. 在编译选项中加入ZIGBEEPRO 最大的节点深度为20,网络模式为mesh网络 如果不配置,则为HOME_CONTROLS,支持5级路由深度,每个路由器最多可连接20个节点(最多包括6个路由器 ...
- view的setTag() 和 getTag()应用 ViewHolder
转自 http://www.cnblogs.com/qingblog/archive/2012/07/03/2575140.html View中的setTag(Onbect)表示给View添加一个格外 ...