-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
199 lines (174 loc) · 4.86 KB
/
scripts.js
File metadata and controls
199 lines (174 loc) · 4.86 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const SIZE = 7;
const dx = [0, 0, 1, -1];
const dy = [1, -1, 0, 0];
let isRunning = false;
let sx, sy, ex, ey, tx, ty, MAX_STEPS, speedCoeff;
function getValues() {
sx = parseInt(document.querySelector('#sx').value) - 1;
sy = parseInt(document.querySelector('#sy').value) - 1;
ex = parseInt(document.querySelector('#ex').value) - 1;
ey = parseInt(document.querySelector('#ey').value) - 1;
tx = parseInt(document.querySelector('#tx').value) - 1;
ty = parseInt(document.querySelector('#ty').value) - 1;
MAX_STEPS = parseInt(document.querySelector('#steps').value);
speedCoeff = parseFloat(document.querySelector('#speed').value);
}
getValues();
/*
let sx = 0, sy = 3;
let ex = 2, ey = 0;
let tx = 1, ty = 1;
let MAX_STEPS = 5;
*/
let dp = [];
let step = 0;
let timer = null;
// Initialize DP array
function initDP() {
dp = Array.from({ length: MAX_STEPS + 1 }, () =>
Array.from({ length: SIZE }, () =>
Array(SIZE).fill(0)
)
);
dp[0][sx][sy] = 1;
}
// Create grid
function buildGrid() {
const grid = document.getElementById("grid");
grid.innerHTML = "";
for (let x = 0; x < SIZE; x++) {
for (let y = 0; y < SIZE; y++) {
const div = document.createElement("div");
div.className = "cell";
div.id = `c-${x}-${y}`;
grid.appendChild(div);
}
}
}
// Render current step
function render() {
document.getElementById("stepLabel").textContent = `Step ${step}`;
let maxVal = 0;
for (let x = 0; x < SIZE; x++)
for (let y = 0; y < SIZE; y++)
maxVal = Math.max(maxVal, dp[step][x][y]);
for (let x = 0; x < SIZE; x++) {
for (let y = 0; y < SIZE; y++) {
const cell = document.getElementById(`c-${x}-${y}`);
const val = dp[step][x][y];
cell.className = "cell";
cell.textContent = val ? val : "";
if (val > 0) {
const intensity = Math.min(val / (maxVal || 1), 1);
cell.style.background = `rgba(30, 144, 255, ${0.2 + intensity * 0.8})`;
} else {
cell.style.background = "white";
}
if (x === sx && y === sy) cell.classList.add("start");
if (x === ex && y === ey) cell.classList.add("end");
if (x === tx && y === ty) cell.classList.add("must");
}
}
}
// Compute next DP step
function nextStep() {
if (step >= MAX_STEPS) return;
for (let x = 0; x < SIZE; x++) {
for (let y = 0; y < SIZE; y++) {
if (dp[step][x][y] === 0) continue;
for (let i = 0; i < 4; i++) {
const nx = x + dx[i];
const ny = y + dy[i];
if (nx >= 0 && nx < SIZE && ny >= 0 && ny < SIZE) {
dp[step + 1][nx][ny] += dp[step][x][y];
}
}
}
}
step++;
render();
}
function nextStepAvoiding() {
if (step >= MAX_STEPS) return;
if ((sx == tx && sy == ty) || (ex == tx && ey == ty)) return 0;
for (let x = 0; x < SIZE; x++) {
for (let y = 0; y < SIZE; y++) {
if (dp[step][x][y] === 0) continue;
for (let i = 0; i < 4; i++) {
const nx = x + dx[i];
const ny = y + dy[i];
if (nx >= 0 && nx < SIZE && ny >= 0 && ny < SIZE && !(nx == tx && ny == ty) && ny >= 0 && ny < SIZE) {
dp[step + 1][nx][ny] += dp[step][x][y];
}
}
}
}
step++;
render();
}
// Controls
async function start() {
if (isRunning) return;
isRunning = true;
const resultDiv = document.getElementById("result");
const avoidingDiv = document.getElementById("avoiding");
const totalDiv = document.getElementById("total");
totalDiv.textContent = `Total: 0`
avoidingDiv.textContent = `Avoiding: 0`;
resultDiv.textContent = `Result: 0`;
console.log("== sequentialStart starts ==");
getValues();
initDP();
render();
const total = nextStepAsync();
totalDiv.textContent = `Total: ${await total}`;
const avoiding = nextStepAvoidingAsync();
avoidingDiv.textContent = `Avoiding: ${await avoiding}`;
reset();
console.log("== sequentialStart done ==");
const result = await total - await avoiding;
resultDiv.textContent = `Result: ${result}`;
isRunning = false;
}
function nextStepAsync() {
console.log("starting nextStepAsync()");
return new Promise((resolve) => {
timer = setInterval(() => {
if (step >= MAX_STEPS) {
const total = dp[step][ex][ey];
resolve(total);
console.log("nextStepAsync() finished")
reset();
}
nextStep();
}, 800 / speedCoeff);
});
}
function nextStepAvoidingAsync() {
console.log("starting nextStepAvoidingAsync()");
return new Promise((resolve) => {
timer = setInterval(() => {
if (step >= MAX_STEPS) {
const avoiding = dp[step][ex][ey];
resolve(avoiding);
console.log("nextStepAvoidingAsync() finished")
reset();
}
nextStepAvoiding();
}, 800 / speedCoeff);
});
}
function pause() {
clearInterval(timer);
timer = null;
}
function reset() {
pause();
step = 0;
initDP();
render();
}
// Init
buildGrid();
initDP();
render();