Backtracking done!

This commit is contained in:
emil
2024-11-01 18:49:52 +03:00
parent bc8c3a90d5
commit b9dafcf241
3 changed files with 64 additions and 43 deletions
+43 -32
View File
@@ -1,72 +1,83 @@
import sys
import heapq
min_costs = [[100]*9 for temp in range(9)]
hs = [[0]*9 for temp in range(9)]
astar_map = [['.']*9 for temp in range(9)]
visited_nodes = [[False]*9 for temp in range(9)]
node_parents = [[None]*9 for temp in range(9)]
# Initialize cost, heuristic, map, visited nodes, and parent tracking arrays
min_costs = [[100]*9 for _ in range(9)] # Initialize minimum cost array with a high value (100)
hs = [[0]*9 for _ in range(9)] # Heuristic array for A* (Manhattan distance)
astar_map = [['.']*9 for _ in range(9)] # Initial unexplored map with '.'
visited_nodes = [[False]*9 for _ in range(9)] # Track visited nodes
node_parents = [[None]*9 for _ in range(9)] # Track path parents for backtracking
perception_radius = int(input())
# Input: perception radius and Keymaker position
perception_radius = int(input()) # 1 or 2 for Neos perception variant
input_list = input().split()
goal_x, goal_y = int(input_list[0]), int(input_list[1])
goal_x, goal_y = int(input_list[0]), int(input_list[1]) # Keymakers coordinates
# Set up heuristic values (Manhattan distance) and initial costs for A*
for i in range(9):
for j in range(9):
hs[j][i] = abs(j - goal_y) + abs(i - goal_x)
min_costs[j][i] = 100
hs[j][i] = abs(j - goal_y) + abs(i - goal_x) # Calculate heuristic distance
min_costs[j][i] = 100 # Set initial high cost for all cells
min_costs[0][0] = 0
min_costs[0][0] = 0 # Starting position (0,0) cost is zero
# Priority queue for A* with starting point at (0,0)
priority_queue = []
heapq.heappush(priority_queue, (min_costs[0][0] + hs[0][0], 0, 0))
heapq.heappush(priority_queue, (min_costs[0][0] + hs[0][0], 0, 0)) # Push initial cell to queue
# Main A* loop
while len(priority_queue) != 0:
# Extract node with lowest f = g + h value
temp, current_x, current_y = heapq.heappop(priority_queue)
if visited_nodes[current_y][current_x]:
continue
visited_nodes[current_y][current_x] = True
visited_nodes[current_y][current_x] = True # Mark node as visited
# Backtrack to get the path to current node
parent_node = node_parents[current_y][current_x]
path_to_current = []
path_to_current.append((current_x, current_y))
path_to_current = [(current_x, current_y)]
while parent_node is not None:
path_to_current.append(parent_node)
parent_node = node_parents[parent_node[1]][parent_node[0]]
# Execute path, querying for perception data
for i in reversed(range(len(path_to_current))):
print(f"m {path_to_current[i][0]} {path_to_current[i][1]}")
neighbor_count = int(input())
for temp in range(neighbor_count):
neighbor_count = int(input()) # Read the number of perceived items
# Update map with perceived items
for _ in range(neighbor_count):
input_data = input().split()
neighbor_x_str, neighbor_y_str, neighbor_char = input_data[0], input_data[1], input_data[2]
neighbor_x = int(neighbor_x_str)
neighbor_y = int(neighbor_y_str)
neighbor_char = neighbor_char[0]
astar_map[neighbor_y][neighbor_x] = neighbor_char
neighbor_x = int(input_data[0])
neighbor_y = int(input_data[1])
neighbor_char = input_data[2][0] # Character representing item
astar_map[neighbor_y][neighbor_x] = neighbor_char # Update map cell
for dx, dy in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
# Explore neighboring cells
for dx, dy in [(1, 0), (0, 1), (-1, 0), (0, -1)]: # Move in four directions
neighbor_x = current_x + dx
neighbor_y = current_y + dy
# Check boundaries and if cell is unexplored and safe
if 0 <= neighbor_x < 9 and 0 <= neighbor_y < 9 and not visited_nodes[neighbor_y][neighbor_x] and astar_map[neighbor_y][neighbor_x] not in ('P', 'A', 'S'):
# Update cost if a better path is found
if min_costs[neighbor_y][neighbor_x] > min_costs[current_y][current_x] + 1:
node_parents[neighbor_y][neighbor_x] = (current_x, current_y)
min_costs[neighbor_y][neighbor_x] = min_costs[current_y][current_x] + 1
# Add node to priority queue with updated f = g + h value
heapq.heappush(priority_queue, (min_costs[neighbor_y][neighbor_x] + hs[neighbor_y][neighbor_x], neighbor_x, neighbor_y))
# Repeat path execution to keep querying
for i in range(len(path_to_current)):
print(f"m {path_to_current[i][0]} {path_to_current[i][1]}")
neighbor_count = int(input())
for temp in range(neighbor_count):
neighbor_count = int(input()) # Re-read surroundings
for _ in range(neighbor_count):
input_data = input().split()
neighbor_x_str, neighbor_y_str, neighbor_char = input_data[0], input_data[1], input_data[2]
neighbor_x = int(neighbor_x_str)
neighbor_y = int(neighbor_y_str)
neighbor_char = neighbor_char[0]
astar_map[neighbor_y][neighbor_x] = neighbor_char
neighbor_x = int(input_data[0])
neighbor_y = int(input_data[1])
neighbor_char = input_data[2][0]
astar_map[neighbor_y][neighbor_x] = neighbor_char # Update map
# Check if the goal is reached and output the result
if min_costs[goal_y][goal_x] != 100:
print(f"e {min_costs[goal_y][goal_x]}")
print(f"e {min_costs[goal_y][goal_x]}") # Output shortest path length
else:
print("e -1")
print("e -1") # Output -1 if unsolvable
+18 -3
View File
@@ -1,6 +1,20 @@
import sys
import heapq
test_number = 0
with open("20k_testset.txt", "r") as file:
lines = file.readlines()
#print(lines)
current_line = lines[0]
while(test_number < 10):
local_line_number = 0
for line_number in range(14):
local_line_number += 1
current_line += lines[test_number * 14 + local_line_number]
print(current_line)
test_number += 1
print(test_number)
min_costs = [[100]*9 for temp in range(9)]
hs = [[0]*9 for temp in range(9)]
astar_map = [['.']*9 for temp in range(9)]
@@ -71,6 +85,7 @@ if min_costs[goal_y][goal_x] != 100:
else:
print("e -1")
test_number = 0
while(test_number != 1000):
+3 -8
View File
@@ -9,11 +9,9 @@ def main():
n = int(input())
position_input = input().split()
x = position_input[0]
y = position_input[1]
#keymaker.append(int(x))
#keymaker.append(int(y))
#print(keymaker)
x = (int)(position_input[0])
y = (int)(position_input[1])
minDists[0][0] = 0
findShortestPath(0, 0)
if minDists[y][x] == 100:
@@ -22,9 +20,6 @@ def main():
print("e " + str(minDists[y][x]))
def exploreMap(x, y):
#if x == keymaker[0] and y == keymaker[1]:
# print("e " + str(minDists[y][x]))
# exit(0)
print(f"m {x} {y}")
n = int(input())
for _ in range(n):