1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
class Solution { public static int qx[]=new int[100000]; public static int qy[]=new int[100000]; public int check(int x,int y,int r,char[][]board){ if(x>0&&x<board.length&&y>0&&y<board[0].length){ if(board[x][y]=='O'){ board[x][y]='X'; qx[r]=x; qy[r]=y; r++; }else{ isHuan=true; } return r; } } public bfs(int x,int y,char[][]board){ int h=0; int r=1; boolean isHuan=false; board[x][y]='X'; while(h<r&&!isHuan){ r=check(qx[h]+1,qy[h],r,board); r=check(qx[h]-1,qy[h],r,board); r=check(qx[h],qy[h]+1,r,board); r=check(qx[h],qy[h]-1,r,board); h++; } if(isHuan){ for(int i=0;i<r;i++){ board[qx[i]][qy[i]]='O'; } } } public void solve(char[][] board) { if(board.length==0||board[0].length==0) return ; int m=board.length; int n=board[0].length; for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ if(board[i][j]=='O'){ bfs(i,j,board); } } } } }
|
近期评论