Leetcode 130 Surrounded Regions DFS
将内部的O点变成X
input
X X X X
X O O X
X X O X
X O X X
output
X X X X
X X X X
X X X X
X O X X
DFS的基本框架是
void dfs(int now,int d){
if(终止条件) {
做相应的操作;
return;
}
for(遍历所有now点的相邻点next){
if(!visit[next]) {
访问每个没有访问过的点;
做相应的操作;
dfs(next, d + );
}
}
}
DFS图所有边上的点,将边上的O以及其联通域变为T,然后将内部的O变为X,外部的T变为O,本质是种子填充算法。
此题对于is_in这个函数要注意, 不要用(x < m) && (x >= 0) && (y < n) && (y >= 0) 会堆栈溢出!!原因是会出现边上的点全是O。。。
class Solution {
public:
int m, n;
bool is_in(int x, int y)
{
return (x < m-) && (x >= ) && (y < n-) && (y >= );
}
void dfs(std::vector<std::vector<char>> &board,int x,int y)
{
//if (!(is_in(x, y) && board[x][y] == 'O')) return;
//printf("%d %d\n", x, y);
board[x][y] = 'T';
int dir[][] = { { , }, { -, }, { , }, { , - } };
for (int i = ; i < ; ++i){
int tx = x + dir[i][];
int ty = y + dir[i][];
if (is_in(tx, ty) && board[tx][ty] == 'O')
{
dfs(board, tx, ty);
}
}
}
void change(std::vector<std::vector<char>> &board)
{
for (int i = ; i < m;++i){
for (int j = ; j < n;++j){
if (board[i][j] == 'T') board[i][j] = 'O';
else if (board[i][j] == 'O') board[i][j] = 'X';
else;
}
}
}
void solve(std::vector<std::vector<char>> &board)
{
m = board.size();
if (m == ) return;
n = board[].size();
if (n == ) return;
for (int i = ; i < m;++i){
if (board[i][] == 'O') dfs(board, i, );
if (board[i][n - ] == 'O') dfs(board, i, n-);
}
for (int i = ; i < n; ++i){
if (board[][i] == 'O') dfs(board, , i);
if (board[m-][i] == 'O') dfs(board, m-, i);
}
change(board);
}
};
Leetcode 130 Surrounded Regions DFS的更多相关文章
- [LeetCode] 130. Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...
- leetcode 130 Surrounded Regions(BFS)
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- Leetcode 130. Surrounded Regions
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- Java for LeetCode 130 Surrounded Regions
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions
两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...
- 130. Surrounded Regions(M)
130.Add to List 130. Surrounded Regions Given a 2D board containing 'X' and 'O' (the letter O), capt ...
- [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- 【LeetCode】130. Surrounded Regions (2 solutions)
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- 【leetcode】Surrounded Regions
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
随机推荐
- TFS2010安装办法及序列号
安装过程: 一.安装操纵体系 安装Windows 2008 R2简体中文版 二.筹办安装过程中的须要的用户账户,并设置响应权限 具体流程如下: 1.点击“开端”——“经管对象”——“计较机经管” 2. ...
- Asp.net web api部署在某些服务器上老是404
asp.net web api部署在Windows服务器上后,按照WebAPI定义的路由访问,老是出现404,但定义一个静态文件从站点访问,却又OK. 这时,便可以确定是WebAPI路由出了问题,经调 ...
- unity3d使用脚本保存屏幕截图
using UnityEngine; using System.Collections; using System.IO; public class FrameAnimation : MonoBeha ...
- AngularJs自定义指令详解(3) - scope
我们之所以要定义指令,目的是重用指令.假设有这么一个应用场景:在同一个html里使用了两次my-directive,第一个my-directive要展示的是Hello World,第二个my-dire ...
- android studio小技巧
1. 为了防止乱码,请将 android studio 右下角编码设置成 UTF-8 2. Ctri + Q 查看api 3 Ctri + Shift +U 大小写互换 4 Ctrl + Alt + ...
- Linux下设置网卡随系统启动
在GUI下安装RHEL,在配置网卡的时候,有时候会忘了勾选网卡随系统自动启动,解决方法是系统启动后,打开网卡配置文件/etc/sysconfig/network-script/ifcfg-eth*,将 ...
- uploadify 上传遇到跨域问题
flash上传跨域,一般是加入crossdomain.xml 我用了别人的图片服务器,只能做此下策: //CURLFile 实现 $file = $_FILES['Filedata']['tmp_na ...
- imageNamed、imageWithContentsOfFile、imageWithData
[UIImage imageNamed:ImageName]; 1.加载图片占据的内存较大 2.相同的图片只会加载一份到内存中,如果同时使用,使用同一个对象即可 3.当对象销毁,图片对象不会随着一起销 ...
- DBAPI部署
1.添加源 sudo rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm ...
- 2.1 View的绘制
view绘制流程是从ViewRoot的performTraversals()方法中开始的,在该方法中会执行view绘制的三部曲,即:measure(测量视图的大小),layout(确定视图的位置)dr ...