-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathff.ts
77 lines (67 loc) · 1.58 KB
/
ff.ts
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
type Point = readonly [x: number, y: number]
class Board {
static of(cells: string) {
return new Board(cells.split("\n").map((x) => x.split("")))
}
constructor(readonly cells: string[][]) {}
*search(endpoints: string, as: string) {
let a, b
find: for (let i = 0; i < this.cells.length; i++) {
for (let j = 0; j < this.cells[i]!.length; j++) {
if (this.cells[i]![j]! == endpoints) {
if (a) {
b = [i, j] satisfies Point as Point
break find
} else {
a = [i, j] satisfies Point as Point
}
}
}
}
if (!a || !b) {
console.warn("no endpoints")
return
}
yield* this.explore(a, b, as)
}
*explore([sx, sy]: Point, [dx, dy]: Point, as: string): Generator<string> {
for (const [ex, ey] of [
[0, 1],
[0, -1],
[1, 0],
[-1, 0],
] as Point[]) {
const x = sx + ex
const y = sy + ey
if (x == dx && y == dy) {
yield this.join()
} else if (this.cells[y]?.[x] == ".") {
this.cells[y]![x] = as
yield* this.explore([x, y], [dx, dy], as)
this.cells[y]![x] = "."
}
}
}
join() {
return this.cells.map((x) => x.join("")).join("\n")
}
}
const src = new Board(
`A....
.....
..B..
CBD.A
D...C`
.split("\n")
.map((x) => x.split("")),
)
for (const a of src.search("A", "a")) {
console.log()
console.log(a)
// for (const b of Board.of(a).search("B", "b")) {
// for (const c of Board.of(b).search("C", "c")) {
// for (const d of c.search("D", "d")) {
// }
// }
// }
}