Surrounded Regions
来源:互联网 发布:mac不识别u盘启动 编辑:程序博客网 时间:2024/05/05 03:41
Problem:
Given a 2D board containing 'X'
and 'O'
, capture all regions surrounded by 'X'
.
A region is captured by flipping all 'O'
s into 'X'
s in that surrounded region.
For example,
X X X XX O O XX X O XX O X X
After running your function, the board should be:
X X X XX X X XX X X XX O X X
尝试了几种扫描整个平面的方法,都失败了。主要原因是二维扫描的顺序不能向四个方向发散,总会有遗漏的死角。尝试一种递归的方法,出现了StackOverFlow 的runtime error。看有评论说用栈模拟递归能解决这个问题。最后用了下面这个队列进行BFS的方法,终于AC了。
Solution:
public class Solution {
public void solve(char[][] board) {
if(board==null||board.length==0||board[0].length==0)
return ;
LinkedList<BoardPoint> pQueue = new LinkedList<>();
int len = board.length;
int wid = board[0].length;
for(int i=0;i<len;i++)
{
if(board[i][wid-1]=='O')
{
board[i][wid-1] = 'N';
if(wid>1&&board[i][wid-2]=='O')
{
board[i][wid-2] = 'N';
pQueue.add(new BoardPoint(i, wid-2));
}
}
if(board[i][0]=='O')
{
board[i][0] = 'N';
if(wid>1&&board[i][1]=='O')
{
board[i][1] = 'N';
pQueue.add(new BoardPoint(i, 1));
}
}
}
for(int i=0;i<wid;i++)
{
if(board[0][i]=='O')
{
board[0][i] = 'N';
if(len>1&&board[1][i]=='O')
{
board[1][i] = 'N';
pQueue.add(new BoardPoint(1, i));
}
}
if(board[len-1][i]=='O')
{
board[len-1][i] = 'N';
if(len>1&&board[len-2][i]=='O')
{
board[len-2][i] = 'N';
pQueue.add(new BoardPoint(len-2, i));
}
}
}
while(!pQueue.isEmpty())
{
BoardPoint p = pQueue.poll();
if(p.x<len-2&&board[p.x+1][p.y]=='O')
{
board[p.x+1][p.y] = 'N';
pQueue.addLast(new BoardPoint(p.x+1, p.y));
}
if(p.x>1&&board[p.x-1][p.y]=='O')
{
board[p.x-1][p.y] = 'N';
pQueue.addLast(new BoardPoint(p.x-1, p.y));
}
if(p.y<wid-2&&board[p.x][p.y+1]=='O')
{
board[p.x][p.y+1] = 'N';
pQueue.addLast(new BoardPoint(p.x, p.y+1));
}
if(p.y>1&&board[p.x][p.y-1]=='O')
{
board[p.x][p.y-1] = 'N';
pQueue.addLast(new BoardPoint(p.x, p.y-1));
}
}
for(int i=0;i<len;i++)
{
for(int j=0;j<wid;j++)
{
if(board[i][j]=='N')
board[i][j] = 'O';
else if(board[i][j]=='O')
board[i][j] = 'X';
}
}
}
}
class BoardPoint
{
int x,y;
public BoardPoint(int x,int y)
{
this.x = x;
this.y = y;
}
}
public void solve(char[][] board) {
if(board==null||board.length==0||board[0].length==0)
return ;
LinkedList<BoardPoint> pQueue = new LinkedList<>();
int len = board.length;
int wid = board[0].length;
for(int i=0;i<len;i++)
{
if(board[i][wid-1]=='O')
{
board[i][wid-1] = 'N';
if(wid>1&&board[i][wid-2]=='O')
{
board[i][wid-2] = 'N';
pQueue.add(new BoardPoint(i, wid-2));
}
}
if(board[i][0]=='O')
{
board[i][0] = 'N';
if(wid>1&&board[i][1]=='O')
{
board[i][1] = 'N';
pQueue.add(new BoardPoint(i, 1));
}
}
}
for(int i=0;i<wid;i++)
{
if(board[0][i]=='O')
{
board[0][i] = 'N';
if(len>1&&board[1][i]=='O')
{
board[1][i] = 'N';
pQueue.add(new BoardPoint(1, i));
}
}
if(board[len-1][i]=='O')
{
board[len-1][i] = 'N';
if(len>1&&board[len-2][i]=='O')
{
board[len-2][i] = 'N';
pQueue.add(new BoardPoint(len-2, i));
}
}
}
while(!pQueue.isEmpty())
{
BoardPoint p = pQueue.poll();
if(p.x<len-2&&board[p.x+1][p.y]=='O')
{
board[p.x+1][p.y] = 'N';
pQueue.addLast(new BoardPoint(p.x+1, p.y));
}
if(p.x>1&&board[p.x-1][p.y]=='O')
{
board[p.x-1][p.y] = 'N';
pQueue.addLast(new BoardPoint(p.x-1, p.y));
}
if(p.y<wid-2&&board[p.x][p.y+1]=='O')
{
board[p.x][p.y+1] = 'N';
pQueue.addLast(new BoardPoint(p.x, p.y+1));
}
if(p.y>1&&board[p.x][p.y-1]=='O')
{
board[p.x][p.y-1] = 'N';
pQueue.addLast(new BoardPoint(p.x, p.y-1));
}
}
for(int i=0;i<len;i++)
{
for(int j=0;j<wid;j++)
{
if(board[i][j]=='N')
board[i][j] = 'O';
else if(board[i][j]=='O')
board[i][j] = 'X';
}
}
}
}
class BoardPoint
{
int x,y;
public BoardPoint(int x,int y)
{
this.x = x;
this.y = y;
}
}
0 0
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Surrounded Regions
- Palindrome Partitioning
- 2014.10.02
- 数字图像之边缘检测(1)
- 检索地理位置(GeocodeService服务)
- Palindrome Partitioning II
- Surrounded Regions
- 【异或】SOJ Find Differences
- Sum Root to Leaf Numbers
- day38,page60,total390
- Longest Consecutive Sequence
- ubuntu 下eclipse 打不开
- Word Ladder
- 各种工业以太网比较(EtherCAT,EtherNet/IP,ProfiNet,Modbus-TCP,Powerlink)
- VMware上新建windows 10 虚拟机(1)