https://school.programmers.co.kr/learn/courses/30/lessons/81302
잘못 한 풀이
→ False가 되는 경우의 수를 놓침
if board[nx][ny]=="1" or board[nx][ny]=="P":
return False
여기서 board[nx][ny]=="P" 를 놓쳐서 틀림. ⇒ 테스트 케이스도 통과해서 잡지 못하고 제출했엇음.
접근 방법
강의실 별로 판단함수 만들어서,
안전거리 만들어서, 안전거리가 충돌할때 False 반환하도록 설계.
단, (px,py)가 “안전거리와 겹칠때” OR “다른 사람이 있는 경우” 테스트 케이스 직접 설계헤서 이 경우 뺴먹지 않고 했어야했음
def solution(places):
N=5
dx=[-1,1,0,0]
dy=[0,0,-1,1]
def is_safe(place):
board=[]
for x in range(N): # O(N)
board.append(list(place[x]))
persons=[] # O(N^2)
for x in range(N):
for y in range(N):
if board[x][y]=="P":
persons.append((x,y))
for px,py in persons: # O(N^2)
for i in range(4):
nx=px+dx[i]
ny=py+dy[i]
if 0<=nx<N and 0<=ny<N:
if board[nx][ny]=="1" or board[nx][ny]=="P":
return False
if board[nx][ny]=="X":
continue
board[nx][ny]="1"
return True
answer = []
for place in places:
if is_safe(place):
answer.append(1)
else:
answer.append(0)
return answer