# Читаем инпут и создаем масив состоящий из списков в списке # Х это первы список и "вертикалб" # У это подсписок с "горизонталь" input = """..@@.@@@@. @@@.@.@.@@ @@@@@.@.@@ @.@@@@..@. @@.@@@@.@@ .@@@@@@@.@ .@.@.@.@@@ @.@@@.@@@@ .@@@@@@@@. @.@.@@@.@.""" # with open("input.txt", "r") as file: # input = file.read() karta = [] for row in input.splitlines(): karta.append(list(map(str, row.strip()))) for row in karta:print("".join(row)) # проверка существования поля def fild_exist(x:int,y:int): if x < 0 or x > len(karta) -1: return False elif y < 0 or y > len(karta[x]) -1: return False else: return True # Функция проверки def cheker(x: int, y: int): roll_count = 0 filds_to_check = 9 # print(f"cheker for {x} {y}") # print(f"roll_count {roll_count}\nfilds_to_check {filds_to_check}") while (int(roll_count) >= 4) is False or (filds_to_check <= 0) is False: # print(f"Предположительные координаты для range: x {list(range(x - 1,x + 2))}, y {list(range(y - 1, y +2))}") for n_x in list(range(x - 1,x + 2)): for n_y in list(range(y - 1, y +2)): # print(f"Существует поле {n_x} {n_y}? {fild_exist(n_x,n_y)}") if fild_exist(n_x,n_y): if karta[n_x][n_y] != "." and not (n_x == x and n_y == y): roll_count += 1 filds_to_check -= 1 # print(f"ADD +1 to roll_count\nroll_count {roll_count}\nfilds_to_check {filds_to_check}") elif n_x == x and n_y == y: # print(f"Do not self chek!!!\nroll_count {roll_count}\nfilds_to_check {filds_to_check}") filds_to_check -= 1 else: # print(f"field has a .\nroll_count {roll_count}\nfilds_to_check {filds_to_check}") filds_to_check -= 1 else: # print(f"FIeld dos'n exist\nroll_count {roll_count}\nfilds_to_check {filds_to_check}") filds_to_check -= 1 # print("FINAL roll_count =",roll_count) # print(filds_to_check <= 0) # print("="*100) return roll_count # Основное тело result = 0 for x, val_x in enumerate(karta): for y, val_y in enumerate(karta[x]): # print(f"проверяем координаты: x={x}, y={y}") if karta[x][y] != ".": if cheker(x, y) < 4: karta[x][y] = "x" result += 1 # print(f"cheker for {x} {y} is {cheker(x,y)}") else: pass else: pass print("="*(len(karta[0]))) for row in karta:print("".join(row)) print("Result",result)