|
25 | 25 | */ |
26 | 26 | public class _130 { |
27 | 27 |
|
28 | | - /**I won't call this problem hard, it's just confusing, you'll definitely want to clarify what the problem means before coding. |
| 28 | + /** |
| 29 | + * I won't call this problem hard, it's just confusing, you'll definitely want to clarify what the problem means before coding. |
29 | 30 | * This problem eactually means: |
30 | 31 | * any grid that is 'O' but on the four edges, will never be marked to 'X'; |
31 | 32 | * furthermore, any grid that is 'O' and that is connected with the above type of 'O' will never be marked to 'X' as well; |
32 | | - * only all other nodes that has any one direct neighbor that is an 'X' will be marked to 'X'.*/ |
| 33 | + * only all other nodes that has any one direct neighbor that is an 'X' will be marked to 'X'. |
| 34 | + */ |
33 | 35 |
|
34 | 36 |
|
35 | | - int[] dirs = new int[]{0,1,0,-1,0}; |
| 37 | + int[] dirs = new int[]{0, 1, 0, -1, 0}; |
36 | 38 |
|
37 | 39 | public void solve(char[][] board) { |
38 | | - if(board == null || board.length == 0 || board[0].length == 0) return; |
| 40 | + if (board == null || board.length == 0 || board[0].length == 0) return; |
39 | 41 | int m = board.length, n = board[0].length; |
40 | 42 | Queue<int[]> queue = new LinkedList(); |
41 | 43 | //check first row and last row and mark all those '0' on these two rows to be '+' to let them be different from other 'O', |
42 | 44 | //at the same time, we put them into the queue to get ready for a BFS to mark all those adjacent 'O' nodes to '+' as well |
43 | | - for(int j = 0; j < n; j++){ |
44 | | - if(board[0][j] == 'O') { |
| 45 | + for (int j = 0; j < n; j++) { |
| 46 | + if (board[0][j] == 'O') { |
45 | 47 | board[0][j] = '+'; |
46 | | - queue.offer(new int[]{0,j}); |
| 48 | + queue.offer(new int[]{0, j}); |
47 | 49 |
|
48 | 50 | } |
49 | | - if(board[m-1][j] == 'O') { |
50 | | - board[m-1][j] = '+'; |
51 | | - queue.offer(new int[]{m-1,j}); |
| 51 | + if (board[m - 1][j] == 'O') { |
| 52 | + board[m - 1][j] = '+'; |
| 53 | + queue.offer(new int[]{m - 1, j}); |
52 | 54 | } |
53 | 55 | } |
54 | 56 |
|
55 | 57 | //check first column and last column too |
56 | | - for(int i = 0; i < m; i++){ |
57 | | - if(board[i][0] == 'O') { |
| 58 | + for (int i = 0; i < m; i++) { |
| 59 | + if (board[i][0] == 'O') { |
58 | 60 | board[i][0] = '+'; |
59 | 61 | queue.offer(new int[]{i, 0}); |
60 | 62 | } |
61 | | - if(board[i][n-1] == 'O') { |
62 | | - board[i][n-1] = '+'; |
63 | | - queue.offer(new int[]{i,n-1}); |
| 63 | + if (board[i][n - 1] == 'O') { |
| 64 | + board[i][n - 1] = '+'; |
| 65 | + queue.offer(new int[]{i, n - 1}); |
64 | 66 | } |
65 | 67 | } |
66 | 68 |
|
67 | | - while(!queue.isEmpty()){ |
| 69 | + while (!queue.isEmpty()) { |
68 | 70 | int[] curr = queue.poll(); |
69 | | - for(int i = 0; i < 4; i++){ |
70 | | - int x = curr[0]+dirs[i]; |
71 | | - int y = curr[1]+dirs[i+1]; |
72 | | - if(x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'O'){ |
| 71 | + for (int i = 0; i < 4; i++) { |
| 72 | + int x = curr[0] + dirs[i]; |
| 73 | + int y = curr[1] + dirs[i + 1]; |
| 74 | + if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'O') { |
73 | 75 | board[x][y] = '+'; |
74 | | - queue.offer(new int[]{x,y}); |
| 76 | + queue.offer(new int[]{x, y}); |
75 | 77 | } |
76 | 78 | } |
77 | 79 | } |
78 | 80 |
|
79 | 81 | //now we can safely mark all other 'O' to 'X', also remember to put those '+' back to 'O' |
80 | | - for(int i = 0; i < m; i++){ |
81 | | - for(int j = 0; j < n; j++){ |
82 | | - if(board[i][j] == 'O') board[i][j] = 'X'; |
83 | | - else if(board[i][j] == '+') board[i][j] = 'O'; |
| 82 | + for (int i = 0; i < m; i++) { |
| 83 | + for (int j = 0; j < n; j++) { |
| 84 | + if (board[i][j] == 'O') board[i][j] = 'X'; |
| 85 | + else if (board[i][j] == '+') board[i][j] = 'O'; |
84 | 86 | } |
85 | 87 | } |
86 | 88 | } |
|
0 commit comments