LeetCode算法搜索
来源:互联网 发布:电火花成型加工编程 编辑:程序博客网 时间:2024/06/07 16:29
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 XX O O XX X O XX O X X
After running your function, the board should be:
X X X XX X X XX X X XX O X X
class Solution {
public:
int dire[4][2] = { { 0, 1 }, { 0, -1 }, { -1, 0 }, {1,0} };
bool isplace(vector<vector<char>>& board,int m, int n)//防止越界
{
int row = board.size();
int col = board[0].size();
return !(m>=row||m<0||n>=col||n<0);
}
void dfs(vector<vector<char>>& board,int x,int y)
{
for (int i = 0; i < 4; i++)
{
int nx = x + dire[i][0];
int ny = y + dire[i][1];
if (isplace(board, nx, ny)&&board[nx][ny]=='O')
{
board[nx][ny] = '*';
dfs(board, nx, ny);
}
}
}
void solve(vector<vector<char>> &board) {
int row = board.size();
if (!row)
return ;
int col = board[0].size();
for (int i = 0; i < col; i++)
{//从最上一行开始搜索
if (board[0][i] == 'O')
{
board[0][i] = '*';
dfs(board, 0, i);
}
}
for (int j = 0; j <row; j++)//左边
{
if (board[j][0]=='O')
{
board[j][0] = '*';
dfs(board, j, 0);
}
}
for (int i = 0; i < col; i++)
{
if (board[row - 1][i] == 'O')
{
board[row - 1][i] = '*';
dfs(board,row-1,i);
}
}
for (int j = 0; j < row; j++)
{
if (board[j][col - 1] == 'O')
{
board[j][col - 1] = '*';
dfs(board, j, col - 1);
}
}
//选择恢复
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (board[i][j] == '*')
board[i][j] = 'O';
else if(board[i][j]=='O')
{
board[i][j] = 'X';
}
}
}
}
};
- LeetCode算法搜索
- 算法思想-深度搜索算法-leetcode相关题目总结
- LeetCode 28. Implement strStr暴力搜索和KMP算法
- 广度优先、深度优先搜索算法——LeetCode
- 78. Subsets 【LeetCode算法之旅之深度优先搜索】
- 搜索算法
- 搜索算法
- 算法 搜索
- 搜索算法
- 搜索算法
- 搜索算法:IDA*算法
- 搜索算法:IDA*算法
- 搜索算法--线性搜索、二分搜索、内插搜索、剪枝搜索
- 【基础算法】搜索-二分搜索
- 搜索算法-搜索的优化
- [搜索算法]三分搜索初步
- 搜索算法----线性搜索、二叉搜索
- 搜索算法:顺序搜索和二分搜索
- Java单例设计模式
- C程序设计进阶week6(指针3)
- PMP模拟试题与解析(九)
- web服务程序设计探索(4)——数据总线-分层模型
- hdoj 2037
- LeetCode算法搜索
- 记账软件_强迫证处女座的福音_功能不是最强大,绝对最实用的记账软件
- 使用Windows远程桌面(mstsc)通过RDP协议访问Ubuntu/Debian服务器 时间:2015-05-16来源:linux网站 作者:wangxiaoyu 觉得通过Windows原生的远程
- hdoj 2051
- Scala基础—集合函数式编程示例(占位符的使用示例)
- redis scan 命令遇到的坑总结
- CPU卡COS系统文件结构详解
- mvc 安装和卸载 bootstrap
- Mac brew 安装 php7 一些注意事项