Written by
최태열
on
on
[Python][프로그래머스][Level4] 블록 게임
[Python][프로그래머스][Level4] 블록 게임
코드
def which(board):
ans=[]
for x,b in enumerate(board):
for y,num in enumerate(b):
if num:
if x<len(board)-1:
if y<len(board)-2 and [num,num,num]==[board[x+1][y],board[x+1][y+1],board[x+1][y+2]]:
ans.append([1,x,y])
elif y>1 and [num,num,num]==[board[x+1][y],board[x+1][y-1],board[x+1][y-2]]:
ans.append([2,x,y])
elif y<len(board)-1 and y>0 and [num,num,num]==[board[x+1][y],board[x+1][y-1],board[x+1][y+1]]:
ans.append([3,x,y])
if x<len(board)-2:
if y<len(board)-1 and [num,num,num]==[board[x+1][y],board[x+2][y],board[x+2][y+1]]:
ans.append([4,x,y])
elif y>0 and [num,num,num]==[board[x+1][y],board[x+2][y],board[x+2][y-1]]:
ans.append([5,x,y])
return ans
def solution(board):
answer = 0
check1=[]
check2=which(board)
while check1!=check2:
check1=check2
for i,x,y in check1:
if i==1:
for num in range(x+1):
if board[num][y+1] or board[num][y+2]:
break
else:
answer+=1
board[x][y],board[x+1][y],board[x+1][y+1],board[x+1][y+2]=0,0,0,0
elif i==2:
for num in range(x+1):
if board[num][y-1] or board[num][y-2]:
break
else:
answer+=1
board[x][y],board[x+1][y],board[x+1][y-1],board[x+1][y-2]=0,0,0,0
elif i==3:
for num in range(x+1):
if board[num][y-1] or board[num][y+1]:
break
else:
answer+=1
board[x][y],board[x+1][y],board[x+1][y-1],board[x+1][y+1]=0,0,0,0
elif i==4:
for num in range(x+2):
if board[num][y+1]:
break
else:
answer+=1
board[x][y],board[x+1][y],board[x+2][y],board[x+2][y+1]=0,0,0,0
elif i==5:
for num in range(x+2):
if board[num][y-1]:
break
else:
answer+=1
board[x][y],board[x+1][y],board[x+2][y],board[x+2][y-1]=0,0,0,0
check2=which(board)
return answer
Discussion and feedback