2
* @param {number[][]} board7
constructor(board, currStep) {8
this.board = this.copyBoard(board);9
this.boardString = this.flatToString(board);10
this.emptyIndex = this.findIndexOf0(board);11
this.currStep = currStep;14
for (let i = 0; i < board.length; i++) {15
for (let j = 0; j < board[i].length; j++) {16
if (board[i][j] === 0) return [i, j];23
board.forEach((row) => {24
newBoard.push([...row]);30
for (let i = 0; i < board.length; i++) {31
for (let j = 0; j < board[i].length; j++) {38
var slidingPuzzle = function (board) {39
let queue = [new BoardState(board, 0)];41
const x = board.length,49
let slide = (i, j, newi, newj, currBoardState) => {50
if (newi < 0 || newj < 0 || newi >= x || newj >= y) {53
const newBoard = currBoardState.copyBoard(currBoardState.board);54
const temp = newBoard[i][j];55
newBoard[i][j] = newBoard[newi][newj];56
newBoard[newi][newj] = temp;57
const newBoardState = new BoardState(newBoard, currBoardState.currStep + 1);60
while (queue.length > 0) {61
const currBoardState = queue.shift();62
set.add(currBoardState.boardString);63
if (currBoardState.boardString === "123450") {64
return currBoardState.currStep;66
const [i, j] = currBoardState.emptyIndex;67
switchMoves.forEach((move) => {68
const newBoardState = slide(75
if (newBoardState && !set.has(newBoardState.boardString)) {76
queue.push(newBoardState);