92 lines
2.2 KiB
Python
92 lines
2.2 KiB
Python
import time
|
|
from os import path, system
|
|
|
|
|
|
def preberi_zemljevid(ime_dat: str):
|
|
directory = path.dirname(path.abspath(__file__))
|
|
file = open(path.join(directory, ime_dat))
|
|
return file.read().strip().split("\n")
|
|
|
|
|
|
z = [list(" " + c for c in line) for line in preberi_zemljevid("test.txt")]
|
|
|
|
|
|
def print_z():
|
|
system('clear')
|
|
for line in z:
|
|
print("".join(line))
|
|
|
|
|
|
def check(x: int, y: int, c: str, zemljevid: list[list[str]], prepovedani: set = None):
|
|
if prepovedani is None:
|
|
prepovedani = set()
|
|
|
|
return (
|
|
(x, y) not in prepovedani
|
|
and 0 <= y < len(zemljevid)
|
|
and 0 <= x < len(zemljevid[0])
|
|
and (c == "" and zemljevid[y][x] != "." or zemljevid[y][x] == c)
|
|
)
|
|
|
|
|
|
counter = 1
|
|
|
|
|
|
def najblizji_distance(x: int, y: int, c: str, zemljevid: list[list[str]], prepovedani: set):
|
|
global counter
|
|
|
|
z[y][x] = " ."
|
|
|
|
d = 1
|
|
max_d = len(zemljevid) + len(zemljevid[0])
|
|
|
|
while d <= max_d:
|
|
x -= 1
|
|
|
|
ry = 0
|
|
ry_inc = 1
|
|
|
|
for rx in range(d * 2 + 1):
|
|
if (0 <= y - ry < len(zemljevid)
|
|
and 0 <= x + rx < len(zemljevid[0])):
|
|
z[y - ry][x + rx] = (" " * (5 - len(str(d)))) + str(d)
|
|
|
|
print_z()
|
|
counter += 1
|
|
|
|
if (0 <= y + ry < len(zemljevid)
|
|
and 0 <= x + rx < len(zemljevid[0])):
|
|
z[y + ry][x + rx] = (" " * (5 - len(str(d)))) + str(d)
|
|
|
|
print_z()
|
|
counter += 1
|
|
|
|
if check(x + rx, y + ry, c, zemljevid, prepovedani):
|
|
return x + rx, y + ry, d
|
|
if check(x + rx, y - ry, c, zemljevid, prepovedani):
|
|
return x + rx, y - ry, d
|
|
if ry == d:
|
|
ry_inc = -1
|
|
ry += ry_inc
|
|
|
|
d += 1
|
|
|
|
|
|
def angelca(x, y, znamenitosti, zemljevid):
|
|
prepovedani = set()
|
|
distance = 0
|
|
for znam in znamenitosti:
|
|
res = najblizji_distance(x, y, znam, zemljevid, prepovedani)
|
|
if res is None:
|
|
return distance
|
|
x, y, d = res
|
|
prepovedani.add((x, y))
|
|
distance += d
|
|
|
|
return distance
|
|
|
|
|
|
res = najblizji_distance(37, 14, "c", z, set())
|
|
|
|
print(res)
|