"""最後の節の背景:⑤の店舗の図面(暗い地に細い線)と、待機場所から全卓へのロボットの道(最短の道を格子で探す)""" import json, sys from collections import deque L = json.load(open(sys.argv[1]))['layout']; A = json.load(open(sys.argv[2])) W, D = L['outer']['w'], L['outer']['d']; S = 0.1; R = 0.3 nx, ny = int(W / S), int(D / S) block = [[False] * ny for _ in range(nx)] def rect_block(x0, y0, x1, y1, pad): for i in range(max(0, int((x0 - pad) / S)), min(nx, int((x1 + pad) / S) + 1)): for j in range(max(0, int((y0 - pad) / S)), min(ny, int((y1 + pad) / S) + 1)): block[i][j] = True for r in L['rooms']: rect_block(*r['rect'], R) for w in L['walls']: rect_block(*w['rect'], R) for t in L['tables']: rect_block(t['x'], t['y'], t['x'] + t['w'], t['y'] + t['h'], R - 0.05) for i in range(nx): for j in range(ny): if i * S < R or j * S < R or i * S > W - R or j * S > D - R: block[i][j] = True sx, sy = L['start']; s = (int(sx / S), int(sy / S)) prev = {s: None}; q = deque([s]) while q: c = q.popleft() for dx, dy in ((1,0),(-1,0),(0,1),(0,-1),(1,1),(1,-1),(-1,1),(-1,-1)): n = (c[0] + dx, c[1] + dy) if 0 <= n[0] < nx and 0 <= n[1] < ny and not block[n[0]][n[1]] and n not in prev: prev[n] = c; q.append(n) def path(p): g = (round(p[0] / S), round(p[1] / S)); best = min(prev, key=lambda c: (c[0]-g[0])**2 + (c[1]-g[1])**2) out = [] while best: out.append(best); best = prev[best] return out[::-1] def simp(pts): o = [pts[0]] for a, b in zip(pts[1:], pts[2:]): d1 = (a[0]-o[-1][0], a[1]-o[-1][1]); d2 = (b[0]-a[0], b[1]-a[1]) if d1[0]*d2[1] - d1[1]*d2[0] != 0: o.append(a) o.append(pts[-1]); return o Y = lambda y: D - y out = [f'', f'', ''] for r in L['rooms']: x0, y0, x1, y1 = r['rect']; out.append(f'') for w in L['walls']: x0, y0, x1, y1 = w['rect']; out.append(f'') for t in L['tables']: out.append(f'') out.append('') e = L['entrance'] out.append(f'') out.append('') for t in A['tables']: p = simp(path(t['stop'])) out.append('') out.append('') for t in A['tables']: out.append(f'') x0, y0, x1, y1 = L['robot_standby']['rect'] out.append(f'') open(sys.argv[3], 'w').write('\n'.join(out)) print(len(A['tables']), 'routes')