-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathSliding Puzzle.js
70 lines (69 loc) · 2.21 KB
/
Sliding Puzzle.js
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Runtime: 172 ms (Top 37.35%) | Memory: 51.6 MB (Top 7.23%)
/**
* @param {number[][]} board
* @return {number}
*/
class BoardState {
constructor(board, currStep) {
this.board = this.copyBoard(board);
this.boardString = this.flatToString(board);
this.emptyIndex = this.findIndexOf0(board);
this.currStep = currStep;
}
findIndexOf0(board) {
for(let i = 0; i < board.length; i++) {
for (let j = 0; j < board[i].length; j++) {
if(board[i][j] === 0) return [i, j];
}
}
return null;
}
copyBoard(board) {
const newBoard = [];
board.forEach((row) => {
newBoard.push([...row]);
});
return newBoard;
}
flatToString(board) {
let str = '';
for(let i = 0; i < board.length; i++) {
for (let j = 0; j < board[i].length; j++) {
str += board[i][j];
}
}
return str;
}
}
var slidingPuzzle = function(board) {
let queue = [new BoardState(board, 0)];
let set = new Set();
const x = board.length, y = board[0].length;
const switchMoves = [[1, 0], [0, 1], [-1, 0], [0, -1]];
let slide = (i, j, newi, newj, currBoardState) => {
if (newi < 0 || newj < 0 || newi >= x || newj >= y) {
return null;
}
const newBoard = currBoardState.copyBoard(currBoardState.board);
const temp = newBoard[i][j];
newBoard[i][j] = newBoard[newi][newj];
newBoard[newi][newj] = temp;
const newBoardState = new BoardState(newBoard, currBoardState.currStep + 1);
return newBoardState
}
while(queue.length > 0) {
const currBoardState = queue.shift();
set.add(currBoardState.boardString);
if(currBoardState.boardString === '123450') {
return currBoardState.currStep;
}
const [i, j] = currBoardState.emptyIndex;
switchMoves.forEach((move) => {
const newBoardState = slide(i, j, i + move[0], j + move[1], currBoardState);
if (newBoardState && !set.has(newBoardState.boardString)) {
queue.push(newBoardState);
}
});
}
return -1;
};