Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Bats Bunker by freeman_lex
from math import sqrt
def checkio(cave):
if cave[0][0] == 'A':
return 0
def vis_bat(x, y, bats):
visib_bats = []
for x1, y1 in bats:
miX, maX = min(x, x1), max(x, x1)
miY, maY = min(y, y1), max(y, y1)
if x == x1:
area = [(x, i) for i in range(miY + 1, maY)]
elif y == y1:
area = [(i, y) for i in range(miX + 1, maX)]
else:
area = []
line = y - y1, x1 - x,\
(x1 + 0.5) * (y + 0.5) - (x + 0.5) * (y1 + 0.5)
for cordX in range(miX + 1, maX + 1):
cordY = (line[2] - cordX * line[0]) / line[1]
cordY1 = int(cordY)
for z in range(1 if cordY - cordY1 else 2):
area += [(cordX - 1, cordY1 - z), (cordX, cordY1 - z)]
for cordY in range(miY + 1, maY + 1):
cordX = (line[2] - cordY * line[1]) / line[0]
cordX1 = int(cordX)
for z in range(1 if cordX - cordX1 else 2):
area += [(cordX1 - z, cordY - 1), (cordX1 - z, cordY)]
if 'W' not in [cave[i][j] for i, j in set(area)]:
visib_bats.append((x1, y1))
return visib_bats
bats = [(r, c) for r in range(len(cave)) for c in range(len(cave[0]))
if cave[r][c] in 'BA']
bats.remove((0, 0))
result = len(cave) * len(cave[0])
sol = [(0, 0, bats, 0)]
while sol:
for x, y, bats, dist in sol.copy():
for x1, y1 in vis_bat(x, y, bats):
dist1 = dist + sqrt((x1 - x)**2 + (y1 - y)**2)
if dist1 < result:
if cave[x1][y1] == 'A':
result = dist1
continue
bats1 = bats.copy()
bats1.remove((x1, y1))
sol.append((x1, y1, bats1, dist1))
sol.remove((x, y, bats, dist))
return float(format(result, '.2f'))
June 7, 2016
Comments: