-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknightstravails.js
92 lines (79 loc) · 2.04 KB
/
knightstravails.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class Queue {
constructor() {
this.queue = [];
}
enqueue(element) {
this.queue.push(element);
}
dequeue() {
if (this.queue.length > 0) return this.queue.shift();
return undefined;
}
isEmpty() {
return this.queue.length == 0;
}
clear() {
this.queue = [];
}
show() {
console.log(this.queue);
}
}
const rq = new Queue();
const cq = new Queue();
let nodes_in_next_layer = 0;
let nodes_left_in_layer = 1;
function firstsearch(start, visited, parent) {
let [r, c] = start;
let dr = [-2, -2, -1, -1, 1, 1, 2, 2];
let dc = [-1, 1, -2, 2, -2, 2, -1, 1];
for (let i = 0; i < 8; i++) {
let rr = r + dr[i];
let cc = c + dc[i];
if (rr < 0 || cc < 0 || rr >= 8 || cc >= 8) continue;
if (visited[rr][cc]) continue;
rq.enqueue(rr);
cq.enqueue(cc);
visited[rr][cc] = true;
parent[rr][cc] = `(${r}, ${c})`;
nodes_in_next_layer++;
}
}
function traverseThrough(start, finish, parent) {
const [sr, sc] = start;
let [fr, fc] = finish;
let myArray = [[fr, fc]];
while (parent[fr][fc] !== `(${sr}, ${sc})`) {
[fr, fc] = parent[fr][fc].replace(/[()]/g, "").split(", ").map(Number);
myArray.push([fr, fc]);
}
myArray.push([sr, sc]);
return myArray.reverse();
}
function extensiveSearch(start, finish) {
const visited = Array.from({ length: 8 }, () => Array(8).fill(false));
const parent = Array.from({ length: 8 }, () => Array(8).fill(null));
let reachedend = false;
const [sr, sc] = start;
const [fr, fc] = finish;
rq.enqueue(sr);
cq.enqueue(sc);
visited[sr][sc] = true;
while (!rq.isEmpty()) {
const r = rq.dequeue();
const c = cq.dequeue();
if (r == fr && c == fc) {
reachedend = true;
break;
}
firstsearch([r, c], visited, parent);
nodes_left_in_layer--;
if (nodes_left_in_layer == 0) {
nodes_left_in_layer = nodes_in_next_layer;
nodes_in_next_layer = 0;
}
}
let myarray = traverseThrough(start, finish, parent);
return reachedend ? myarray : -1;
}
console.log(extensiveSearch([6, 7], [7, 7]));