p.325
def rotated(a):
n=len(a)
m=len(a[0])
result=[[0]*n for _ in range(m)]
for i in range(n):
for j in range(m):
result[j][n-i-1]=a[i][j]
return result
def check(new_lock):
for i in range(len(new_lock)//3, len(new_lock)//3*2):
for j in range(len(new_lock)//3, len(new_lock)//3*2):
if new_lock[i][j]!=1:
return False
return True
def solution(key, lock):
key_len=len(key)
lock_len=len(lock)
# 자물쇠의 크기를 3배로
original_new_lock=[[0]*lock_len*3 for _ in range(lock_len*3)]
# 새로운 자물쇠 가운데에 원래 자물쇠 정보 넣기
for i in range(lock_len):
for j in range(lock_len):
original_new_lock[lock_len+i][lock_len+j]=lock[i][j]
# 구현에서 사용할 new_lock
new_lock=original_new_lock.copy()
for a in range(lock_len*2):
for b in range(lock_len*2): # 키를 자물쇠 전체에 꽂아보기
for rotation in range(4): #4방향으로 돌려보기
for i in range(key_len):
for j in range(key_len):
new_lock[a+i][b+j]+=key[i][j]
result=check(new_lock)
if result==True:
return True
else:
for i in range(key_len):
for j in range(key_len):
new_lock[a+i][b+j]-=key[i][j]
key=rotated(key)
return False
key=[[0, 0, 0], [1, 0, 0], [0, 1, 1]]
lock=[[1, 1, 1], [1, 1, 0], [1, 0, 1]]
result=solution(key,lock)
print(result)
댓글