-
Notifications
You must be signed in to change notification settings - Fork 3
/
road-search-bread.html
110 lines (97 loc) · 2.71 KB
/
road-search-bread.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.cell {
width: 6px;
height: 6px;
background-color: lightgreen;
border-bottom: solid 1px white;
border-right: solid 1px white;
vertical-align: middle;
}
#container {
display: flex;
flex-wrap: wrap;
width: 700px;
}
</style>
</head>
<body>
<div id="container"></div>
<button onclick="localStorage.map = JSON.stringify(map)">save</button>
</body>
<script>
// const map = new Array(100001).join(0).split('').map(item => Number(item))
const map = localStorage.map ? JSON.parse(localStorage.map) : new Array(10000).fill(0)
let container = document.getElementById('container')
for (let row = 0; row < 100; row++) {
for (let col = 0; col < 100; col++) {
let cell = document.createElement("div")
cell.classList.add('cell')
if (map[row * 100 + col] === 1) {
cell.style.backgroundColor = "black"
}
cell.addEventListener("mousemove", () => {
if (mouse) {
if (clear) {
cell.style.backgroundColor = ""
cell.classList.remove("light")
map[row * 100 + col] = 0
} else {
cell.style.backgroundColor = "black"
map[row * 100 + col] = 1
}
}
})
container.appendChild(cell)
}
}
let mouse = false
let clear = false
document.addEventListener("mousedown", (e) => {
mouse = true
clear = (e.which === 3) // 3 鼠标右键
})
document.addEventListener("mouseup", () => mouse = false)
document.addEventListener("contextmenu", e => e.preventDefault())
function sleep(t) {
return new Promise((resolve, reject) => {
setTimeout(resolve, t)
})
}
// DeepSearch
async function findPath(map, start, end) {
map = map.slice()
let stack = [start]
async function insert([col, row], pre) {
if (map[100 * row + col] !== 0) {
return
}
if (col < 0 || row < 0 || col >= 100 || row >= 100) {
return
}
map[100 * row + col] = pre
container.children[row * 100 + col].style.backgroundColor = "pink"
await sleep(5)
stack.push([col, row])
}
while (stack.length) {
console.log('stack============================', stack)
let [x, y] = stack.pop() // pop unshift / push shift
if (x === end[0] && y === end[1]) {
while(x !== start[0] || y !== start[1]) {
return true
}
}
await insert([x - 1, y])
await insert([x + 1, y])
await insert([x, y - 1])
await insert([x, y + 1])
}
}
</script>
</html>