-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisiqi.py
173 lines (128 loc) · 4.02 KB
/
lisiqi.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
RList = [150, 150, 60]
MAXDISTANCE = 1000
IM = np.zeros(0)
disB = 1.3
def ReadFile():
global IM
IM = mpimg.imread('pict.png')
im = []
for i in IM:
t = []
for j in i:
if np.sum(j) == 4:
t.append(0)
else:
t.append(1)
im.append(t)
IM = im
IM = np.asarray(IM)
print(IM.shape)
def calc_dis(Veca, Vecb):
return np.linalg.norm(Veca - Vecb) * disB
def limit(x, y):
s = IM.shape
xl = s[0]
yl = s[1]
if x < 0 or x >= xl: return False
if y < 0 or y >= yl: return False
return True
def updateIM(xList: list, yList: list, rList: list):
global IM
im = []
for i in range(3):
im.append(np.zeros(IM.shape))
for index, x in enumerate(xList):
y = yList[index]
print(index)
for tr in rList[index]:
r = RList[tr]
if not IM[x][y] == 1:
continue
for ty in range(y - r, y + r):
for tx in range(x - r, x + r):
c1 = np.asarray([tx, ty], dtype=np.float_)
c2 = np.asarray([x, y], dtype=np.float_)
if not limit(tx, ty):
continue
if calc_dis(c1, c2) <= r and IM[tx][ty] == 1:
im[tr][tx][ty] = 1
return im
def setCover(xList,yList):
ansList = []
for r in RList:
stations = {}
needed = set()
finial = set()
for index,x in enumerate(xList):
y = yList[index]
connect = []
for ty in range(y - r, y + r):
for tx in range(x - r, x + r):
c1 = np.asarray([tx, ty], dtype=np.float_)
c2 = np.asarray([x, y], dtype=np.float_)
if calc_dis(c1, c2) <= r and IM[tx][ty] == 1 and limit(tx, ty):
connect.append((tx,ty))
needed.add((tx,ty))
stations[(x,y)] = set(connect)
while needed:
best = None
covered = set()
station_covered = set()
for station,states in stations.items():
covered = needed & states
if len(covered) > len(station_covered):
best = station
station_covered = covered
needed -= station_covered
finial.add(best)
ansList.append(finial)
return ansList
def calc_area(xList: list, yList: list,rList = []):
if len(rList) < 1:
for i in xList:
rList.append([0,1,2])
im = updateIM(xList, yList, rList)
sum = 0
for k in range(3):
for i in im[k]:
for j in i:
sum += j
return sum
def mkPic(xList, yList, im):
tm = np.asarray(IM, dtype=np.float_)
for ix, x in enumerate(tm):
for iy, y in enumerate(x):
sum = 0
for k in range(3):
sum += im[k][ix][iy] * 0.2
tm[ix][iy] -= sum
for index, x in enumerate(xList):
y = yList[index]
tm[x][y] = 1
plt.imshow(tm, cmap=plt.cm.gray)
plt.show()
def finial(xList,yList):
result = setCover(xList,yList)
rList = []
for i in xList:
rList.append([])
for index,x in enumerate(xList):
y = yList[index]
for j in range(3):
tset = result[j]
if (x,y) in tset:
rList[index].append(j)
tim = updateIM(xList,yList,rList)
print('Area = ' + str(calc_area(xList,yList,rList)))
for index in range(len(xList)):
x = xList[index]
y = yList[index]
r = rList[index]
print(x,y,r)
ans = calc_area(xList,yList,rList)
mkPic(xList,yList,tim)
if __name__ == '__main__':
ReadFile()