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 50 51
| var solve = function (board) { let listarr = []; let jude = function (board, arr, i, j) { if (i - 1 < 0 || i + 1 > board.length - 1 || j - 1 < 0 || j + 1 > board[i].length - 1) { return false; } if (board[i + 1][j] == "O" && arr[`${i+1},${j}`] == undefined) { arr[`${i+1},${j}`] = [i + 1, j]; if (jude(board, arr, i + 1, j) == false) { return false; } } if (board[i - 1][j] == "O" && arr[`${i-1},${j}`] == undefined) { arr[`${i-1},${j}`] = [i - 1, j];
if (jude(board, arr, i - 1, j) == false) { return false; } } if (board[i][j - 1] == "O" && arr[`${i},${j-1}`] == undefined) { arr[`${i},${j-1}`] = [i, j - 1]; if (jude(board, arr, i, j - 1) == false) { return false; } } if (board[i][j + 1] == "O" && arr[`${i},${j+1}`] == undefined) { arr[`${i},${j+1}`] = [i, j + 1]; if (jude(board, arr, i, j + 1) == false) { return false; } } return arr; } for (let i = 0; i < board.length; i++) { for (let j = 0; j < board[i].length; j++) { if (board[i][j] == "O") { let arr = {}; arr[i + "," + j] = [i, j]; let d = jude(board, arr, i, j); if (d != false) { console.log(d); for (let i in d) { let sp = i.split(","); board[sp[0]][sp[1]] = "X"; } } } } } return board; };
|