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 ' ...
随机推荐
- jsp内置对象request 和response
1.request对象主要用于处理客户端的请求 request对象常用方法 一.String request.getParameter(String name) 根据页面表单 ...
- Codelf 变量取名
Codelf 变量取名 可以看到别的变量是怎么命名的,站在巨人的肩膀上.
- 从操作系统rm数据文件后,利用句柄与rman恢复的过程。(已验证)
以下操作代码的流程是配的,但是相应的文件名,啥的 必须改动. 故障现象 数据文件被误删除 具体情况 接到反馈说,数据文件data20120512.dbf被误删除,需要恢复 数据库提示 ERROR ...
- 线性模型的fit,predict
线性模型的fit其实一个进行学习的过程,根据数据和标签进行学习:predict则是基于fit之后形成的模型,来决定指定的数据对应于标签(y_train_5)的值. 下面的是手写字母判断是否为“5” s ...
- Oracle的静默安装 升级和卸载 参考规范
Oracle的静默安装 升级和卸载 参考规范 20180912 V1 一.Oracle的安装 Oracle产品的三种安装方式分别为: 1.图形化(Java向导)安装引导 2.使用应答文件静默安装 3. ...
- Scrapy下xpath基本的使用方法
Scrapy是基于python的开源爬虫框架,使用起来也比较方便.具体的官网档:http://doc.scrapy.org/en/latest/ 之前以为了解python就可以直接爬网站了,原来还要了 ...
- 【python】实例-读取已有文件的内容
import os Filename=raw_input("please input filename that you will open: ") if os.path.exis ...
- 跟老齐学Django 项目实战笔记
创建项目 mysite 创建应用 blog mysit/settings.py配置app INSTALLED_APPS = [ 'django.contrib.admin', 'django.cont ...
- bzoj 4961: 除除除
Description 我们定义一种操作是将一个正整数n(n>1)用某个整数d替换,这个数必须是n的约数(1≤d≤n).给你一个正整数n, 你需要确定操作进行的期望次数,如果我们希望不断地使用这 ...
- [转]Java.APK 反编译
本文转自:http://blog.csdn.net/vipzjyno1/article/details/21039349/ (注:反编译不是让各位开发者去对一个应用破解搞重装什么的,主要目的是为了促进 ...