-
Notifications
You must be signed in to change notification settings - Fork 0
/
12094.py
122 lines (116 loc) · 3.52 KB
/
12094.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
def dfs(n,arr, string2 = ''):
global ans
temp = 0
#print(arr, n,string2)
for i in range(N):
temp = max(temp, max(arr[i]))
if temp > ans:
ans = temp
if n == 10:
return
else:
string = ''
for i in range(N):
for j in range(N):
string += str(arr[i][j])
if string in memo and memo[string] < n:
return
else:
memo[string] = n
logic = 0
next = [[0]* N for _ in range(N)]
for i in range(N):
temp = []
for j in range(N):
if arr[i][j] > 0:
temp.append(arr[i][j])
if len(temp) > 0:
for k in range(1,len(temp)):
if temp[k-1] == temp[k]:
temp[k-1] <<=1
temp[k] = 0
nextpos = 0
for k in temp:
if k:
next[i][nextpos] = k
nextpos += 1
if arr[i] == next[i]:
logic += 1
if logic != N:
dfs(n+1,next, string2 +'1')
logic = 0
next = [[0]* N for _ in range(N)]
for i in range(N):
temp = []
for j in range(N-1, -1, -1):
if arr[i][j] > 0:
temp.append(arr[i][j])
if len(temp) > 0:
for k in range(1,len(temp)):
if temp[k-1] == temp[k]:
temp[k-1] <<= 1
temp[k] = 0
nextpos = N-1
for k in temp:
if k:
next[i][nextpos] = k
nextpos -= 1
if arr[i] == next[i]:
logic += 1
if logic != N:
dfs(n+1,next, string2+'2')
logic = 0
next = [[0]* N for _ in range(N)]
for i in range(N):
temp = []
for j in range(N):
if arr[j][i] > 0:
temp.append(arr[j][i])
if len(temp) > 0:
for k in range(1,len(temp)):
if temp[k-1] == temp[k]:
temp[k-1] <<= 1
temp[k] = 0
nextpos = 0
for k in temp:
if k:
next[nextpos][i] = k
nextpos += 1
for j in range(N):
if arr[j][i] != next[j][i]:
break
else:
logic += 1
if logic != N:
dfs(n+1,next,string2 +'3')
logic = 0
next = [[0]* N for _ in range(N)]
for i in range(N):
temp = []
for j in range(N-1, -1, -1):
if arr[j][i] > 0:
temp.append(arr[j][i])
if len(temp) > 0:
for k in range(1,len(temp)):
if temp[k-1] == temp[k]:
temp[k-1] <<= 1
temp[k] = 0
nextpos = N-1
for k in temp:
if k:
next[nextpos][i] = k
nextpos -= 1
for j in range(N):
if arr[j][i] != next[j][i]:
break
else:
logic += 1
if logic != N:
dfs(n+1,next, string2+'4')
N = int(input())
arr = [list(map(int,input().split())) for _ in range(N)]
memo = {}
ans = 2
dfs(0,arr)
print(ans)
#print(memo)