LeetCode OJ-- Surrounded Regions **@
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 **@的更多相关文章
- [LeetCode] 130. Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...
- 【leetcode】Surrounded Regions
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- Leetcode 130. Surrounded Regions
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- 【leetcode】Surrounded Regions(middle)☆
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- 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 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 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 ...
- [LeetCode] Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- 验证LeetCode Surrounded Regions 包围区域的DFS方法
在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ ...
- 【LeetCode】130. Surrounded Regions (2 solutions)
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
随机推荐
- python * args和** kwargs的用法
所属网站分类: python基础 > 函数 作者:慧雅 链接: http://www.pythonheidong.com/blog/article/18/ 来源:python黑洞网 www.py ...
- poj 3292 H-素数问题 扩展艾氏筛选法
题意:形似4n+1的被称作H-素数,两个H-素数相乘得到H-合成数.求h范围内的H-合成数个数 思路: h-素数 ...
- nova boot添加volume_type参数支持
早前由于添加了全SSD的高性能Ceph集群,区别于现有的HDD集群,在OpenStack端需要能够选择使用两种集群.Cinder配置多Ceph后端的文档早已整理,整理文件夹时发现这篇为nova boo ...
- 笔记-python-standard library-12.1 pickle
笔记-python-standard library-12.1 pickle 1. pickle简介 source code: Lib/pickle.py pickle模块实质上是一个实现p ...
- Contest - 中南大学第六届大学生程序设计竞赛(Semilive)
题1:1160十进制-十六进制 注意他给的数据范围 2^31,int是 2^31-1 #include<iostream> using namespace std; int main() ...
- Python基础-week07 Socket网络编程
一 客户端/服务器架构 1.定义 又称为C/S架构,S 指的是Server(服务端软件),C指的是Client(客户端软件) 本章的中点就是教大写写一个c/s架构的软件,实现服务端软件和客户端软件基于 ...
- Bat windows 批处理 常用命令
设置全屏: To make all bat files fullscreen: reg add HKCU\Console\ /v Fullscreen /t REG_DWORD /d /f To ma ...
- c++ primer plus 第6版 部分三 9章 - 章
c++ primer plus 第6版 部分三 9章 - 章 第9章 内存模型和名称空间 1.单独编译 ...
- POJ 2836:Rectangular Covering(状态压缩DP)
题目大意:在一个平面内有若干个点,要求用一些矩形覆盖它们,一个矩形至少覆盖两个点,可以相互重叠,求矩形最小总面积. 分析: 数据很小,很容易想到状压DP,我们把点是否被覆盖用0,1表示然后放在一起得到 ...
- Dos中查看mysql数据时 中文乱码
使用jsp页面查看数据时可以正确显示中文,但是dos窗口查看数据时中文显示乱码. 上网查了一下原因:之所以会显示乱码,就是因为MySQL客户端输出窗口显示中文时使用的字符编码不对造成的,可以使用如下的 ...