Created
August 14, 2021 00:26
-
-
Save germanescobar/f6f61ef513cda9b038e3c081e79fc299 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var solve = function(board) { | |
for (let row=0; row < board.length; row++) { | |
// columna izquierda | |
if (board[row][0] === "O") markCells(board, row, 0) | |
// columna derecha | |
const col = board[row].length-1 | |
if (board[row][col] === "O") markCells(board, row, col) | |
} | |
for (let col=0; col < board[0].length; col++) { | |
// fila arriba | |
if (board[0][col] === "O") markCells(board, 0, col) | |
// fila abajo | |
const row = board.length-1 | |
if (board[row][col] === "O") markCells(board, row, col) | |
} | |
for (let i=0; i < board.length; i++) { | |
for (let j=0; j < board[i].length; j++) { | |
board[i][j] = board[i][j] === "m" ? "O" : "X" | |
} | |
} | |
}; | |
function markCells(board, i, j) { | |
board[i][j] = "m" | |
if (i > 0 && board[i-1][j] === "O") { | |
markCells(board, i-1, j) | |
} | |
if (j < board[0].length-1 && board[i][j+1] === "O") { | |
markCells(board, i, j+1) | |
} | |
if (i < board.length-1 && board[i+1][j] === "O") { | |
markCells(board, i+1, j) | |
} | |
if (j > 0 && board[i][j-1] === "O") { | |
markCells(board, i, j-1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment