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 ...
随机推荐
- sed速查手册
1. Sed简介sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后, ...
- 关于在namanode上编写脚本控制DataNode的...
脚本如下:(我的虚拟机名字分别为:wang201 wang 202 wang 203 wang 204) params=$@ i= ; i <= ; i++)) ; do echo ====== ...
- Python学习笔记(三):文件和集合操作
python string与list互转 因为python的read和write方法的操作对象都是string.而操作二进制的时候会把string转换成list进行解析,解析后重新写入文件的时候,还得 ...
- Special Segments of Permutation - CodeForces - 1156E (笛卡尔树上的启发式合并)
题意 给定一个全排列\(a\). 定义子区间\([l,r]\),当且仅当\(a_l + a_r = Max[l,r]\). 求\(a\)序列中子区间的个数. 题解 笛卡尔树上的启发式合并. \(200 ...
- JVM——自定义类加载器
)以上两种情况在实际中的综合运用:比如你的应用需要通过网络来传输 Java 类的字节码,为了安全性,这些字节码经过了加密处理.这个时候你就需要自定义类加载器来从某个网络地址上读取加密后的字节代码,接着 ...
- itchat 总结(转)
python实现微信接口(itchat) 安装 sudo pip install itchat 登录 itchat.auto_login() 这种方法将会通过微信扫描二维码登录,但是这种登录的方式确实 ...
- cglib动态代理之原理说明
cglib采用了非常底层的字节码技术,通过目标类的字节码,为目标类创建子类,并在子类中用方法拦截技术,拦截所有父类方法的调用,并对拦截方法进行增强. 1)底层采用字节码框架ASM,来转换字节码来生成新 ...
- Jetty,Tomcat对MIME协议的配置参数说明
此处做一下小的汇总,针对Jetty容器内,存在excel的xlsx文件直接通过链接的方式下载的时候,如果是在Chrome浏览器时,则直接触发浏览器的下载行为,但是在IE11的浏览器上,则浏览器会直 ...
- loj2057 「TJOI / HEOI2016」游戏
记横联通是一块横着的没有硬石头的地,把他们编号.竖联通同理. 对于一个空地,将其横联通编号和竖联通编号连边,二分图匹配,最大匹配为答案. #include <iostream> #incl ...
- 云计算之路-阿里云上:用上了开放缓存服务OCS
你知道在我们使用的云服务器中哪台最贵吗?跑memcached的缓存服务器(12G内存).你知道保证网站访问速度的功臣之一是谁吗?跑memcached的缓存服务器. 用云服务器这么高贵的内存跑memca ...