프로그래머스 81302번 거리두기 확인하기 풀이 ( 파이썬 )
https://programmers.co.kr/learn/courses/30/lessons/81302
코딩테스트 연습 - 거리두기 확인하기
[["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"]] [1, 0, 1, 1, 1]
programmers.co.kr
문제 설명에 나온 경우들을 모두 구현하여 이 문제를 해결했다.
from collections import deque
def judge(place):
people = deque()
dx = [-1,-1,0,1,1,1,0,-1]
dy = [0,-1,-1,-1,0,1,1,1]
for i in range(5):
for j in range(5):
if place[i][j] == "P":
people.append((i, j))
while people:
x,y = people.popleft()
for i in range(8):
nx = x + dx[i]
ny = y + dy[i]
if nx < 0 or ny < 0 or nx >= 5 or ny >= 5:
continue
if i == 0 or i == 2 or i == 4 or i == 6:
if place[nx][ny] == "P":
return False
else:
nnx = nx + dx[i]
nny = ny + dy[i]
if nnx < 0 or nny < 0 or nnx >= 5 or nny >= 5:
continue
if place[nnx][nny] == "P" and place[nx][ny] == "O":
return False
if place[nx][ny] == "P":
if i == 1:
if place[x - 1][y] == "O" or place[x][y - 1] == "O":
return False
if i == 3:
if place[x + 1][y] == "O" or place[x][y - 1] == "O":
return False
if i == 5:
if place[x + 1][y] == "O" or place[x][y + 1] == "O":
return False
if i == 7:
if place[x - 1][y] == "O" or place[x][y + 1] == "O":
return False
return True
def solution(places):
answer = []
for place in places:
if judge(place):
answer.append(1)
else:
answer.append(0)
return answer
[ programmers ] 행렬 테두리 회전하기 77485번 ( python ) (0) | 2021.09.08 |
---|---|
[ programmers ] 입국심사 43238번 ( python ) (0) | 2021.09.06 |
[ programmers ] 위클리 챌린지 5주차 84512번 ( python ) (0) | 2021.09.01 |
[ programmers ] 약수의 개수와 덧셈 77884번 ( python ) (0) | 2021.08.25 |
[ programmers ] 베스트앨범 42579번 ( python ) (0) | 2021.08.21 |