-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2010_r1c_pc.py
130 lines (111 loc) · 3.32 KB
/
2010_r1c_pc.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
123
124
125
126
127
128
129
130
'''
Created on 2014-3-22
@author: ezonghu
'''
def analysisHex(H):
filters = [8,4,2,1]
bits = []
for f in filters:
if H & f != 0:
bits.append(1)
else:
bits.append(-1)
return bits
def Squares(Bark):
Rows = len(Bark)
squares = []
for r in Bark:
squares.append([])
for _c in r:
squares[-1].append(0)
for rid in range(Rows):
r = Bark[rid]
for cid in range(len(r)):
if rid == 0 and Bark[rid][cid] != 0:
squares[rid][cid]= 1
continue
if cid == 0 and Bark[rid][cid] != 0:
squares[rid][cid]= 1
continue
if (Bark[rid][cid] * Bark[rid-1][cid-1] == 1 and
Bark[rid][cid] * Bark[rid][cid-1] == -1 and
Bark[rid][cid] * Bark[rid-1][cid]) == -1 :
squares[rid][cid] = 1 + min( squares[rid-1][cid-1],
squares[rid-1][cid],
squares[rid][cid-1] )
elif Bark[rid][cid] == 0:
squares[rid][cid] = 0
else:
squares[rid][cid] = 1
return squares
def checkSquares(max_size, Squares, s_rid, s_cid):
for rid, cid in Squares:
deltar=abs(s_rid-rid)
deltac=abs(s_cid-cid)
if deltar>=max_size or deltac>=max_size:
continue
else:
return False
return True
def getLargestSquares(squares):
sizes = {}
for rid in range(len(squares)):
for cid in range(len(squares[rid])):
sz = squares[rid][cid]
if sz not in sizes:
sizes[sz] = [[rid,cid]]
continue
sizes[sz].append([rid,cid])
max_size = max(sizes.keys())
max_squares = sizes[max_size]
# print max_squares
OkSquares = []
for rid,cid in max_squares:
if checkSquares(max_size, OkSquares, rid, cid):
OkSquares.append([rid,cid])
return max_size, OkSquares
def updateRows(size, squares, rows):
for rid, cid in squares:
for r in range(rid-size+1, rid+1):
for c in range(cid-size+1, cid+1):
rows[r][c]=0
# printRows(rows)
return rows
def printRows(rows):
for r in rows:
print(r)
f = open('C-large-practice.in')
first_line = f.readline()
Cases = int(first_line)
CaseNo = 0
for l in f:
CaseNo += 1
[M, N] = [int(i) for i in l.split()]
total_grid = M * N
# print M,N
rows = []
for r in f:
M -= 1
tmp = []
for c in r.strip():
tmp.extend(analysisHex(int(c, 16)))
rows.append(tmp)
if M == 0:
break
res = []
def checkRows(rows):
for r in rows:
for c in r:
if c !=0:
return True
return False
while sum([sz*sz*num for sz, num in res]) < total_grid:
s, sqs = getLargestSquares(Squares(rows))
updateRows(s,sqs, rows)
res.append([s, len(sqs)])
print('Case #%d: %d' % (CaseNo, len(res)))
for sz, num in res:
print(sz,num)
if CaseNo==Cases:
break
f.close()