プログラミングのお題スレ Part5
■ このスレッドは過去ログ倉庫に格納されています
0116デフォルトの名無しさん
2014/09/30(火) 01:07:03.81ID:C909vV3Kあんまり検証してないけど・・・
def ant_walk(sec):
x, y, direct, map, msg = 0, 0, 0, {}, None
move_dict = [(0, -1), (1, 0), (0, 1), (-1, 0)]
for i in xrange(sec):
if (x, y) in map:
del map[(x, y)]
direct = (direct + 3) % 4
else:
map[(x, y)] = True
direct = (direct + 1) % 4
move = move_dict[direct]
x, y = x + move[0], y + move[1]
if x == 0 and y == 0:
msg = '移動なし'
if y != 0:
msg = '{}へ{}歩'.format('北' if y < 0 else '南', abs(y))
if x != 0:
msg = (msg + '、') if msg is not None else ''
msg += '{}へ{}歩'.format('西' if x < 0 else '東', abs(x))
print(msg)
ant_walk(0): 移動なし
ant_walk(1): 東へ1歩
ant_walk(4): 移動なし
ant_walk(10): 南へ1歩、西へ1歩
ant_walk(100): 北へ2歩
■ このスレッドは過去ログ倉庫に格納されています