Python 2 3,141-15 = 126
def win(x,y):w([y]*x)
w=lambda b,f=print:not[f(r+1,c+1)for r,p in enumerate(b)for c in range(p)if(r+c)*w(b[:r]+[min(i,c)for i in b[r:]],max)]
蛮力minimax搜索。对于每一个可能的举动,我们都会递归地查看对手做出该举动后是否可以获胜。打的很弱;别人应该可以做得更好。感觉就像是APL的工作。
win
是公共接口。它采用了板子的尺寸,将其转换为板子表示,并将其传递给w
。
w
是minimax算法。它处于棋盘状态,尝试所有移动,构建一个列表,该列表的元素与获胜移动相对应,如果列表为空,则返回True。使用默认设置f=print
,建立列表具有打印获胜举动的副作用。该函数名称在返回获胜动作列表时更有意义,但后来我将not
了列表的前面以节省空间。
for r,p in enumerate(b)for c in xrange(p) if(r+c)
:遍历所有可能的动作。1 1
被视为不合法的举动,从而简化了基本案例。
b[:r]+[min(i,c)for i in b[r:]]
:在坐标r
和表示的移动之后构造板的状态c
。
w(b[:r]+[min(i,c)for i in b[r:]],max)
:递归查看新状态是否为丢失状态。max
是我能找到的最短的函数,它将接受两个整数参数并且不会抱怨。
f(r+1,c+1)
:如果f
正在打印,则打印移动。不管f
是什么,它都会产生一个值来填充列表长度。
not [...]
:not
返回True
空列表和False
非空列表。
原始的Python 2代码,完全不用关注,包括用于处理更大输入的备注:
def win(x, y):
for row, column in _win(Board([y]*x)):
print row+1, column+1
class MemoDict(dict):
def __init__(self, func):
self.memofunc = func
def __missing__(self, key):
self[key] = retval = self.memofunc(key)
return retval
def memoize(func):
return MemoDict(func).__getitem__
def _normalize(state):
state = tuple(state)
if 0 in state:
state = state[:state.index(0)]
return state
class Board(object):
def __init__(self, state):
self.state = _normalize(state)
def __eq__(self, other):
if not isinstance(other, Board):
return NotImplemented
return self.state == other.state
def __hash__(self):
return hash(self.state)
def after(self, move):
row, column = move
newstate = list(self.state)
for i in xrange(row, len(newstate)):
newstate[i] = min(newstate[i], column)
return Board(newstate)
def moves(self):
for row, pieces in enumerate(self.state):
for column in xrange(pieces):
if (row, column) != (0, 0):
yield row, column
def lost(self):
return self.state == (1,)
@memoize
def _win(board):
return [move for move in board.moves() if not _win(board.after(move))]
演示:
>>> for i in xrange(7, 11):
... for j in xrange(7, 11):
... print 'Dimensions: {} by {}'.format(i, j)
... win(i, j)
...
Dimensions: 7 by 7
2 2
Dimensions: 7 by 8
3 3
Dimensions: 7 by 9
3 4
Dimensions: 7 by 10
2 3
Dimensions: 8 by 7
3 3
Dimensions: 8 by 8
2 2
Dimensions: 8 by 9
6 7
Dimensions: 8 by 10
4 9
5 6
Dimensions: 9 by 7
4 3
Dimensions: 9 by 8
7 6
Dimensions: 9 by 9
2 2
Dimensions: 9 by 10
7 8
9 5
Dimensions: 10 by 7
3 2
Dimensions: 10 by 8
6 5
9 4
Dimensions: 10 by 9
5 9
8 7
Dimensions: 10 by 10
2 2