Given a 2D board containing 'X' and '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

思路:

反向思考,先找到没有被包围的区域,标记为‘+’,再把标为‘+’的区域标为‘O',标为’O'的区域改为‘X'。没有被包围的区域一定是与最外圈的’O'相连的区域,所以要先遍历区域的上下左右边界,找到‘O'的地方。接下来的问题是如何把所有与最外层‘O'相连的区域标记上。有两种思路:BFS和DFS。

所谓BFS是指把当前标记位置的上下左右都标记一遍,然后再标记相邻点的上下左右位置。不需递归,用队列。

DFS是指把当前标记位置的向一个方向标记,比如一直向左,直到没有可标记的,再换一个方向。需要递归。

代码里面BFS可以通过,DFS栈溢出了。

void solve(vector<vector<char>> &board) {
if(board.size() == ) return;
int rowNum = board.size();
int colNum = board[].size();
//遍历最外面一圈,找‘O'
//最上
for(int j = ; j < colNum; j++)
{
if(board[][j] == 'O')
BFS(board, , j);
}
//最下
for(int j = ; j < colNum; j++)
{
if(board[rowNum - ][j] == 'O')
BFS(board, rowNum - , j);
}
//最左
for(int i = ; i < rowNum; i++)
{
if(board[i][] == 'O')
BFS(board, i, );
}
//最右
for(int i = ; i < rowNum; i++)
{
if(board[i][colNum - ] == 'O')
BFS(board, i, colNum - );
} for(int i = ; i < rowNum; i++)
{
for(int j = ; j < colNum; j++)
{
if(board[i][j] == 'O')
board[i][j] = 'X';
if(board[i][j] == '+')
board[i][j] = 'O';
}
} }
void DFS(vector<vector<char>> &board, int r, int c)
{
if(r >= && c >= && r < board.size() && c < board[].size() && board[r][c] == 'O')
{
board[r][c] = '+';
DFS(board, r - , c);
DFS(board, r + , c);
DFS(board, r, c - );
DFS(board, r, c + );
}
}
void BFS(vector<vector<char>> &board, int r, int c)
{
queue<pair<int, int>> q;
q.push(make_pair(r, c));
while(!q.empty())
{
int i = q.front().first;
int j = q.front().second;
q.pop();
if(i >= && j >= && i < board.size() && j < board[].size() && board[i][j] == 'O')
{
board[i][j] = '+';
q.push(make_pair(i - , j));
q.push(make_pair(i + , j));
q.push(make_pair(i, j - ));
q.push(make_pair(i, j + ));
} } }

上面的代码已经是优化过的了,我自己写的时候只写出了DFS的,而且自己也没有意识到是DFS。代码也很繁琐。注意通过把判断条件放在一起来简化代码。

我原本很挫的代码:栈溢出。

class Solution {
public:
void solve(vector<vector<char>> &board) {
if(board.size() == ) return;
int rowNum = board.size();
int colNum = board[].size();
//遍历最外面一圈,找‘O'
//最上
for(int j = ; j < colNum; j++)
{
if(board[][j] == 'O')
{
board[][j] = '+';
mySolve(board, , j);
}
}
//最下
for(int j = ; j < colNum; j++)
{
if(board[rowNum - ][j] == 'O')
{
board[rowNum - ][j] = '+';
mySolve(board, rowNum - , j);
}
}
//最左
for(int i = ; i < rowNum; i++)
{
if(board[i][] == 'O')
{
board[i][] = '+';
mySolve(board, i, );
}
}
//最右
for(int i = ; i < rowNum; i++)
{
if(board[i][colNum - ] == 'O')
{
board[i][colNum - ] = '+';
mySolve(board, i, colNum - );
}
} for(int i = ; i < rowNum; i++)
{
for(int j = ; j < colNum; j++)
{
if(board[i][j] == 'O')
board[i][j] = 'X';
if(board[i][j] == '+')
board[i][j] = 'O';
}
} }
void mySolve(vector<vector<char>> &board, int r, int c)
{
if(r - >= && board[r - ][c] == 'O') //上
{
board[r - ][c] == '+';
mySolve(board, r - , c);
}
if(r + < board.size() && board[r + ][c] == 'O') //下
{
board[r + ][c] == '+';
mySolve(board, r + , c);
}
if(c - >= && board[r][c - ] == 'O') //左
{
board[r][c - ] == '+';
mySolve(board, r, c - );
}
if(c + < board[].size() && board[r][c + ] == 'O') //下
{
board[r][c + ] == '+';
mySolve(board, r, c + );
}
}
};

【leetcode】Surrounded Regions(middle)☆的更多相关文章

  1. 【leetcode】Reverse Integer(middle)☆

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...

  2. 【leetcode】Reorder List (middle)

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...

  3. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  4. 【leetcode】Rotate List(middle)

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  5. 【leetcode】Partition List(middle)

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  6. 【leetcode】Spiral Matrix(middle)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  7. 【leetcode】Rotate Image(middle)

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  8. 【leetcode】Next Permutation(middle)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  9. 【leetcode】Reverse Bits(middle)

    Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...

随机推荐

  1. js反序列化时间

    var time = "/Date(1279270720000+0800)/"; var tme1 = ChangeDateFormat(time); alert(tme1); J ...

  2. AlwaysOn可用组基础知识

    AlwaysOn可用组概念 AlwaysOn作为SQL Server2012新功能,其目的主要为了替代镜像功能. AlwaysOn拥有镜像的同步和异地容灾功能,并能在镜像的基础上实现一对多的镜像副本同 ...

  3. Java并发包源码学习之AQS框架(三)LockSupport和interrupt

    接着上一篇文章今天我们来介绍下LockSupport和Java中线程的中断(interrupt). 其实除了LockSupport,Java之初就有Object对象的wait和notify方法可以实现 ...

  4. PHP基础之 继承(一)

    ========================================= *                 继承 extends *============================ ...

  5. hash-2.hashMap

    1.HashMap的数据结构 a.HashMap是一个链表散列的结合体,即,数组和链表的结合体. b.HashMap类中定义了Entry类型的数组,Entry [ ] ,Entry有key value ...

  6. C# 读写App.config

    Jul142013 [C#] 读写App.config配置文件的方法 作者:xieyc   发布:2013-07-14 17:29   字符数:3433   分类:编程   阅读: 39,139 次 ...

  7. rem和em,px的使用

    rem是CSS3中新增加的一个单位值,他和em单位一样,都是一个相对单位.不同的是em是相对于元素的父元素的font-size进行计算:rem是相对于根元素html的font-size进行计算.这样一 ...

  8. ajax读取XML文本(如读取城市)

    //加载城市 function loadArea_pep() { $.ajax({ url: "/xmlFile/crty.xml", success: function (res ...

  9. iOS开发——多线程篇——多线程介绍

    一.进程和线程1.什么是进程进程是指在系统中正在运行的一个应用程序每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内 比如同时打开迅雷.Xcode,系统就会分别启动2个进程 通过“活动监 ...

  10. iOS开发——UI进阶篇(十八)核心动画小例子,转盘(裁剪图片、自定义按钮、旋转)图片折叠、音量震动条、倒影、粒子效果

    一.转盘(裁剪图片.自定义按钮.旋转) 1.裁剪图片 将一张大图片裁剪为多张 // CGImageCreateWithImageInRect:用来裁剪图片 // image:需要裁剪的图片 // re ...