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 X X O O X X X O X X O X X
After running your function, the board should be:
DFS堆栈溢出public class solution2 { int[][] visited=null; char[][] myboard=null; char[][] res=null; public void solve(char[][] board) { int rows; int cols; if(board.length==0) return; rows=board.length; cols=board[0].length; visited = new int[rows+2][cols+2]; myboard = new char[rows+2][cols+2]; res = new char[rows][cols]; //board全部设置为没有访问,数值为0 for(int i=0;i<rows+2;i++){ for(int j=0;j<cols+2;j++){ visited[i][j]=0; } } //把board数据复制到新建的数组中 for(int i=1;i<=rows;i++){ for(int j=1;j<=cols;j++){ myboard[i][j]=board[i-1][j-1]; } } //为外层增加一圈XX for(int i=0;i<cols+2;i++){ myboard[0][i]='X'; myboard[rows+1][i]='X'; } for(int i=0;i<rows+2;i++){ myboard[i][0]='X'; myboard[i][cols+1]='X'; } //把要返回的结果数组先初始化为‘X',再dfs把一些没包含的’o‘找出来 for(int i=0;i<rows;i++){ for(int j=0;j<cols;j++){ res[i][j]='X'; } } for(int i=1;i<rows+1;i++){ if(visited[i][1]!=1&&myboard[i][1]!='X') dfs(i,1); if(visited[i][cols]!=1&&myboard[i][cols]!='X') dfs(i,cols); } for(int i=1;i<cols+1;i++){ if(visited[1][i]!=1&&myboard[1][i]!='X') dfs(1,i); if(visited[rows][i]!=1&&myboard[rows][i]!='X') dfs(rows,i); } for(int i=0;i<rows;i++){ for(int j=0;j<cols;j++){ board[i][j]=res[i][j]; } } } private void dfs(int i, int j) { // TODO Auto-generated method stub visited[i][j]=1; res[i-1][j-1]='O'; if(visited[i][j+1]!=1&&myboard[i][j+1]=='O') dfs(i, j+1); if(visited[i][j-1]!=1&&myboard[i][j-1]=='O') dfs(i, j-1); if(visited[i+1][j]!=1&&myboard[i+1][j]=='O') dfs(i+1, j); if(visited[i-1][j]!=1&&myboard[i-1][j]=='O') dfs(i-1, j); } }
import java.util.LinkedList; import java.util.Queue; /** * 从边界一圈遍历,与边界的O联通的不能改变为X,记录与边界联通的为C * 其他包围的O改变为X * 用队列实现bfs,队列存的是位置 * @author Administrator * */ public class Solution{ private int rows; private int cols; private char[][] myboard=null; private int[][] visited=null; Queue<Integer> queue=null; public void solve(char[][] board) { if(board==null||board.length==0) return ; rows = board.length; cols = board[0].length; visited=new int[rows][cols]; myboard=new char[rows][cols]; queue=new LinkedList<Integer>(); for(int i=0;i<rows;i++){ for(int j=0;j<cols;j++){ visited[i][j]=0; myboard[i][j]=board[i][j]; } } for(int i=0;i<cols;i++){ if(visited[0][i]==0&&myboard[0][i]=='O') bfs(0,i);//第一行· if(visited[rows-1][i]==0&&myboard[rows-1][i]=='O') bfs(rows-1, i);//最后一行 } for(int i=0;i<rows;i++){ if(visited[i][0]==0&&myboard[i][0]=='O') bfs(i, 0);//第一列 if(visited[i][cols-1]==0&&myboard[i][cols-1]=='O') bfs(i, cols-1); } for(int i=0;i<rows;i++){ for(int j=0;j<cols;j++){ if(myboard[i][j]=='C') board[i][j]='O'; else board[i][j]='X'; } } } private void bfs(int i, int i2) { // TODO Auto-generated method stub visited[i][i2]=1; myboard[i][i2]='C'; queue.add(i*cols+i2); int r; int c; while(queue.size()>0){ r=queue.peek()/cols; c=queue.poll()%cols; //&&具有短路功能,c+1<cols等应该放在最前面 if(c+1<cols&&visited[r][c+1]==0&&myboard[r][c+1]=='O') {queue.add(r*cols+c+1);visited[r][c+1]=1;myboard[r][c+1]='C';} if(c-1>=0&&visited[r][c-1]==0&&myboard[r][c-1]=='O') {queue.add(r*cols+c-1);visited[r][c-1]=1;myboard[r][c-1]='C';} if(r+1<rows&&visited[r+1][c]==0&&myboard[r+1][c]=='O') {queue.add((r+1)*cols+c);visited[r+1][c]=1;myboard[r+1][c]='C';} if(r-1>=0&&visited[r-1][c]==0&&myboard[r-1][c]=='O') {queue.add((r-1)*cols+c);visited[r-1][c]=1;myboard[r-1][c]='C';} } } }