-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfs.js
128 lines (112 loc) · 3.41 KB
/
bfs.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
window.addEventListener('load', () => {
let box = document.querySelector('.box');
box.addEventListener('click', startBFS);
document.querySelector('#generateGridButton').addEventListener('click', newGrid)
generateGrid()
});
let row = 5
let col = 5
function checkoverflow(box) {
let width = box.offsetWidth
let height = box.offsetHeight
console.log(height, screen.height * .80);
console.log(width, screen.width * .80);
if (height > screen.height * 0.80) {
box.style.overflowY = 'scroll'
box.style.height = `${screen.height * .80}px`
}
if (width > screen.width * 0.80) {
box.style.overflowX = 'scroll'
box.style.width = `${screen.width * .80}px`
}
}
function generateGrid() {
document.querySelector('.box').style.gridTemplateColumns = `repeat(${col},1fr)`
let counter = row * col;
let box = document.querySelector('.box');
box.innerHTML = ''
for (let i = 0; i < counter; i++) {
let div = document.createElement('div');
div.id = `box${i}`;
div.innerHTML = i;
div.className = 'childBox';
box.appendChild(div);
}
checkoverflow(box)
}
function newGrid() {
let grow = document.querySelector('#rows').value
let gcolumn = document.querySelector('#columns').value
if (!grow || !gcolumn) {
console.log("Not row or not column");
return
}
console.log("check1");
grow = Math.floor(parseInt(grow))
gcolumn = Math.floor(parseInt(gcolumn))
if (gcolumn <= 0 || grow <= 0) {
console.log("Less than 0",grow,gcolumn);
return
}
row = grow
col = gcolumn
generateGrid()
}
function emptyArray(r, c) {
let visited = new Array(r)
for (let i = 0; i < r; i++) {
visited[i] = new Array(c);
visited[i].fill(0);
}
return visited
}
function boxSelected(num) {
let selectedBox = document.querySelector(`#box${num}`)
selectedBox.classList.remove('inQueue')
selectedBox.classList.add('selected')
}
function boxInQueue(num) {
let selectedBox = document.querySelector(`#box${num}`)
selectedBox.classList.add('inQueue')
}
function boxVisited(num) {
let selectedBox = document.querySelector(`#box${num}`)
selectedBox.classList.remove('selected')
selectedBox.classList.add('visited')
}
function boxReset() {
generateGrid()
}
function sleep(milliseconds) {
return new Promise(resolve => setTimeout(resolve, milliseconds))
}
async function startBFS(event) {
const start = event.target.innerText
if(isNaN(start)) return
const time = 500;
const moves = [{ i: 0, j: 1 }, { i: 0, j: -1 }, { i: 1, j: 0 }, { i: -1, j: 0 }]
let visited = emptyArray(row, col);
let startI = Math.floor(start / col);
let startJ = Math.floor(start % col);
visited[startI][startJ] = 1
let queue = [];
queue.push({ i: startI, j: startJ });
while (queue.length>0) {
let { i, j } = queue.shift();
let num = i * col + j;
boxSelected(num)
await sleep(time)
moves.forEach(move => {
const ni = i + move.i;
const nj = j + move.j
if (ni >= 0 && ni < row && nj >= 0 && nj < col && !visited[ni][nj]) {
queue.push({ i: ni, j: nj})
boxInQueue(ni * col + nj)
visited[ni][nj] = 1
}
})
boxVisited(num)
await sleep(time)
}
boxReset()
}