Skip to content
This repository was archived by the owner on Mar 18, 2024. It is now read-only.

Commit d31535b

Browse files
zsmalla임지수
and
임지수
authored
Create 메이즈 러너 jisu.py
Create 메이즈 러너 jisu.py Co-authored-by: 임지수 <[email protected]>
1 parent 50e8db7 commit d31535b

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed

CodeTree/메이즈 러너/jisu.py

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
"""
2+
풀이 시작 : 2023-10-13 12:40 (이후 시간을 측정하지 않고 과제 느낌으로 풀이)
3+
#### 제한사항
4+
- 미로의 크기 : 4 <= N <= 10
5+
- 참가자 수 : 1 <= M <= 10
6+
- 게임 시간 : 1 <= K <= 100
7+
8+
#### 풀이
9+
1. 이동
10+
2. 회전
11+
2-1. 정사각형 찾기
12+
2-2. 회전시키기
13+
14+
풀이 완료 : 2023-10-14 18:03 (이틀동안 이거만 함 ㅎㅎ..)
15+
수행시간 | 메모리
16+
--|--
17+
327ms | 27MB
18+
"""
19+
20+
import sys
21+
from typing import Tuple
22+
from collections import deque
23+
24+
input = sys.stdin.readline
25+
26+
N, M, K = map(int, input().rstrip().split())
27+
28+
matrix = [list(map(int, input().rstrip().split())) for _ in range(N)]
29+
participants = deque(
30+
[tuple(map(lambda x: int(x) - 1, input().rstrip().split())) for _ in range(M)]
31+
)
32+
E = tuple(map(lambda x: int(x) - 1, input().rstrip().split()))
33+
34+
dy = [-1, 1, 0, 0] # 상 하 좌 우 순서
35+
dx = [0, 0, -1, 1]
36+
37+
answer = 0
38+
39+
40+
def move(py: int, px: int) -> int:
41+
"""
42+
특정 참가자를 출구와 가까운 쪽으로 1만큼 이동시킨다.
43+
이동 시 출구와 가까워지는 쪽으로, 상-하-좌-우 우선순위로 이동한다.
44+
45+
input : 이동시킬 참가자 (y, x) 좌표
46+
output : 참가자 이동 여부 (이동했으면 1, 이동하지 않았으면 0)
47+
"""
48+
# 기존 참가자와 출구까지의 최단 거리
49+
dist = abs(py - E[0]) + abs(px - E[1])
50+
51+
for d in range(4):
52+
ny = py + dy[d]
53+
nx = px + dx[d]
54+
# 이동 후 참가자와 출구까지의 최단 거리
55+
new_dist = abs(ny - E[0]) + abs(nx - E[1])
56+
# 이동 가능하며, 출구와 가까워지는 경우
57+
if 0 <= ny < N and 0 <= nx < N and matrix[ny][nx] == 0 and new_dist < dist:
58+
# 이동 후 좌표가 탈출구인 경우 탈출시키고 아닌 경우, 이동시킨 좌표를 큐에 삽입
59+
if (ny, nx) != E:
60+
participants.append((ny, nx))
61+
# 이동 성공 시 count 1 반환
62+
return 1
63+
# 이동 실패 시 원래 좌표를 다시 큐에 삽입, count 0 반환
64+
participants.append((py, px))
65+
66+
return 0
67+
68+
69+
def get_rectangle() -> Tuple[int, int, int, int]:
70+
"""
71+
출구와 최소 한 명의 참가자를 포함하는 최소 넓이 직사각형의 네 좌표를 반환한다.
72+
73+
input : x
74+
output : 직사각형의 min_y, min_x, max_y, max_x
75+
"""
76+
rectangles = []
77+
78+
for y, x in set(participants):
79+
max_y = max(y, E[0])
80+
min_y = min(y, E[0])
81+
max_x = max(x, E[1])
82+
min_x = min(x, E[1])
83+
84+
# 한 변의 길이
85+
side_len = max(max_y - min_y, max_x - min_x)
86+
# 한 변의 길이를 유지하는 최소의 좌상단 좌표를 가지는 직사각형 좌표 구하기
87+
min_y = max(max_y - side_len, 0)
88+
max_y = min_y + side_len
89+
min_x = max(max_x - side_len, 0)
90+
max_x = min_x + side_len
91+
92+
rectangles.append((side_len**2, min_y, min_x, max_y, max_x))
93+
# 우선순위 : 넓이 > 좌상단 y좌표 > 좌상단 x좌표
94+
return min(rectangles)[1:]
95+
96+
97+
def rotate(min_y: int, min_x: int, max_y: int, max_x: int) -> None:
98+
"""
99+
rotate시킬 정사각형의 좌표를 받아 시계방향으로 90도 회전시킨다.
100+
101+
input : rotate 시킬 정사각형의 min_y, min_x, max_y, max_x 좌표
102+
output : x
103+
"""
104+
global E
105+
106+
participants_set = set(participants)
107+
# rotate 시킬 부분의 임시 matrix
108+
rotate_matrix = [
109+
[0 for _ in range(min_x, max_x + 1)] for _ in range(min_y, max_y + 1)
110+
]
111+
112+
# rotate전 정보 백업
113+
for y in range(min_y, max_y + 1):
114+
for x in range(min_x, max_x + 1):
115+
# 벽의 경우 내구도 -1 시켜 백업
116+
if matrix[y][x] > 0:
117+
rotate_matrix[y - min_y][x - min_x] = matrix[y][x] - 1
118+
# 출구 좌표 정보 백업
119+
elif (y, x) == E:
120+
rotate_matrix[y - min_y][x - min_x] = "E"
121+
# 참가자 좌표 정보 백업
122+
elif (y, x) in participants_set:
123+
# 참가자는 동일 좌표에 여러명이 위치할 수 있음
124+
cnt = 0
125+
while (y, x) in participants:
126+
cnt += 1
127+
participants.remove((y, x))
128+
# 참가자 한 명당 *의 개수로 백업
129+
rotate_matrix[y - min_y][x - min_x] = "*" * cnt
130+
131+
# 해당 백업 matrix 90도 회전
132+
rotate_matrix = [list(row)[::-1] for row in zip(*rotate_matrix)]
133+
134+
# 원본 matrix의 정사각형 위치를 90도 회전시킨 백업 matrix로 대치
135+
for y in range(min_y, max_y + 1):
136+
for x in range(min_x, max_x + 1):
137+
# 회전 후 참가자 위치를 다시 큐에 삽입
138+
if str(rotate_matrix[y - min_y][x - min_x]).startswith("*"):
139+
# 기존 존재하던 참가자 수만큼 삽입
140+
for _ in range(len(rotate_matrix[y - min_y][x - min_x])):
141+
participants.append((y, x))
142+
matrix[y][x] = 0
143+
# 회전 후 출구 위치
144+
elif rotate_matrix[y - min_y][x - min_x] == "E":
145+
E = (y, x)
146+
matrix[y][x] = 0
147+
# 회전 후 벽의 내구도
148+
else:
149+
matrix[y][x] = rotate_matrix[y - min_y][x - min_x]
150+
151+
152+
# 최대 k초 이동 가능
153+
for i in range(K):
154+
# 참가자 당 한 번씩 move
155+
M = len(participants)
156+
for _ in range(M):
157+
answer += move(*participants.popleft())
158+
# 이동 과정에서 참가자가 모두 탈출한 경우 종료
159+
if not participants:
160+
break
161+
# 정사각형 좌표를 구해 해당 위치 90도 rotate
162+
rotate(*get_rectangle())
163+
164+
print(answer)
165+
print(E[0] + 1, E[1] + 1)

0 commit comments

Comments
 (0)