https://school.programmers.co.kr/learn/courses/30/lessons/81302
구현 문제였다고 생각된다.
로직은 간단했으나 코드가 길어졌다.
# 프로그래머스 거리두기 확인하기
def solution(places):
outer = [[0, 2], [2, 0]]
diag = [[1, 1], [1, -1]]
inner = [[0, 1], [1, 0]]
def distance_check(i, j, place):
# 2칸 아래, 위, 좌, 우에 P 있는지 확인 -> 있으면 중심과 P 사이 X 있는지 확인
for di, dj in outer:
ni, nj = i + di, j + dj
if (0 <= ni < 5) and (0 <= nj < 5) and place[ni][nj] == 'P':
if place[i + (di//2)][j + (dj//2)] != 'X':
return 0
# 대각선 1칸 거리에 P 있는지 확인 -> 있으면 중심과 P 사이 X 있는지 확인(2개)
for di, dj in diag:
ni, nj = i + di, j + dj
if (0 <= ni < 5) and (0 <= nj < 5) and place[ni][nj] == 'P':
if place[i][nj] != 'X' or place[ni][j] != 'X':
return 0
# 상하좌우에 P 있는지 확인
for di, dj in inner:
ni, nj = i + di, j + dj
if (0 <= ni < 5) and (0 <= nj < 5) and place[ni][nj] == 'P':
return 0
return 1
answer = []
for place in places:
flag = 1
for row in range(5):
if flag == 0:
break
for col in range(5):
if place[row][col] == 'P':
flag = distance_check(row, col, place)
if flag == 0:
break
answer.append(flag)
return answer
places = [["POOOP", "OXXOX", "OPXPX", "OOXOX", "POXXP"], ["POOPX", "OXPXP", "PXXXO", "OXXXO", "OOOPP"], ["PXOPX", "OXOXP", "OXPOX", "OXXOP", "PXPOX"], ["OOOXX", "XOOOX", "OOOXX", "OXOOX", "OOOOO"], ["PXPXP", "XPXPX", "PXPXP", "XPXPX", "PXPXP"]]
print(solution(places))
def check(place):
for irow, row in enumerate(place):
for icol, cell in enumerate(row):
if cell != 'P':
continue
if irow != 4 and place[irow + 1][icol] == 'P':
return 0
if icol != 4 and place[irow][icol + 1] == 'P':
return 0
if irow < 3 and place[irow + 2][icol] == 'P' and place[irow + 1][icol] != 'X':
return 0
if icol < 3 and place[irow][icol + 2] == 'P' and place[irow][icol + 1] != 'X':
return 0
if irow != 4 and icol != 4 and place[irow + 1][icol + 1] == 'P' and (place[irow + 1][icol] != 'X' or place[irow][icol + 1] != 'X'):
return 0
if irow != 4 and icol != 0 and place[irow + 1][icol - 1] == 'P' and (place[irow + 1][icol] != 'X' or place[irow][icol - 1] != 'X'):
return 0
return 1
def solution(places):
return [check(place) for place in places]
보고 공부하기