1 public class Solution { 2 public void solve(char[][] board) { 3 // IMPORTANT: Please reset any member data you declared, as 4 // the same Solution instance will be reused for each test case. 5 if(board == null||board.length == 0||board[0] == null||board[0].length == 0) 6 return; 7 int y = board.length; 8 int x = board[0].length; 9 LinkedList<location> mylist = new LinkedList<location>(); 10 for(int i = 0; i < x; i++) 11 { 12 if(board[0][i] == 'O') 13 { 14 board[0][i] = 'Y'; 15 mylist.add(new location(0, i)); 16 } 17 if(board[y-1][i] == 'O') 18 { 19 board[y-1][i] = 'Y'; 20 mylist.add(new location(y-1, i)); 21 } 22 } 23 for(int i = 0; i < y; i++) 24 { 25 if(board[i][0] == 'O') 26 { 27 board[i][0] = 'Y'; 28 mylist.add(new location(i, 0)); 29 } 30 if(board[i][x-1] == 'O') 31 { 32 board[i][x-1] = 'Y'; 33 mylist.add(new location(i, x-1)); 34 } 35 } 36 while(!mylist.isEmpty()) 37 { 38 location myloc = mylist.poll(); 39 if(myloc.x - 1 >= 0 && board[myloc.x - 1][myloc.y] == 'O') 40 { 41 board[myloc.x - 1][myloc.y] = 'Y'; 42 mylist.add(new location(myloc.x - 1, myloc.y)); 43 } 44 if(myloc.y - 1 >= 0 && board[myloc.x][myloc.y - 1] == 'O') 45 { 46 board[myloc.x][myloc.y - 1] = 'Y'; 47 mylist.add(new location(myloc.x, myloc.y - 1)); 48 } 49 if(myloc.x + 1 < y && board[myloc.x + 1][myloc.y] == 'O') 50 { 51 board[myloc.x + 1][myloc.y] = 'Y'; 52 mylist.add(new location(myloc.x + 1, myloc.y)); 53 } 54 if(myloc.y + 1 < x && board[myloc.x][myloc.y + 1] == 'O') 55 { 56 board[myloc.x][myloc.y + 1] = 'Y'; 57 mylist.add(new location(myloc.x, myloc.y + 1)); 58 } 59 } 60 for(int i = 0; i < y; i++) 61 for(int j = 0; j < x; j++) 62 { 63 if(board[i][j] == 'Y') 64 board[i][j] = 'O'; 65 else if(board[i][j] == 'O') 66 board[i][j] = 'X'; 67 } 68 } 69 class location{ 70 int x; 71 int y; 72 location(int x, int y) 73 {this.x = x; this.y = y;} 74 } 75 }