|
| 1 | +/** |
| 2 | + * res.js |
| 3 | + * @authors Joe Jiang (hijiangtao@gmail.com) |
| 4 | + * @date 2017-03-10 22:17:27 |
| 5 | + * @version $Id$ |
| 6 | + * |
| 7 | + * @param {character[][]} board |
| 8 | + * @return {void} Do not return anything, modify board in-place instead. |
| 9 | + */ |
| 10 | + |
| 11 | +let solve = function(board) { |
| 12 | + if (board.length === 0) { |
| 13 | + return ; |
| 14 | + } |
| 15 | + |
| 16 | + let rowlen = board.length, |
| 17 | + collen = board[0].length, |
| 18 | + rowecp = rowlen-1, |
| 19 | + colecp = collen-1; |
| 20 | + |
| 21 | + if (rowlen <= 2 || collen <= 2) { |
| 22 | + return ; |
| 23 | + } |
| 24 | + |
| 25 | + for (let i=0; i<collen; i++) { |
| 26 | + // 判断第一行是否存在'O' |
| 27 | + if (board[0][i] === 'O') { |
| 28 | + board[0][i] = '1'; |
| 29 | + if (board[1][i] === 'O' && i!==0 && i!==collen-1) { |
| 30 | + judZero(1, i, rowlen, collen, board); |
| 31 | + } |
| 32 | + } |
| 33 | + // judZero(0, i, rowlen, collen, board); |
| 34 | + |
| 35 | + // 判断最后一行是否存在'O' |
| 36 | + if (board[rowecp][i] === 'O') { |
| 37 | + board[rowecp][i] = '1'; |
| 38 | + if (board[rowecp-1][i] === 'O' && i!==0 && i!==collen-1) { |
| 39 | + judZero(rowecp-1, i, rowlen, collen, board); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // judZero(rowecp, i, rowlen, collen, board); |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + for (let i=1; i<rowecp; i++) { |
| 48 | + // 判断第一列是否存在'O' |
| 49 | + if (board[i][0] === 'O') { |
| 50 | + board[i][0] = '1'; |
| 51 | + if (board[i][1] === 'O' && i!==0 && i!==rowecp) { |
| 52 | + judZero(i, 1, rowlen, collen, board); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // judZero(i, 0, rowlen, collen, board); |
| 57 | + |
| 58 | + // 判断最后一列是否存在'O' |
| 59 | + if (board[i][colecp] === 'O') { |
| 60 | + board[i][colecp] = '1'; |
| 61 | + if (board[i][colecp-1] === 'O' && i!==0 && i!==rowecp) { |
| 62 | + judZero(i, colecp-1, rowlen, collen, board); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // judZero(i, colecp, rowlen, collen, board); |
| 67 | + } |
| 68 | + |
| 69 | + for (let i=0; i<rowlen; i++) { |
| 70 | + for (let j=0; j<collen; j++) { |
| 71 | + if (board[i][j] === '1') { |
| 72 | + board[i][j] = 'O'; |
| 73 | + } else if (board[i][j] === 'O') { |
| 74 | + board[i][j] = 'X'; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return ; |
| 80 | +}; |
| 81 | + |
| 82 | +let judZero = function(x, y, rowlen, collen, matrix) { |
| 83 | + // if (matrix[x][y] !== 'O') { |
| 84 | + // return ; |
| 85 | + // } |
| 86 | + |
| 87 | + matrix[x][y] = '1'; |
| 88 | + |
| 89 | + if (x-1 > 1 && matrix[x-1][y] === 'O') { |
| 90 | + judZero(x-1, y, rowlen, collen, matrix); |
| 91 | + } |
| 92 | + if (x+1 < rowlen-2 && matrix[x+1][y] === 'O') { |
| 93 | + judZero(x+1, y, rowlen, collen, matrix); |
| 94 | + } |
| 95 | + if (y-1 > 1 && matrix[x][y-1] === 'O') { |
| 96 | + judZero(x, y-1, rowlen, collen, matrix); |
| 97 | + } |
| 98 | + if (y+1 < collen-2 && matrix[x][y+1] === 'O') { |
| 99 | + judZero(x, y+1, rowlen, collen, matrix); |
| 100 | + } |
| 101 | +}; |
| 102 | + |
| 103 | + |
0 commit comments