This commit is contained in:
parent
c3c73ee517
commit
f965eea33a
@ -1,7 +1,7 @@
|
|||||||
|
import heapq
|
||||||
|
from collections import defaultdict
|
||||||
from typing import Any, Iterator, TypeAlias
|
from typing import Any, Iterator, TypeAlias
|
||||||
|
|
||||||
import networkx as nx
|
|
||||||
|
|
||||||
from ..base import BaseSolver
|
from ..base import BaseSolver
|
||||||
|
|
||||||
Position: TypeAlias = tuple[int, int]
|
Position: TypeAlias = tuple[int, int]
|
||||||
@ -18,41 +18,45 @@ class Solver(BaseSolver):
|
|||||||
target = next(
|
target = next(
|
||||||
(i, j) for i in range(n_rows) for j in range(n_cols) if grid[i][j] == "E"
|
(i, j) for i in range(n_rows) for j in range(n_cols) if grid[i][j] == "E"
|
||||||
)
|
)
|
||||||
facing = (0, 1)
|
|
||||||
|
|
||||||
G = nx.DiGraph()
|
queue: list[tuple[int, Position, Direction, tuple[Position, ...]]] = [
|
||||||
|
(0, rein, (0, 1), (rein,))
|
||||||
|
]
|
||||||
|
|
||||||
for i in range(1, n_rows - 1):
|
max_score = n_rows * n_cols * 1000
|
||||||
for j in range(1, n_cols - 1):
|
target_score: int = max_score
|
||||||
if grid[i][j] == "#":
|
scores: dict[tuple[Position, Direction], int] = defaultdict(lambda: max_score)
|
||||||
|
visited: set[Position] = set()
|
||||||
|
|
||||||
|
while queue:
|
||||||
|
score, pos, dir, path = heapq.heappop(queue)
|
||||||
|
|
||||||
|
if target_score < score:
|
||||||
|
break
|
||||||
|
|
||||||
|
if pos == target:
|
||||||
|
target_score = score
|
||||||
|
visited |= set(path)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
for di, dj in ((-1, 0), (0, 1), (1, 0), (0, -1)):
|
scores[pos, dir] = score
|
||||||
if grid[i + di][j + dj] != "#":
|
|
||||||
G.add_edge(
|
|
||||||
((i, j), (di, dj)), ((i + di, j + dj), (di, dj)), weight=1
|
|
||||||
)
|
|
||||||
|
|
||||||
if di == 0:
|
row, col = pos
|
||||||
G.add_edge(((i, j), (di, dj)), ((i, j), (1, 0)), weight=1000)
|
d_row, d_col = dir
|
||||||
G.add_edge(((i, j), (di, dj)), ((i, j), (-1, 0)), weight=1000)
|
|
||||||
else:
|
|
||||||
G.add_edge(((i, j), (di, dj)), ((i, j), (0, 1)), weight=1000)
|
|
||||||
G.add_edge(((i, j), (di, dj)), ((i, j), (0, -1)), weight=1000)
|
|
||||||
|
|
||||||
for di, dj in ((-1, 0), (0, 1), (1, 0), (0, -1)):
|
for cost, n_pos, n_dir in (
|
||||||
G.add_edge((target, (di, dj)), (target, (-1, -1)), weight=0)
|
(1, (row + d_row, col + d_col), dir),
|
||||||
|
(1000, pos, (1, 0) if d_row == 0 else (0, 1)),
|
||||||
yield nx.shortest_path_length(
|
(1000, pos, (-1, 0) if d_row == 0 else (0, -1)),
|
||||||
G,
|
|
||||||
source=(rein, facing),
|
|
||||||
target=(target, (-1, -1)),
|
|
||||||
weight="weight",
|
|
||||||
)
|
|
||||||
|
|
||||||
nodes: set[tuple[int, int]] = set()
|
|
||||||
for path in nx.all_shortest_paths(
|
|
||||||
G, source=(rein, facing), target=(target, (-1, -1)), weight="weight"
|
|
||||||
):
|
):
|
||||||
nodes.update(position for position, _ in path)
|
n_row, n_col = n_pos
|
||||||
yield len(nodes)
|
score_n = score + cost
|
||||||
|
if grid[n_row][n_col] != "#" and score_n < scores[n_pos, n_dir]:
|
||||||
|
heapq.heappush(
|
||||||
|
queue,
|
||||||
|
(score_n, n_pos, n_dir, path + (n_pos,)),
|
||||||
|
)
|
||||||
|
|
||||||
|
assert target_score is not None
|
||||||
|
yield target_score
|
||||||
|
yield len(visited)
|
||||||
|
Loading…
Reference in New Issue
Block a user