状态压缩 dp

                                无论是状态压缩dp还是普通的dp状态的设计都是至关重要的。

                                 关于这方面的知识可以参考ZW大神的论文!

C. Petya and Spiders
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Petya loves training spiders. Petya has a board n × m in size. Each cell of the board initially has a spider sitting on it. After one second Petya chooses a certain action for each spider, and all of them humbly perform its commands. There are 5 possible commands: to stay idle or to move from current cell to some of the four side-neighboring cells (that is, one command for each of the four possible directions). Petya gives the commands so that no spider leaves the field. It is allowed for spiders to pass through each other when they crawl towards each other in opposite directions. All spiders crawl simultaneously and several spiders may end up in one cell. Petya wants to know the maximum possible number of spider-free cells after one second.

Input

The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 40, n·m ≤ 40) — the board sizes.

Output

In the first line print the maximum number of cells without spiders.

Sample test(s)
input
1 1
output
0
input
2 3
output
4
Note

In the first sample the only possible answer is:

s

In the second sample one of the possible solutions is:

rdl
rul

s denotes command "stay idle", l, r, d, u denote commands "crawl left", "crawl right", "crawl down", "crawl up", correspondingly.



关于这题可以用状态压缩的思想来解决,考虑到n * m <= 40, 则Min(n, m) <= 6, 则一行的覆盖情况可以用二进制位来表示,显然改行的
蜘蛛情况受到上一行以及下一行的影响。故可以设计dp[n][state1][state2]来表示当前考虑到第n行,状态为state1, 下一行状态为state2
dp[n][st1][st2] = max(dp[n-1][st3][st1] + num(st1)


D. Minesweeper 1D
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Game "Minesweeper 1D" is played on a line of squares, the line's height is 1 square, the line's width is n squares. Some of the squares contain bombs. If a square doesn't contain a bomb, then it contains a number from 0 to 2 — the total number of bombs in adjacent squares.

For example, the correct field to play looks like that: 001*2***101*. The cells that are marked with "*" contain bombs. Note that on the correct field the numbers represent the number of bombs in adjacent cells. For example, field 2* is not correct, because cell with value 2 must have two adjacent cells with bombs.

Valera wants to make a correct field to play "Minesweeper 1D". He has already painted a squared field with width of n cells, put several bombs on the field and wrote numbers into some cells. Now he wonders how many ways to fill the remaining cells with bombs and numbers are there if we should get a correct field in the end.

Input

The first line contains sequence of characters without spaces s1s2... sn (1 ≤ n ≤ 106), containing only characters "*", "?" and digits "0", "1" or "2". If character si equals "*", then the i-th cell of the field contains a bomb. If character si equals "?", then Valera hasn't yet decided what to put in the i-th cell. Character si, that is equal to a digit, represents the digit written in the i-th square.

Output

Print a single integer — the number of ways Valera can fill the empty cells and get a correct field.

As the answer can be rather large, print it modulo 1000000007 (109 + 7).

Sample test(s)
input
?01???
output
4
input
?
output
2
input
**12
output
0
input
1
output
0
Note

In the first test sample you can get the following correct fields: 001**1001***001*2*001*10.




比赛时该题没出,现在来补题。。。。

先考虑一个简单情况。如果全是?????????????
一共有多少种情况了

很显然组合数学的方法无法解决。。。

 
这里采用dp的思想和方法。当前第i位状态受到第i-1位状态影响,比如第i位为0,第i-1位也只能为0 或者1
但是1的话可能是*1 或者 1 * ,故一共可设计5个状态, dp[n][i], 表示考虑到第n位状态是i的方案数
则dp[n][i]可以由dp[n-1][i]转换过来....

很显然之前染色的结果并不影响dp。。。。








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值