(python) 求优化
查看原帖
(python) 求优化
306754
Master_Hash楼主2020/4/27 23:37

orz

from queue import Queue
q = Queue()
n, m, stx, sty = map(int, input().split())
pos = ((1, 2), (2, 1), (-1, 2), (-2, 1), (1, -2), (2, -1), (-1, -2), (-2, -1))
f = {(i, j):-1 for i in range(1, n + 1) for j in range(1, m + 1)}
q.put((stx, sty, 0))
while not q.empty():
    tmpa = q.get()
    f[tmpa[:-1]] = tmpa[-1]
    for i in pos:
        tmpb = (i[0] + tmpa[0], i[1] + tmpa[1])
        if tmpb in f and not f[tmpb] + 1:
            q.put((tmpb[0], tmpb[1], tmpa[-1] + 1))
#for i in range(1, n + 1): print(''.join(f"{f[(i, j)]:<5d}" for j in range(1, m + 1)))
for i in range(1, n + 1): print(''.join("%-5d"%f[(i, j)] for j in range(1, m + 1)))
2020/4/27 23:37
加载中...