728x90
반응형
Python
a, b = [-1, 0, 0, 1], [0, -1, 1, 0]#이중배열은 상하좌우 좌표로 푸는 거 기억!
#a, b 리스트는 상하좌우 하나씩 움직일 수 있는 배열!
#상하좌우 검사 후 p가 있으면 0, 상하좌우가 O로 되어있고, O의 상하좌우에 P가 0(자기자신 빼고)
def solution(places):
answer = []
for place in places:
answer.append(checkIsOk(place))
return answer
def checkIsOk(places):
for i, place in enumerate(places):
for j in range(len(place)):
if place[j] != "P":
continue
for k in range(4):
if (i+a[k]) >= 0 and (j+b[k]) >= 0 and (i+a[k]) < 5 and (j+b[k]) < 5:
if places[i+a[k]][j+b[k]] == "P":
return 0
if places[i+a[k]][j+b[k]] == "O":
if checkMore(places, i+a[k], j+b[k], i, j):
return 0
return 1
def checkMore(places, x, y, no_check_x, no_check_y):
for k in range(4):
if x+a[k] >= 0 and y+b[k] >= 0 and x+a[k] < 5 and y+b[k] < 5:
if x+a[k] == no_check_x and y+b[k] == no_check_y:
continue
if places[x+a[k]][y+b[k]] == "P":
return True
return False
728x90
반응형