1. 程式人生 > >Codeforces404D Minesweeper 1D 遞推

Codeforces404D Minesweeper 1D 遞推

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).

題意:
求給出的字串中有多少種正確的答案.

這題自己寫,wa6 , 很鬱悶, 然後去看題解, 發現自己的思路就是錯的.

正確的思路是建立一個dp二維陣列.

dp[i][4]陣列表示的是:

dp[i][0]: i位置不是bomb, i+1位置不是bomb

dp[i][1]: i位置不是bomb, i+1位置是bomb

dp[i][2]: i位置是bomb, i+1位置不是bomb

dp[i][3]: i位置是bomb,i+1位置是bomb

然後根據遞推關係, 有如下式子:

if(s[i]=='0')
            dp[i][0]=dp[i-1][0]%mod;

if(s[i]=='1')
            dp[i][0]=dp[i-1][2]%mod;
            dp[i][1]=dp[i-1][0]%mod;

if(s[i]=='2')
            dp[i][1]=dp[i-1][2]%mod;
if(s[i]=='*')
            dp[i][2]=(dp[i-1][1]+dp[i-1][3])%mod;
            dp[i][3]=(dp[i-1][1]+dp[i-1][3])%mod;
if(s[i]=='?')
            dp[i][0]=(dp[i-1][0]+dp[i-1][2])%mod;
            dp[i][1]=(dp[i-1][0]+dp[i-1][2])%mod;
            dp[i][2]=(dp[i-1][1]+dp[i-1][3])%mod;
            dp[i][3]=(dp[i-1][1]+dp[i-1][3])%mod;

當等於len時,需要對其單獨處理.

程式碼如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn=1000005;
const long long mod=1000000007;
typedef long long ll;
char s[maxn];
ll dp[maxn][4];
ll ans;
void init()
{
    dp[0][0]=dp[0][1]=1;
}
int main()
{
    scanf("%s",s+1);
    int len=strlen(s+1);
    init();
    for (int i=1;i<len;i++)
    {
        if(s[i]=='0')
        {
            dp[i][0]=dp[i-1][0]%mod;
        }
        else if(s[i]=='1')
        {
            dp[i][0]=dp[i-1][2]%mod;
            dp[i][1]=dp[i-1][0]%mod;
        }
        else if(s[i]=='2')
        {
            dp[i][1]=dp[i-1][2]%mod;
        }
        else if(s[i]=='*')
        {
            dp[i][2]=(dp[i-1][1]+dp[i-1][3])%mod;
            dp[i][3]=(dp[i-1][1]+dp[i-1][3])%mod;
        }
        else
        {
            dp[i][0]=(dp[i-1][0]+dp[i-1][2])%mod;
            dp[i][1]=(dp[i-1][0]+dp[i-1][2])%mod;
            dp[i][2]=(dp[i-1][1]+dp[i-1][3])%mod;
            dp[i][3]=(dp[i-1][1]+dp[i-1][3])%mod;
        }
    }
    if(s[len]=='0')
    {
        ans=dp[len-1][0];
    }
    else if(s[len]=='1')
    {
        ans=dp[len-1][2];
    }
    else if(s[len]=='2')
    {
        ans=0;
    }
    else if(s[len]=='*')
    {
        ans=(dp[len-1][1]+dp[len-1][3])%mod;
    }
    else
    {
        ans=(dp[len-1][0]+dp[len-1][1]+dp[len-1][2]+dp[len-1][3])%mod;
    }
    printf("%lld\n",ans);
    return 0;
}