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 ...
随机推荐
- 迭代器Iterator与语法糖for-each
一.为什么需要迭代器 设计模式迭代器 迭代器作用于集合,是用来遍历集合元素的对象.迭代器迭代器不是Java独有的,大部分高级语言都提供了迭代器来遍历集合.实际上,迭代器是一种设计模式: 迭代器模式提供 ...
- 用iTerm快速链接远程服务器
通常情况下,iTerm2访问远程Linux使用ssh ssh <用户名>@<ip> 然后输入访问的密码即可.当然还有的时候需要指定访问端口. ssh -p <端口号> ...
- [USACO]奶牛博览会(DP)
Description 奶牛想证明他们是聪明而风趣的.为此,贝西筹备了一个奶牛博览会,她已经对N头奶牛进行了面试,确定了每头奶牛的智商和情商. 贝西有权选择让哪些奶牛参加展览.由于负的智商或情商会造成 ...
- poj1142 Smith Numbers
Poj1142 Smith Numbers Smith Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13854 ...
- Storm: 性能优化 (转载)
Storm 性能优化 原文地址:http://www.jianshu.com/p/f645eb7944b0 目录 场景假设 调优步骤和方法 Storm 的部分特性 Storm 并行度 Storm 消 ...
- 图学java基础篇之IO
java io体系 如图可以看出,java的io按照包来划分的话可以分为三大块:io.nio.aio,但是从使用角度来看,这三块其实揉杂在一起的,下边我们先来概述下这三块: io:主要包含字符流和字节 ...
- Python-S9—Day86-ORM项目实战之会议室预定相关
01 会议室预定1 02 会议室预定2 03 会议室预定3 04 会议室预定4 05 会议室预定5 06 会议室预定6 01 会议室预定1 1.1 项目的架构目录: 1.2 使用Pycharm创建Dj ...
- [HTTPS]pfx转jks
keytool -importkeystore -srckeystore src.pfx -srcstoretype pkcs12 -destkeystore trg.jks -deststoret ...
- Spread.js 上下级关系
- easyUI 接收Spring Mvc中@ResponseBody中文乱码解决
接触springMVC不够深入,乱码困扰我到深夜,特此留下记忆: @responsebody默认滴是ISO-8859-1 Controller注解参数 @ResponseBody 标注后返回Strin ...