- 論壇徽章:
- 4
|
再貼個答案。- import random, time
- L = 100
- N = 500
- D = 6
- R = D//2
- def getpoint(L, N, D):
- allpoint = [(i, j) for i in range(R, L-R+1) for j in range(R, L-R+1)]
- goodpoint = []
- count = 0
-
- while len(goodpoint) < N:
- try:
- samplepoint = random.sample(allpoint, N - len(goodpoint))
- except ValueError:
- if allpoint == []:
- print('Max point number is {} for this time.'.format(len(goodpoint)))
- break
- else:
- samplepoint = allpoint.copy()
- allpoint.clear()
- for p in samplepoint:
- if p in allpoint:
- goodpoint.append(p)
- x, y = p
- for i in range(max(R, x-D), min(L-R+1, x+D+1)):
- for j in range(max(R, y-D), min(L-R+1, y+D+1)):
- if (i-x)**2 + (j-y)**2 < D**2 and (i, j) in allpoint:
- allpoint.remove((i, j))
- goodpoint.sort()
- for i in range(len(goodpoint)):
- print(i, goodpoint[i][0], goodpoint[i][1])
- start = time.clock()
- getpoint(L, N, D)
- end = time.clock()
- print(end - start)
復(fù)制代碼 |
|