https://oj.leetcode.com/problems/surrounded-regions/

棋盘类的题目。找出所有没有被 X 包围的 O

使用深搜,但是太深了,run time error

可以自己写个栈,模拟深搜,取数据存入,弹出的

后来参考了九章

struct point{
int x;
int y;
point(int x, int y)
{
this->x = x;
this->y = y;
}
}; class Solution {
public:
queue<point> myQueue;
int row;
int col; void solve(vector<vector<char>> &board) {
if(board.empty() || board.size() == || board[].size() == )
return; row = board.size();
col = board[].size(); // push 边上的 O in queue
for(int i = ; i < row; i++)
{
if(board[i][] == 'O')
myQueue.push(point(i,));
if(col >= && board[i][col - ] == 'O')
myQueue.push(point(i,col-));
}
for(int j = ; j< col; j++)
{
if(board[][j] == 'O')
myQueue.push(point(,j));
if(board[row-][j] == 'O')
myQueue.push(point(row-,j));
}
point tempPoint(,);
// 对queue中的每一个位置,处理它的上下左右
while(myQueue.empty() == false)
{
tempPoint = myQueue.front();
myQueue.pop();
int x = tempPoint.x;
int y = tempPoint.y; board[x][y] = 'K'; // mark as K, which can't be chagned into X in the end
//left
if(y >= && board[x][y-] == 'O')
myQueue.push(point(x,y-));
//right
if(y+ < col && board[x][y+] == 'O')
myQueue.push(point(x,y+));
//up
if(x- >= && board[x-][y] == 'O')
myQueue.push(point(x-,y));
//down
if(x+ < row && board[x+][y] == 'O')
myQueue.push(point(x+,y));
} for(int i = ; i < row; i++)
for(int j = ; j < col; j++)
{
if(board[i][j] == 'O')
board[i][j] = 'X';
else if(board[i][j] == 'K')
board[i][j] = 'O';
}
return;
} };

下面是我的解法,但是测试数据大的时候过不去。。。

class Solution {
public:
vector<vector<bool> > isSafe;
vector<vector<char> > board;
vector<vector<bool> > visited; void solve(vector<vector<char>> &board) {
if(board.empty() || board.size() == || board[].size() == )
return; this->board = board; isSafe.resize(board.size());
visited.resize(board.size()); // init
for(int i = ; i < board.size(); i++)
{
isSafe[i].resize(board[].size());
visited[i].resize(board[].size()); for(int j = ; j < board[].size(); j++)
{
isSafe[i][j] = true;
if(board[i][j] == 'X')
visited[i][j] = true;
else
visited[i][j] = false;
}
} for(int i = ; i < board.size(); i++)
for(int j = ; j < board[].size(); j++)
{
if(!visited[i][j])
isSurround(i,j);
} //isSurround(0,0); for(int i = ; i < board.size(); i++)
for(int j = ; j < board[].size(); j++)
if(isSafe[i][j])
board[i][j] = 'X'; return;
} void isSurround(int i, int j)
{
if(i == board.size())
return; // handle 边
if(board[i][j] == 'O' && (i == || i == board.size() - || j == || j == board[].size() - ))
{
isSafe[i][j] = false;
visited[i][j] = true;
} if(isSafe[i][j] == false)
{
// mark right and down
for(int p = j + ; p < board[].size(); p++)
{
if(board[i][p] == 'O')
{
isSafe[i][p] = false;
visited[i][p] = true;
}
else
break;
}
for(int q = i + ; q < board.size(); q++)
{
if(board[q][j] == 'O')
{
isSafe[q][j] = false;
visited[q][j] = true;
}
else
break;
}
}
else if(board[i][j] == 'O')
{
visited[i][j] = true; // left
if(j > && board[i][j-] == 'O' && !visited[i][j-])
isSurround(i,j-);
if(j > && isSafe[i][j-] == false)
{
isSafe[i][j] = false;
return;
}
// up
if(i > && board[i-][j] == 'O' && !visited[i-][j])
isSurround(i-,j);
if(i > && isSafe[i-][j] == false)
{
isSafe[i][j] = false;
return;
} // right is O
if((j < board[].size() - ) &&board[i][j+] == 'O' && !visited[i][j+])
isSurround(i,j+);
if((j < board[].size() - ) && isSafe[i][j+] == false)
{
isSafe[i][j] = false;
return;
}
// down is O
if((i < board.size() - ) && board[i+][j] == 'O' && !visited[i+][j])
isSurround(i+,j);
if((j < board.size() - ) && isSafe[i+][j] == false)
{
isSafe[i][j] = false;
return;
} visited[i][j] = true;
} } };

