*문제 출처는 프로그래머스에 있습니다.
문제 제목: 자물쇠와 열쇠 - 3단계
문제 사이트: https://school.programmers.co.kr/learn/courses/30/lessons/60059
- 자물쇠와 열쇠
고고학자인 "튜브"는 고대 유적지에서 보물과 유적이 가득할 것으로 추정되는 비밀의 문을 발견하였습니다. 그런데 문을 열려고 살펴보니 특이한 형태의 자물쇠로 잠겨 있었고 문 앞에는 특이한 형태의 열쇠와 함께 자물쇠를 푸는 방법에 대해 다음과 같이 설명해 주는 종이가 발견되었습니다.
잠겨있는 자물쇠는 격자 한 칸의 크기가 1 x 1인 N x N 크기의 정사각 격자 형태이고 특이한 모양의 열쇠는 M x M 크기인 정사각 격자 형태로 되어 있습니다.
자물쇠에는 홈이 파여 있고 열쇠 또한 홈과 돌기 부분이 있습니다. 열쇠는 회전과 이동이 가능하며 열쇠의 돌기 부분을 자물쇠의 홈 부분에 딱 맞게 채우면 자물쇠가 열리게 되는 구조입니다. 자물쇠 영역을 벗어난 부분에 있는 열쇠의 홈과 돌기는 자물쇠를 여는 데 영향을 주지 않지만, 자물쇠 영역 내에서는 열쇠의 돌기 부분과 자물쇠의 홈 부분이 정확히 일치해야 하며 열쇠의 돌기와 자물쇠의 돌기가 만나서는 안됩니다. 또한 자물쇠의 모든 홈을 채워 비어있는 곳이 없어야 자물쇠를 열 수 있습니다.
열쇠를 나타내는 2차원 배열 key와 자물쇠를 나타내는 2차원 배열 lock이 매개변수로 주어질 때, 열쇠로 자물쇠를 열수 있으면 true를, 열 수 없으면 false를 return 하도록 solution 함수를 완성해주세요.
제한사항
- key는 M x M(3 ≤ M ≤ 20, M은 자연수)크기 2차원 배열입니다.
- lock은 N x N(3 ≤ N ≤ 20, N은 자연수)크기 2차원 배열입니다.
- M은 항상 N 이하입니다.
- key와 lock의 원소는 0 또는 1로 이루어져 있습니다.
- 0은 홈 부분, 1은 돌기 부분을 나타냅니다.
입출력 예
keylockresult[[0, 0, 0], [1, 0, 0], [0, 1, 1]] | [[1, 1, 1], [1, 1, 0], [1, 0, 1]] | true |
입출력 예에 대한 설명
key를 시계 방향으로 90도 회전하고, 오른쪽으로 한 칸, 아래로 한 칸 이동하면 lock의 홈 부분을 정확히 모두 채울 수 있습니다.
나의 풀이
- 시계방향으로 90도씩 회전하는 코드
total_rot = []
for _ in range(4):
rotate = []
for i in range(len(key)):
stack = []
for j in range(len(key)):
stack.append(key[j][i])
rotate.append(stack[::-1])
total_rot.append(rotate)
key = total_rot[-1]
for i in total_rot:
for j in i:
print(j)
print()
보드를 확장해서 전체 탐색하는 코드
sp = N-M + 1 # start row point
while sp < N*N-M + 1 :
for sc in range(N-M+1,(N*N)-M + 1):
print(sp,sc) # start column point
tmp_board = [[0 for i in range(N*N)] for _ in range(N*N)]
for i in range(M):
for j in range(M):
tmp_board[sp+i][sc+j] += key[i][j]
for i in tmp_board:
print(i)
print()
sp += 1
1 1
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 1, 1, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
1 2
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 1, 1, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
1 3
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 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0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 1, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 1, 1, 0]
6 6
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 1, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 1, 1]
확장한 보드 가운데에 자물쇠를 나타낸 코드
new_board = [[0 for i in range(N*N)] for _ in range(N*N)]
for i in range(N):
for j in range(N):
new_board[i+N][j+N] += lock[i][j]
가운데에 자물쇠를 두고 열쇠가 이동하면서 탐색하는 코드
def solution(key,lock):
answer = False
total_rot = []
for _ in range(4):
rotate = []
for i in range(len(key)):
stack = []
for j in range(len(key)):
stack.append(key[j][i])
rotate.append(stack[::-1])
total_rot.append(rotate)
key = total_rot[-1]
N = len(lock)
M = len(key)
zero_board = [[0 for i in range(N*N)] for _ in range(N*N)]
new_board = [[0 for i in range(N*N)] for _ in range(N*N)]
for i in range(N):
for j in range(N):
new_board[i+N][j+N] += lock[i][j]
sp = N-M + 1 # start row point
while sp < N*N-M + 1 :
for tmp_key in total_rot:
for sc in range(N-M+1,(N*N)-M + 1):
# print(sp,sc) # start column point
tmp_board = [[0 for i in range(N*N)] for _ in range(N*N)]
for i in range(N):
for j in range(N):
tmp_board[i+N][j+N] += lock[i][j]
for i in range(M):
for j in range(M):
tmp_board[sp+i][sc+j] += tmp_key[i][j]
result = True
for i in range(N):
for j in tmp_board[N:2*N]:
for k in j[N:2*N]:
if k != 1:
result = False
if result == True:
return True
sp += 1
return answer
key = [[0, 0, 0], [1, 0, 0], [0, 1, 1]]
lock = [[1, 1, 1], [1, 1, 0], [1, 0, 1]]
total_rot = []
for _ in range(4):
rotate = []
for i in range(len(key)):
stack = []
for j in range(len(key)):
stack.append(key[j][i])
rotate.append(stack[::-1])
total_rot.append(rotate)
key = total_rot[-1]
for i in total_rot:
for j in i:
print(j)
print()
for idx in range(len(total_rot)):
# 행 기준으로 0인 행이 있는지 검사
if sum(total_rot[idx][0]) == 0:
print(idx)
total_rot[idx] = total_rot[idx][1:]
if sum(total_rot[idx][-1]) == 0:
total_rot[idx] = total_rot[idx][:-1]
# 컬럼을 기준으로 0안 행이 있는지 검사
r_cal_stack = 0
l_cal_stack = 0
for i in range(len(total_rot[idx])):
if total_rot[idx][i][0] == 0:
r_cal_stack += 1
if total_rot[idx][i][-1] == 0:
l_cal_stack += 1
if r_cal_stack == len(total_rot[idx]):
for i in total_rot[idx]:
i.pop(0)
if l_cal_stack == len(total_rot[idx]):
for i in total_rot[idx]:
i.pop()
print()
for i in total_rot:
for j in i:
print(j)
print()
나의 풀이 - 2번째 풀이 (넘파이 사용, 83점 ,시간초과)
import numpy as np
def solution(key,lock):
answer = False
N = len(lock)
M = len(key)
key = np.array(key)
lock = np.array(lock)
new_board = np.zeros(shape = (N*N,N*N), dtype = int)
total_rot = np.array([key,np.rot90(key,k = -1),np.rot90(key,k = -3),np.rot90(key,k = -2)])
new_board[N:2*N,N:2*N] += lock
for tmp_key in total_rot:
sp = N-M + 1 # start row point
while sp < N*N-M + 1 :
for sc in range(N-M+1,(N*N)-M + 1):
tmp_board = new_board.copy()
tmp_board[sp:sp+M,sc:sc+M] += tmp_key
if np.all(tmp_board[N:2*N,N:2*N] == 1):
return True
sp += 1
return answer
세번째 풀이 (92점 / 25,27,31,32 시간초과)
import numpy as np
import sys
sys.stdin.readline()
def solution(key,lock):
answer = False
N = len(lock)
M = len(key)
key = np.array(key)
lock = np.array(lock)
new_board = np.zeros(shape = (N*N,N*N), dtype = int)
total_rot = np.array([np.rot90(key,k = -2),key,np.rot90(key,k = -3),np.rot90(key,k = -1)])
new_board[N:2*N,N:2*N] += lock
sp = N-M + 1 # start row point
while sp < N*N-M + 1 :
for sc in range(N-M+1,(N*N)-M + 1):
for tmp_key in total_rot:
tmp_board = new_board.copy()
tmp_board[sp:sp+M,sc:sc+M] += tmp_key
if np.all(tmp_board[N:2*N,N:2*N] == 1):
return True
sp += 1
return answer
최종 코드 (자물쇠의 크기 문제였다. N*N으로 하면 너무 커져서 시간초과가 났던 것...)
보드 판을 N*3 으로 해주고 문제를 풀었다
드디어 해결했다 ㅜㅡㅜ
import numpy as np
import sys
sys.stdin.readline()
def shape_key(key): # 열쇠 모양을 정사각형 모양으로 줄여나감
lp = True
rp = True
while lp or rp:
if np.all(key[:,0] == 0) and np.all(key[0,:] == 0):
key = np.delete(key, 0 , axis = 0)
key = np.delete(key, 0 , axis = 1)
else:
lp = False
if np.all(key[:,-1] == 0) and np.all(key[-1,:] == 0):
key = np.delete(key, -1 , axis = 0)
key = np.delete(key, -1 , axis = 1)
else:
rp = False
return key
def solution(key,lock):
answer = False
key = np.array(key)
lock = np.array(lock)
if np.all(key[:,:] == 0):
return False
key = shape_key(key)
N = len(lock)
M = len(key)
lock_start = N
for i in range(N):
if np.any(lock[i]==0):
lock_start += i
break
new_board = np.zeros(shape = (N*3,N*3), dtype = int)
total_rot = np.array([key,np.rot90(key,k = 1),np.rot90(key,k = 2),np.rot90(key,k = 3)])
new_board[N:2*N,N:2*N] += lock
sp = lock_start- M + 1
while sp < N*3-M + 1 :
for tmp_key in total_rot:
for sc in range(N-M+1,(N*3)-M + 1):
tmp_board = new_board.copy()
tmp_board[sp:sp+M,sc:sc+M] += tmp_key
if np.all(tmp_board[N:2*N,N:2*N] == 1):
return True
sp += 1
return answer
※ 알아야 할 것
- 자물쇠를 맞추면서 푸는 문제
'coding test - python > Programmers' 카테고리의 다른 글
Programmers / 귤 고르기 / Python 파이썬 (0) | 2023.03.13 |
---|---|
Programmers / 선입선출스케줄링 / Python 파이썬 (0) | 2023.03.08 |
Programmers / 양과 늑대 -DFS / Python 파이썬 (0) | 2023.02.27 |
Programmers / [1차] 셔틀버스 / Python 파이썬 (0) | 2023.02.07 |
Programmers / [카카오 인턴] 경주로 건설 - dp / Python 파이썬 (0) | 2023.02.06 |