Created
September 22, 2024 02:56
-
-
Save overnew/1ab447bce5b0813740744da2948187d8 to your computer and use it in GitHub Desktop.
[PCCP 기출문제] 3번 / 충돌위험 찾기
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list = [] | |
cnt = 0 | |
g_points = [] | |
g_routes = [] | |
robot_pos_list = [] | |
robot_num = 0 | |
end_robot_check = [False for i in range(101)] | |
end_robot_cnt = 0 | |
def move_cycle(): | |
global end_robot_check, g_routes, g_points | |
for i, robot_pos in enumerate(robot_pos_list): | |
if end_robot_check[i]: | |
continue | |
dest_num = g_routes[i][0] - 1 | |
dest_pos = g_points[dest_num].copy() | |
#print("robot: " + str(i + 1) + ", pos: " + str(robot_pos) + ", dest_index: " + str(dest_num) + ", dest: " + str(dest_pos), ) | |
if robot_pos[0] != dest_pos[0]: | |
if robot_pos[0] < dest_pos[0]: | |
robot_pos[0] = robot_pos[0] + 1 | |
else: | |
robot_pos[0] = robot_pos[0] - 1 | |
else: | |
if robot_pos[1] < dest_pos[1]: | |
robot_pos[1] += 1 | |
else: | |
robot_pos[1] -= 1 | |
def check_conflict(): | |
global cnt, robot_num,end_robot_check | |
#conflict_num = 0 | |
pos_dict = dict() | |
for i, robot_pos in enumerate(robot_pos_list): | |
if end_robot_check[i]: | |
continue | |
key=str(robot_pos) | |
if pos_dict.get(key) == None: | |
pos_dict[key] = 1 | |
else: | |
pos_dict[key] += 1 | |
#print(pos_dict.keys()) | |
for i in pos_dict.values(): | |
if i > 1: | |
cnt += 1 | |
#print(cnt) | |
def end_robot(): | |
global end_robot_cnt, end_robot_check, g_routes | |
for i, robot_pos in enumerate(robot_pos_list): | |
#print("enumer" + str(i)) | |
if end_robot_check[i]: | |
continue | |
dest_num = g_routes[i][0] - 1 | |
dest_pos = g_points[dest_num] | |
if robot_pos[0] == dest_pos[0] and robot_pos[1] == dest_pos[1]: | |
if len(g_routes[i]) <= 1: | |
end_robot_check[i] = True | |
end_robot_cnt += 1 | |
else: | |
del g_routes[i][0] | |
def replace_robot(): | |
for i, list in enumerate(g_routes): | |
start_pos = list[0] | |
robot_pos_list.append(g_points[start_pos-1].copy()) | |
del g_routes[i][0] #시작 지점 삭제 | |
def solution(points, routes): | |
global g_points, g_routes,end_robot_cnt | |
g_routes = routes | |
g_points = points | |
global robot_num | |
robot_num = len(routes) | |
replace_robot() | |
check_conflict() | |
end_robot() | |
#loop =0 | |
while True: #loop < 10: # | |
move_cycle() | |
check_conflict() | |
end_robot() | |
#print(str(end_robot_check)) | |
if end_robot_cnt >= robot_num: | |
break | |
#print(end_robot_check) | |
#loop+=1 | |
return cnt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment