プログラミングのお題スレ Part3
■ このスレッドは過去ログ倉庫に格納されています
0161デフォルトの名無しさん
2014/02/12(水) 23:41:06.71import itertools, copy
def f148(tate, yoko):
area = [[None for y in range(yoko)] for x in range(tate)]
result = dict()
def f148r(area, tate, yoko, n=0):
for x,y in itertools.product(range(yoko), range(tate)):
if not area[y][x]:
for (dx,dy) in [(1,0), (0,1)]:
if x+dx<yoko and y+dy<tate:
if not area[y+dy][x+dx]:
area_copy = copy.deepcopy(area)
area_copy[y][x], area_copy[y+dy][x+dx] = u"→↓"[dy], u"←↑"[dy]
f148r(area_copy, tate, yoko, n+1)
return
elif y != tate-1:
if x == yoko-1 and all(c in u"→←↑" for c in area[y]): return
else:
if x != yoko-1 and all(c in u"←↑↓" for c in [area[i][x] for i in range(len(area))]): return
else:
key = "".join("".join(line) for line in area)
if not result.has_key(key): result[key] = [1, area]
else: result[key][0] += 1
f148r(area, tate, yoko)
print "<result>", len(result.keys())
for key in sorted(result.keys(), lambda x,y: cmp(x[0],y[0]))[-3:]:
n,a = result[key]
print "(%d)" % (n)
for x in a: print "".join(x)
f148(6,7)
■ このスレッドは過去ログ倉庫に格納されています