-
Notifications
You must be signed in to change notification settings - Fork 0
/
자물쇠와 열쇠.py
72 lines (66 loc) · 2.2 KB
/
자물쇠와 열쇠.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
시작 시간: 2022년 4월 13일 오후 12시 50분
소요 시간: 40분
풀이 방법:
너무 효율적으로만 풀려고 했다,,
하지만 배열의 크기가 충분히 작기 때문에, 완전탐색을 해도 무리 없음..
"""
def get_core_area(area, core):
top, right, left, bottom = 20, 0, 20, 0
for i in range(len(area)):
for j in range(len(area)):
if area[i][j] == core:
if i < top:
top = i
if i > bottom:
bottom = i
if j < left:
left = j
if j > right:
right = j
core_area = []
for i in range(top, bottom + 1):
row = []
for j in range(left, right+1):
row.append(area[i][j])
core_area.append(row)
return core_area
def rotate(key):
# 왼쪽으로 90도
rotated_key = []
for i in range(len(key[0])-1, -1, -1):
row = []
for j in range(len(key)):
row.append(key[j][i])
rotated_key.append(row)
return rotated_key
def is_okay(lock_core, key_core):
if len(lock_core) != len(key_core):
return False
if len(lock_core[0]) != len(key_core[0]):
return False
for i in range(len(lock_core)):
for j in range(len(key_core)):
if lock_core[i] + lock_core[j] > 1:
return False
return True
def solution(key, lock):
key_bump_area, lock_engrave_area = get_core_area(key, 1), get_core_area(lock, 0)
print(">>>>>>>>>lock")
for i in range(len(lock_engrave_area)):
for j in range(len(lock_engrave_area[0])):
print(lock_engrave_area[i][j], end=" ")
print()
print(">>>>>>>>>key")
for i in range(len(key_bump_area)):
for j in range(len(key_bump_area[0])):
print(key_bump_area[i][j], end=" ")
print()
if is_okay(lock_engrave_area, key_bump_area):
return True
for _ in range(3):
key_bump_area = rotate(key_bump_area)
if is_okay(lock_engrave_area, key_bump_area):
return True
return False
solution([[0, 0, 0], [1, 0, 0], [0, 1, 1]], [[1, 1, 1], [1, 1, 0], [1, 0, 1]])