Algorithm PS/프로그래머스
[프로그래머스] level2 / 거리두기 확인하기 / Python 파이썬
migrationArc
2021. 8. 30. 21:36
[프로그래머스] level2 / 거리두기 확인하기 / Python 파이썬
https://programmers.co.kr/learn/courses/30/lessons/81302
풀이
주어진 지도를 탐색하고 조건에 부합하는지 확인하는 문제이다.
거리두기 조건은 직선 1칸, 직선 2칸 그리고 대각선 거리로 나누어 확인해 주었다.
answer = []
oneBlock_dy = [1, -1, 0, 0]
oneBlock_dx = [0, 0, 1, -1]
diagonal_dy = [1, 1, -1, -1]
diagonal_dx = [1, -1, 1, -1]
# 직선 한칸
def check_oneBlock(place, y, x):
for i in range(4):
if place[y][x] == 'P':
Y = y + oneBlock_dy[i]
X = x + oneBlock_dx[i]
if 0 <= Y < 5 and 0 <= X < 5 and place[Y][X] == 'P':
return False
return True
# 직선 두칸
def check_twoBlock(place, y, x):
for i in range(4):
if place[y][x] == 'P':
Y = y + (oneBlock_dy[i] * 2)
X = x + (oneBlock_dx[i] * 2)
if 0 <= Y < 5 and 0 <= X < 5 and place[Y][X] == 'P':
if place[y + oneBlock_dy[i]][x + oneBlock_dx[i]] != 'X':
return False
return True
# 대각선
def check_diagonal(place, y, x):
for i in range(4):
if place[y][x] == 'P':
Y = y + diagonal_dy[i]
X = x + diagonal_dx[i]
if 0 <= Y < 5 and 0 <= X < 5 and place[Y][X] == 'P':
if place[y][x+diagonal_dx[i]] != 'X' or place[y+diagonal_dy[i]][x] != 'X':
return False
return True
def checkPlace(place):
for y in range(5):
for x in range(5):
if place[y][x] == 'P':
if not check_oneBlock(place, y, x):
answer.append(0)
return
if not check_twoBlock(place, y, x):
answer.append(0)
return
if not check_diagonal(place, y, x):
answer.append(0)
return
answer.append(1)
return
def solution(places):
for place in places:
checkPlace(place)
return answer