# Dynamic programming of State compression--checkerboard problem POJ 1321

Source: Internet
Author: User

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

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.