|
@@ -14,11 +14,9 @@ def shortest_paths(grid, w):
|
|
|
explored = {start}
|
|
explored = {start}
|
|
|
while work:
|
|
while work:
|
|
|
loc, dist = work.popleft()
|
|
loc, dist = work.popleft()
|
|
|
-
|
|
|
|
|
node = grid[loc]
|
|
node = grid[loc]
|
|
|
if node.isdigit():
|
|
if node.isdigit():
|
|
|
dists[int(node)] = dist
|
|
dists[int(node)] = dist
|
|
|
-
|
|
|
|
|
for nb in (loc - 1, loc + 1, loc - w, loc + w):
|
|
for nb in (loc - 1, loc + 1, loc - w, loc + w):
|
|
|
if grid[nb] != '#' and nb not in explored:
|
|
if grid[nb] != '#' and nb not in explored:
|
|
|
explored.add(nb)
|
|
explored.add(nb)
|
|
@@ -32,8 +30,7 @@ def visit_steps(dists, back):
|
|
|
dists = shortest_paths(grid, w)
|
|
dists = shortest_paths(grid, w)
|
|
|
best = 100000000
|
|
best = 100000000
|
|
|
for path in permutations(range(1, len(dists))):
|
|
for path in permutations(range(1, len(dists))):
|
|
|
- total = 0
|
|
|
|
|
- prev = 0
|
|
|
|
|
|
|
+ total = prev = 0
|
|
|
for loc in path:
|
|
for loc in path:
|
|
|
total += dists[prev][loc]
|
|
total += dists[prev][loc]
|
|
|
prev = loc
|
|
prev = loc
|
|
@@ -42,7 +39,6 @@ def visit_steps(dists, back):
|
|
|
best = min(best, total)
|
|
best = min(best, total)
|
|
|
return best
|
|
return best
|
|
|
|
|
|
|
|
-
|
|
|
|
|
grid, w = read_grid(sys.stdin)
|
|
grid, w = read_grid(sys.stdin)
|
|
|
dists = shortest_paths(grid, w)
|
|
dists = shortest_paths(grid, w)
|
|
|
print(visit_steps(dists, False))
|
|
print(visit_steps(dists, False))
|