commit before new attempt

This commit is contained in:
emil
2024-10-31 02:54:58 +03:00
parent 4fbb4b4f30
commit 4ddc2b83ab
+91 -180
View File
@@ -1,225 +1,136 @@
#import time #TODO TEMP import time
# Constants and Initialization
MAP_SIZE = 8 MAP_SIZE = 9
map = [] start = (0, 0) # initial position of Neo
start = (0, 0) #initial position of Neo neo = start # position of Neo
neo = start #position of Neo observer = neo
keymaster = () #position of Keymaster keymaster = () # position of Keymaster
closed_cells = []
blocked_cells = []
passed_cells = []
steps_count = 0
accumulated_g = 0
weighted_map_dict = dict()
# Helper Functions
def get_position_input(): def get_position_input():
position_input_list = (input().split(" ")) position_input_list = input().split(" ")
return ((int)(position_input_list[0]), (int)(position_input_list[1])) return int(position_input_list[0]), int(position_input_list[1])
def get_walkable_cells(): def get_walkable_cells(obj:tuple):
walkable_cells = [] potential_positions = [
potential_positions = [] (obj[0], obj[1] + 1), (obj[0], obj[1] - 1),
potential_positions.append((neo[0], neo[1] + 1)) (obj[0] - 1, obj[1]), (obj[0] + 1, obj[1])
potential_positions.append((neo[0], neo[1] - 1)) ]
potential_positions.append((neo[0] - 1, neo[1])) return [pos for pos in potential_positions if pos[0] in range(MAP_SIZE) and pos[1] in range(MAP_SIZE) and pos not in closed_cells]
potential_positions.append((neo[0] + 1, neo[1]))
for i in potential_positions:
if i[0] in range(MAP_SIZE) and i[1] in range(MAP_SIZE) and i not in closed_cells:
walkable_cells.append(i)
return walkable_cells
def get_g(cell:tuple): def get_g(cell):
return abs(start[0] - cell[0]) + abs(start[1] - cell[1]) #TODO FROM START OR NEO? return accumulated_g + 1
def get_h(cell:tuple): def get_h(cell):
return abs(keymaster[0] - cell[0]) + abs(keymaster[1] - cell[1]) return abs(keymaster[0] - cell[0]) + abs(keymaster[1] - cell[1])
def get_f(cell:tuple): def get_f(cell):
return get_g(cell) + get_h(cell) return get_g(cell) + get_h(cell)
def get_verified_move_position(new_postion:tuple): '''def get_verified_move_position(new_position):
if new_postion in get_walkable_cells(): if new_position in get_walkable_cells():
return new_postion return new_position
else: else:
print("CAN'T MOVE HERE!") #TEMP print("CAN'T MOVE HERE!")
return neo return neo'''
def print_wheights(cell:tuple): def print_map():
print(f"{get_g(cell)} + {get_h(cell)} = {get_f(cell)}")
def print_map_f():
print("f")
map_str = "" map_str = ""
for x in range(MAP_SIZE): for x in range(MAP_SIZE):
for y in range(MAP_SIZE): for y in range(MAP_SIZE):
if x == neo[0] and y == neo[1]: if x == neo[0] and y == neo[1]:
map_str += " n " map_str += " n "
elif (x == observer[0] and y == observer[1]):
map_str += " o "
elif (x == keymaster[0] and y == keymaster[1]): elif (x == keymaster[0] and y == keymaster[1]):
map_str += " k " map_str += " k "
elif ((x,y) in passed_cells):
map_str += " # "
elif ((x,y) in closed_cells):
map_str += " = "
elif ((x,y) in blocked_cells):
map_str += " - "
else: else:
f = get_f((x,y)) map_str += " + "
if len(str(f)) == 2:
f_str = " " + str(f)
else:
f_str = " " + str(f) + " "
map_str += f_str
map_str += "\n"
print(map_str)
def print_map_g():
print("g")
map_str = ""
for x in range(MAP_SIZE):
for y in range(MAP_SIZE):
if x == neo[0] and y == neo[1]:
map_str += " n "
elif (x == keymaster[0] and y == keymaster[1]):
map_str += " k "
else:
f = get_g((x,y))
if len(str(f)) == 2:
f_str = " " + str(f)
else:
f_str = " " + str(f) + " "
map_str += f_str
map_str += "\n"
print(map_str)
def print_map_h():
print("h")
map_str = ""
for x in range(MAP_SIZE):
for y in range(MAP_SIZE):
if x == neo[0] and y == neo[1]:
map_str += " n "
elif (x == keymaster[0] and y == keymaster[1]):
map_str += " k "
else:
f = get_h((x,y))
if len(str(f)) == 2:
f_str = " " + str(f)
else:
f_str = " " + str(f) + " "
map_str += f_str
map_str += "\n" map_str += "\n"
print(map_str) print(map_str)
def read_system(): def read_system():
number_of_items = (int)(input()) number_of_items = int(input())
items = dict()
if number_of_items == 0: if number_of_items == 0:
return False return False
else: items = {}
for _ in range(number_of_items):
for i in range(number_of_items): x, y, status = input().split(' ')
input_str_split = input().split(' ') items[(int(x), int(y))] = status
input_formatted = []
input_formatted.append((int)(input_str_split[0]))
input_formatted.append((int)(input_str_split[1]))
input_formatted.append((input_str_split[2]))
items[(input_formatted[0], input_formatted[1])] = input_formatted[2] #SWAP 0 and 1 ?
return items return items
def regenerate_route():
global closed_cells, observer
accumulated_g = 0
finish = False
while not finish:
walkable_cells_and_f = {cell: get_f(cell) for cell in get_walkable_cells(observer) if cell not in blocked_cells}
min_f_value = min(walkable_cells_and_f.values())
min_f_cell_list = [cell for cell, f_value in walkable_cells_and_f.items() if f_value == min_f_value]
if len(min_f_cell_list) == 1:
next_cell = min_f_cell_list[0]
else:
next_cell = min(min_f_cell_list, key=lambda cell: get_h(cell))
observer = next_cell
accumulated_g += 1 # TODO ENSURE THAT g WORKS PROPERLY
closed_cells.append(next_cell)
print(f"m {next_cell[1]} {next_cell[0]}") # TODO FIX OR ENSURE THAT x,y OR y,x DOES NOT MAKE ANY DIFFERENCE
print_map()
time.sleep(0.2)
finish = (observer == keymaster)
def initialize_weighted_map_dict():
for x in range(MAP_SIZE):
for y in range(MAP_SIZE):
weighted_map_dict[(x, y)] = (float("inf"), float("inf"), float("inf"), '+') # (x,y) : (h, g, f, type)
# Main Logic
perception_radius = input() perception_radius = input()
position_input = get_position_input() keymaster = get_position_input()
keymaster = (position_input[0], position_input[1])
print(f"m {neo[0]} {neo[1]}")
closed_cells.extend((0, 0)) #TODO FIX START CELL SET TO =
finish = False finish = False
closed_cells = [] while not finish:
observer = neo
#print("neo:") regenerate_route()
#print(neo)
#print("keymaster:")
#print(keymaster)
walkable_cells_and_f = dict()
for i in get_walkable_cells():
walkable_cells_and_f[i] = get_f(i)
min_f_cell = min(walkable_cells_and_f, key=walkable_cells_and_f.get)
#print("min_f:")
#print(min_f_cell)
#neo = get_verified_move_position(position_input)
#print(walkable_cells_and_f)
print(f"m {neo[0]} {neo[1]}")
steps_count = 0
while (finish == False):
'''
position_input = get_position_input()
print(position_input)
neo = get_verified_move_position(position_input)
print("\nneo:")
print(neo)
print("\nkeymaster:")
print(keymaster)
print("\nwalkabe cells")
'''
#A*
recieved_input = read_system() recieved_input = read_system()
if recieved_input:
blocked_cells.extend([pos for pos, status in recieved_input.items() if status == "P"])
if recieved_input != False: walkable_cells_and_f = {cell: get_f(cell) for cell in get_walkable_cells(neo) if cell not in blocked_cells}
for i in recieved_input.items():
if i[1] == "P":
closed_cells.append(i[0])
walkable_cells_and_f = dict()
for i in get_walkable_cells():
walkable_cells_and_f[i] = get_f(i)
min_f_cell = min(walkable_cells_and_f, key=walkable_cells_and_f.get)
min_f_value = walkable_cells_and_f[min_f_cell]
# Находим минимальное значение f
min_f_value = min(walkable_cells_and_f.values()) min_f_value = min(walkable_cells_and_f.values())
# Собираем все ячейки с минимальным значением f
min_f_cell_list = [cell for cell, f_value in walkable_cells_and_f.items() if f_value == min_f_value] min_f_cell_list = [cell for cell, f_value in walkable_cells_and_f.items() if f_value == min_f_value]
# Если только одна ячейка с минимальным f, выбираем её
if len(min_f_cell_list) == 1: if len(min_f_cell_list) == 1:
next_cell = min_f_cell_list[0] next_cell = min_f_cell_list[0]
else: else:
# Если таких ячеек несколько, выбираем ту, у которой минимальный h
next_cell = min(min_f_cell_list, key=lambda cell: get_h(cell)) next_cell = min(min_f_cell_list, key=lambda cell: get_h(cell))
neo = next_cell
#time.sleep(3) #TODO TEMP accumulated_g += 1 # TODO ENSURE THAT g WORKS PROPERLY
neo = get_verified_move_position(next_cell)
closed_cells.append(next_cell) closed_cells.append(next_cell)
print(f"m {next_cell[0]} {next_cell[1]}") passed_cells.append(next_cell)
print(f"m {next_cell[1]} {next_cell[0]}") # TODO FIX OR ENSURE THAT x,y OR y,x DOES NOT MAKE ANY DIFFERENCE
steps_count += 1 steps_count += 1
finish = neo == keymaster #print_map()
''' finish = (neo == keymaster)
for i in walkable_cells_and_f.items():
cell = i[0]
print(cell)
print(f"{get_g(cell)} + {get_h(cell)} = {get_f(cell)}")
print(neo)
print(print_wheights(neo))
print_map_g()
print_map_h()
print_map_f()
print(walkable_cells_and_f)
print("min_f:")
print(min_f_cell)
print("min_f_count:")
print(len(min_f_cell_list))
#neo = get_verified_move_position(position_input)
for i in walkable_cells_and_f.items():
cell = i[0]
print(cell)
print(f"{get_g(cell)} + {get_h(cell)} = {get_f(cell)}")
print(walkable_cells_and_f)
print("next_cell:")
print(next_cell)'''
#print_map_f()
#print("FINISH!")
print(f"e {steps_count}") print(f"e {steps_count}")
# TODO CHECK TESTS FROM CODEFORCES