leetcode 130: Surrounded Regions
2015-08-17 00:48
253 查看
The algorithm is not so hard to think. Just try from the boarder, if there is an 'O', DFS or BFS and change all 'O's in that region to another character. After that, change all 'O's to 'X' and all different characters (I use 'n') to 'O'. The trick when using
BFS here is to change the character before push into the queue, or it will push some positions that are already in the queue and finally cause TLE.
class Solution {
public:
void solve(vector<vector<char>>& board) {
if(board.empty())
return;
int m=board.size();
int n=board[0].size();
for(int i=0;i<m;i++)
{
if(board[i][0]=='O')
bfs(board,i,0,m,n);
if(board[i][n-1]=='O')
bfs(board,i,n-1,m,n);
}
for(int i=0;i<n;i++)
{
if(board[0][i]=='O')
bfs(board,0,i,m,n);
if(board[m-1][i]=='O')
bfs(board,m-1,i,m,n);
}
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
if(board[i][j]=='O')
board[i][j]='X';
else if(board[i][j]=='n')
board[i][j]='O';
}
}
void bfs(vector<vector<char> >& board,int r,int c,int m,int n)
{
queue<pair<int,int> > q;
board[r][c]='n';
q.push(make_pair(r,c));
while(!q.empty())
{
r=q.front().first;
c=q.front().second;
q.pop();
if(r>0&&board[r-1][c]=='O')
{
board[r-1][c]='n';
q.push(make_pair(r-1,c));
}
if(r<m-1&&board[r+1][c]=='O')
{
board[r+1][c]='n';
q.push(make_pair(r+1,c));
}
if(c>0&&board[r][c-1]=='O')
{
board[r][c-1]='n';
q.push(make_pair(r,c-1));
}
if(c<n-1&&board[r][c+1]=='O')
{
board[r][c+1]='n';
q.push(make_pair(r,c+1));
}
}
}
};
BFS here is to change the character before push into the queue, or it will push some positions that are already in the queue and finally cause TLE.
class Solution {
public:
void solve(vector<vector<char>>& board) {
if(board.empty())
return;
int m=board.size();
int n=board[0].size();
for(int i=0;i<m;i++)
{
if(board[i][0]=='O')
bfs(board,i,0,m,n);
if(board[i][n-1]=='O')
bfs(board,i,n-1,m,n);
}
for(int i=0;i<n;i++)
{
if(board[0][i]=='O')
bfs(board,0,i,m,n);
if(board[m-1][i]=='O')
bfs(board,m-1,i,m,n);
}
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
if(board[i][j]=='O')
board[i][j]='X';
else if(board[i][j]=='n')
board[i][j]='O';
}
}
void bfs(vector<vector<char> >& board,int r,int c,int m,int n)
{
queue<pair<int,int> > q;
board[r][c]='n';
q.push(make_pair(r,c));
while(!q.empty())
{
r=q.front().first;
c=q.front().second;
q.pop();
if(r>0&&board[r-1][c]=='O')
{
board[r-1][c]='n';
q.push(make_pair(r-1,c));
}
if(r<m-1&&board[r+1][c]=='O')
{
board[r+1][c]='n';
q.push(make_pair(r+1,c));
}
if(c>0&&board[r][c-1]=='O')
{
board[r][c-1]='n';
q.push(make_pair(r,c-1));
}
if(c<n-1&&board[r][c+1]=='O')
{
board[r][c+1]='n';
q.push(make_pair(r,c+1));
}
}
}
};
相关文章推荐
- C语言中的dirent.h说明
- /sys/class/gpio 文件接口操作IO端口(s3c2440)
- TYVJ 1330 乳草的入侵
- CSS3属性box-flex
- 编写高质量代码改善C#程序的157个建议——建议55:利用定制特性减少可序列化的字段
- Oracle清理回收站的方法
- IO流笔记01
- R语言抓取pm2.5数据绘制全国pm2.5分布图
- Oracle清理回收站的方法
- ListView的工作原理及优化
- sublime text 3 快捷键大全以及配置编译环境
- Linux学习笔记:系统启动引导过程
- HDU_1176_免费馅饼
- QQ安装教程 2 -for wuqs
- QQ安装教程 2 -for wuqs
- 计算机基本常识
- python抓取网页图片
- USB with NXP Microcontrollers
- 【Java并发编程实战】-----“J.U.C”:ReentrantLock之一简介
- python抓取网页图片