最短的华容道程序

 1 # -*- coding: gbk -*-
 2 from functools import reduce
 3 from copy import deepcopy
 4 def get_list(n, v): return list(map(lambda _:deepcopy(v), range(n)))
 5 def s(l): return reduce(lambda x,y:x+y, l, '')
 6 E = '  ' # 不知如何输出全角空格。宋体、新宋体、Consolas等是等宽字体。刚知道用GBK编码可以,UTF-8不行。
 7 class Brd:
 8     def __str__(m): return reduce(lambda x,y:s(x)+'\n'+s(y), m.b, '')
 9     def put(m, blks):
10         m.b = get_list(5, get_list(4, E))
11         for b in blks:
12             if b.x < 0 or b.x + b.w > 4 or b.y < 0 or b.y + b.h > 5: return False
13             for y in range(b.y, b.y + b.h):
14                 for x in range(b.x, b.x + b.w):
15                     if m.b[y][x] != E: return False
16                     m.b[y][x] = b.name
17         return True
18 class Blk:
19     def __init__(m, name, x, y, w = 1, h = 1): m.name = name; m.x = x; m.y = y; m.w = w; m.h = h; m.old = []
20     def step(m, dx, dy): m.old.append((m.x, m.y)); m.x += dx; m.y += dy
21     def back(m): (m.x, m.y) = m.old.pop()
22 brd = Brd()
23 blks = [
24     Blk('', 1, 0, 2, 2), # 调试期间可注释掉下面一些块
25     Blk('', 0, 0, 1, 2),
26     Blk('', 3, 0, 1, 2),
27     Blk('', 0, 2, 1, 2),
28     Blk('', 1, 2, 2, 1),
29     Blk('', 3, 2, 1, 2),
30     Blk('', 1, 3), Blk('', 2, 3), Blk('', 0, 4), Blk('', 3, 4)]
31 cc = blks[0]; brd.put(blks); print(brd)
32 input('Press the Enter key to continue.')
33 seen = {}; path = []
34 def search(n): # 找的不是最优解。n供调试用。
35     if cc.y == 3:
36         i = 1
37         for p in path: print(p, i); i = i + 1 # 怀念i++
38         return True
39     s = str(brd)
40     if seen.get(s, False): return False
41     seen[s] = True
42     for (dx, dy) in [[-1,0],[1,0],[0,-1],[0,1]]:
43         for b in blks:
44             b.step(dx, dy)
45             if brd.put(blks):
46                 path.append(str(brd))
47                 if search(n + 1): return True
48                 path.pop()
49             b.back()
50     return False
51 search(0)
52 # 踩过的坑: 1. deepcopy; 2. blk.old没搞成堆栈; 3. seen放错地方

华容道与魔方、独立钻石棋一起被称为“智力游戏界的三个不可思议”。它与七巧板、九连环等中国传统益智玩具还有个代名词叫“中国的难题”。

移动各个棋子使曹操从初始位置移到棋盘最下方逃走。曹操逃出华容道的最大障碍是关羽。

姜长英说“估计它的历史只不过有几十年。从前人的笔记中没有发现有玩具华容道的记载。” 他自己在1943年夏第一次看到这个玩具。林德宽说他在1938年在陕西省城固县的乡下见过小孩玩用纸片做的华容道。

20世纪50年代,许莼舫详细分析了华容道游戏,给出了100步的解法,总结出8条规则,4个要点:
1. 四个小兵必须两两在一起,不要分开;
2. 曹操、关羽、大将移动时前面应有两个小兵开路;
3. 曹操移动时后面应有两个小兵追赶;
4. 以上三种状况,其中各块都可局部任意移动。

经姜长英、许莼舫、藤村幸三郎、清水达雄、马丁·加达纳, Thomas B. Lenann等的努力,游戏解法已减少至81步。

Klotski Acervo Lima

Klotski's first report of occurrence in China is in Shaanxi province, where Lín Dé Kuān, from the Northwestern Polytechnic University, noticed children in a village playing a version of Klotski made with pieces of paper in 1938. The first books on Klotski pattern were written by Chinese professor Jiāng Cháng Yīng of Northwestern Polytechnical University in 1949, in his book 科学消遣. (English translation: Science Passime) This book has been republished as 科学思维锻炼与消遣. ISBN 7-5612-0971-1.

The minimum number of moves for the original puzzle is 81, which is verified by the computer as the absolute minimum for the standard initial layout, if you consider sliding a single piece to any accessible position as a single move. But how to verify it?

For the first published solutions (not optimal solution), currently known is by the Chinese educator Xǔ Chún Fǎng, in his book 數學漫談. (Kāi Minɡ Shū Diàn, March 1952) His solution involves 100 steps.

日本人又乱改三国了!

   

I get it. 开门七件事,柴米油盐酱醋茶。做女人难,做日本女人挺难。书道是为了米饭上撒芝麻用吗?能增加蛋白质吗?

posted @ 2021-11-28 23:44  Fun_with_Words  阅读(140)  评论(0编辑  收藏  举报









 张牌。