문제
풀이
진짜 오래걸렸다
여러분은 저처럼 쓰레기같이 풀지마세요
저는 빡구현으로 풀었는데요
생각해보니까 그냥 딕셔너리 key값에 현재 초 + 현재 좌표로 해두고 1씩 더해주면 되는 문제였어요...
이동을 한 로봇 기준으로 돌려도 되는 문제였읍니다..
코드
from collections import defaultdict
from collections import Counter
import copy
def solution(points, routes):
answer = 0
# 위치 좌표 딕셔너리 선언
positions = defaultdict(list)
for i in range(len(points)):
positions[i+1] = points[i]
# key = 로봇의 번호, value = [현재 위치, (로봇의 경로들,이미 지나간 경로인지), 로봇의 목적지 도착 여부]
robot_route = defaultdict(list)
for i in range(len(routes)):
robot_route[i+1].append(positions[routes[i][0]].copy())
for j in routes[i][1:]:
lst = positions[j].copy()
lst.append(False)
robot_route[i+1].append(lst)
robot_route[i+1].append(False)
fir = []
# 시작할 때 같은 위치에서 시작하는지 검사
for i in robot_route:
fir.append(tuple(robot_route[i][0]))
for item,cnt in Counter(fir).items():
if cnt >= 2:
answer+=1
#경로 탐색
arrive_cnt = 0
while True:
now_positions = []
# 모든 로봇이 목적지에 도착했을 경우 while문 break
if arrive_cnt == len(routes):
break
for i in range(1,len(robot_route)+1):
# 이미 목적지에 도착한 로봇인 경우 continue
if robot_route[i][-1]:
continue
# i번 로봇의 시작위치
nx,ny = robot_route[i][0]
now_destination_idx = -1
# i번 로봇의 목적지
for idx,destination in enumerate(robot_route[i][1:-1]):
if not destination[2]:
dx,dy = destination[0], destination[1]
now_destination_idx = idx+1
break
# x좌표 움직일 수 있을 때
if abs(nx-dx) >= 1:
if nx>dx:
robot_route[i][0][0] -= 1
else:
robot_route[i][0][0] += 1
elif abs(ny-dy) >= 1:
if ny>dy:
robot_route[i][0][1] -= 1
else:
robot_route[i][0][1] += 1
now_positions.append(tuple(robot_route[i][0]))
# 현재 설정된 목적지 도착시
if robot_route[i][0][0]==dx and robot_route[i][0][1]==dy:
robot_route[i][now_destination_idx][2] = True
# 마지막 목적지 도착시
if now_destination_idx == len(robot_route[i])-2:
robot_route[i][-1] = True
arrive_cnt += 1
# print(robot_route)
# 현재 로봇들의 위치 리스트 now_positions 검사
counter = Counter(now_positions)
for item,count in counter.items():
if count >= 2:
answer += 1
return answer
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
'Algorithm > Programmers' 카테고리의 다른 글
[프로그래머스] 파이썬 - 순위 검색 (KAKKO) (0) | 2024.11.01 |
---|---|
[프로그래머스] 파이썬 - 석유 시추 (PCCP) (0) | 2024.10.31 |
[프로그래머스] 파이썬 - 수식 분석하기 (PCCP) (0) | 2024.10.19 |
[프로그래머스] 파이썬 - k진수에서 소수 개수 구하기 (0) | 2024.10.11 |
[프로그래머스] 파이썬 - 스킬트리 (2) | 2024.10.09 |