An n * N chess board, some lattices can not put, put the pieces of M,
each of the two pieces cannot be in the same row or column, ask how many kinds of release method
Dfs is too slow, use SCR better dot
Python has only 22 lines, which in fact can be shorter. But it's going to be a very long, very long line.
While true:table = [[0 to J in Range] ' for I in range ()] table[0][0] = 1 boardsize, chessnum = ma P (int, raw_input (). Split ()) if boardsize = = Chessnum = -1:break states = Range (1 << boardsize) cols = Raws = Range (1, boardsize + 1) chessboard = Dict (Zip (raws, [' + raw_input () for I in Raws])) ones = Dict (Zip (states, map (Lambda S:str) (Bin (s)), COUNT (' 1 '), States)))) S:if Ones[state] <= chessnum:table[raw][state] + = Table[raw-1][state] For Col in cols:s = 1 << (COL-1) if chessboard[raw][col] = = ' # ' and state &am P s = = 0:nextstate = state | s table[raw][nextstate] + = table[raw-1][state] Print sum ([Table[boar Dsize][state] in states if ones[state] = = Chessnum])
Dynamic programming of State compression--checkerboard problem POJ 1321