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. pandas库Series类型与基本操作

    pandas读取excel的类型是dataFrame,然后提取每一列是一个Series类型 Series类型包括index和values两部分 a = pd.Series({'a':1,'b':5}) ...

  2. 4 Values whose Sum is 0 POJ - 2785

    4 Values whose Sum is 0 Time Limit: 15000MS   Memory Limit: 228000K Total Submissions: 29243   Accep ...

  3. excel VBA 将文本数值转换为数字格式(单元格中数据左上角是绿三角,鼠标点上有叹号标示)

    Range("A6").SelectSelection.CopyRange("A10:A60").SelectRange(Selection, Selectio ...

  4. Delphi字符串处理函数

    1.Copy 功能说明:该函数用于从字符串中复制指定范围中的字符.该函数有3个参数.第一个参数是数据源(即被复制的字符串),第二个参数是从字符串某一处开始复制,第三个参数是要复制字符串的长度(即个数) ...

  5. ADO之密码验证--3次错误就锁定『改进』

    这里使用了SqlHelper,简化程序 自己写一个SqlHelper,把数据库的连接等都写到里面去. 首先把连接字符串添加到配置文件里去,右键解决方案-->添加新建项-->选择应用程序配置 ...

  6. 静态文本框控件的美化CStatic

    VC通用控件都是灰色,当对程序界面进行美化时,使用通用控件就和美化后的程序界面不搭配,在VB,C#中,可以很方便的更改控件背景颜色,但在VC中就不能,需要我们自己来完善这方面的功能.我在这只简单的介绍 ...

  7. python中subprocess.Popen的args和shell参数的使用

    subprocess模块定义了一个类: Popen class subprocess.Popen( args,        bufsize=0,        executable=None,    ...

  8. Selenium中如何运行 auto.exe 文件

    Runtime exe = Runtime.getRuntime(); try{ String str = "D:\\Auto上传文件\\photo.exe"; exe.exec( ...

  9. Python-S9—Day86-ORM项目实战之会议室预定相关

    01 会议室预定1 02 会议室预定2 03 会议室预定3 04 会议室预定4 05 会议室预定5 06 会议室预定6 01 会议室预定1 1.1 项目的架构目录: 1.2 使用Pycharm创建Dj ...

  10. CSU-1982 小M的移动硬盘

    CSU-1982 小M的移动硬盘 Description 最近小M买了一个移动硬盘来储存自己电脑里不常用的文件.但是他把这些文件一股脑丢进移动硬盘后,觉得这些文件似乎没有被很好地归类,这样以后找起来岂 ...