Skip to content

Commit 387b4b8

Browse files
committed
[PGS] 거리두기 확인하기 / Level 2 / 40분 / 성공
1 parent 1e6578b commit 387b4b8

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
def solution(places):
2+
def is_safe(place):
3+
for i in range(5):
4+
for j in range(5):
5+
if place[i][j] != 'P':
6+
continue
7+
8+
for dx, dy in dirs:
9+
ni, nj = i + dx, j + dy
10+
if not (0 <= ni < 5 and 0 <= nj < 5):
11+
continue
12+
if place[ni][nj] != 'P':
13+
continue
14+
15+
dist = abs(dx) + abs(dy)
16+
if dist == 1:
17+
return 0 # 거리 1에서 바로 P면 위반
18+
elif dist == 2:
19+
# 파티션 여부 확인
20+
if dx == 0: # 수평
21+
if place[i][j + dy // 2] != 'X':
22+
return 0
23+
elif dy == 0: # 수직
24+
if place[i + dx // 2][j] != 'X':
25+
return 0
26+
else: # 대각선
27+
if place[i][nj] != 'X' or place[ni][j] != 'X':
28+
return 0
29+
return 1
30+
31+
dirs = [
32+
(-1, 0), (1, 0), (0, -1), (0, 1), # 거리 1
33+
(-2, 0), (2, 0), (0, -2), (0, 2), # 일직선 거리 2
34+
(-1, -1), (-1, 1), (1, -1), (1, 1) # 대각선 거리 2
35+
]
36+
37+
answer = []
38+
for place in places:
39+
answer.append(is_safe(place))
40+
41+
return answer
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
def solution(places):
2+
def is_safe(place):
3+
for i in range(5):
4+
for j in range(5):
5+
if place[i][j] != 'P':
6+
continue
7+
8+
for dx, dy in dirs:
9+
ni, nj = i + dx, j + dy
10+
if not (0 <= ni < 5 and 0 <= nj < 5):
11+
continue
12+
if place[ni][nj] != 'P':
13+
continue
14+
15+
dist = abs(dx) + abs(dy)
16+
if dist == 1:
17+
return 0 # 거리 1에서 바로 P면 위반
18+
elif dist == 2:
19+
# 파티션 여부 확인
20+
if dx == 0: # 수평
21+
if place[i][j + dy // 2] != 'X':
22+
return 0
23+
elif dy == 0: # 수직
24+
if place[i + dx // 2][j] != 'X':
25+
return 0
26+
else: # 대각선
27+
if place[i][nj] != 'X' or place[ni][j] != 'X':
28+
return 0
29+
return 1
30+
31+
dirs = [
32+
(-1, 0), (1, 0), (0, -1), (0, 1), # 거리 1
33+
(-2, 0), (2, 0), (0, -2), (0, 2), # 일직선 거리 2
34+
(-1, -1), (-1, 1), (1, -1), (1, 1) # 대각선 거리 2
35+
]
36+
37+
answer = []
38+
for place in places:
39+
answer.append(is_safe(place))
40+
41+
return answer

0 commit comments

Comments
 (0)