LeetCode OJ-- Surrounded Regions **@的更多相关文章

  1. [LeetCode] 130. Surrounded Regions 包围区域

    Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...

  2. 【leetcode】Surrounded Regions

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

  3. Leetcode 130. Surrounded Regions

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  4. 【leetcode】Surrounded Regions(middle)☆

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  5. Java for LeetCode 130 Surrounded Regions

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  6. leetcode 130 Surrounded Regions(BFS)

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  7. Leetcode 130 Surrounded Regions DFS

    将内部的O点变成X input X X X XX O O X X X O XX O X X output X X X XX X X XX X X XX O X X DFS的基本框架是 void dfs ...

  8. [LeetCode] Surrounded Regions 包围区域

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  9. 验证LeetCode Surrounded Regions 包围区域的DFS方法

    在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ ...

  10. 【LeetCode】130. Surrounded Regions (2 solutions)

    Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...

随机推荐

  1. psutil模块的基础使用

    注:Python并没有自带psutil模块,需要自己去安装 安装psutil模块 pip install psutilorpip3 install psutil 一.导入模块 import psuti ...

  2. Java消息中间件--初级篇

    一. 为什么使用消息中间件? 假设用户登录系统   传统方式 用户登录  调用短息服务   积分服务  日志服务等各种服务  如果短息服务出现问题就无法发送短信而且用户登录成功必须所有调用全部完成返回 ...

  3. java中equals和==

    https://www.cnblogs.com/bluestorm/archive/2012/03/02/2377615.html

  4. LeetCode118 杨辉三角 Python

    给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 示例:输入: 5 输出: [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] cl ...

  5. Java学习3之成员方法及函数重载

    方法的定义:方法名称,返回值,参数列表,修饰符(权限修饰符,final,static),实现体. 参考自:<Java 程序设计与工程实践> 方法的签名: 唯一区别其他方法的元素:(1)方法 ...

  6. [python][django学习篇][14]markdown 代码高亮

    1 修改detail视图函数,渲染文件的时候,增加codehight拓展 post.body = markdown.markdown(post.body, extensions=[ 'markdown ...

  7. 位图 c++ 位图排序

    什么是位图?来自http://www.cnblogs.com/dolphin0520/archive/2011/10/19/2217369.html 位图就是用一个bit来标记某个元素对应的值,键值就 ...

  8. 软工实践 - 第九次作业 Alpha 冲刺 (1 / 10)

    队名:起床一起肝活队 组长博客:https://www.cnblogs.com/dawnduck/p/9949350.html 作业博客:(班级博客本次作业的链接) 组员情况 组员1(队长):白晨曦 ...

  9. loadrunner rtsp协议模拟

    在核心网做过3年的sip消息模拟,所以rtsp消息模拟只要知道信令消息交互就非常顺利了 RTSP 实时流传输协议, 是TCP/IP协议体系中的一个应用层协议, 该协议定义了一对多应用程序如何有效地通过 ...

  10. 【bzoj3555】[Ctsc2014]企鹅QQ 字符串hash

    题目描述 PenguinQQ是中国最大.最具影响力的SNS(Social Networking Services)网站,以实名制为基础,为用户提供日志.群.即时通讯.相册.集市等丰富强大的互联网功能体 ...