Minesweeper 1D CodeForces - 404D (dp)

本文介绍了一款一维扫雷游戏的算法实现,玩家在一个由方格组成的一维线上进行游戏,部分方格含有地雷。文章详细解析了如何通过动态规划的方法计算填充剩余方格的所有合法方式的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 ≤ 10^6), 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 (10^9 + 7).

Example
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**1, 001***, 001*2*, 001*10.

大致题意:有若干个连续的格子,每个格子里有这么几种符号”*”, “0”, “1” ,”2”,”?”
“*”表示当前格子里埋着地雷,“0”表示左右两个相连的格子里都没有地雷,“1”表示左右两个相连的格子里有一个有地雷,“2”表示左右两个相连的格子里有两个地雷,“?”表示你可以选择前面的任意一个。
问你有多少种合法的方式。

思路:很巧妙的dp,dp[i][j]表示位置i上的状态为j时有多少种合法方案,具体看下面代码。

代码如下

#include <iostream> 
#include <cstring> 
#include <cstdio>  
using namespace std;
const int maxn=1e6+5;
const int mod=1e9+7;
long long int dp[maxn][5];//5种状态
/*
0 str[i]为“0”
1 str[i]为“1” 前一个是地雷,后一个不是 
2 str[i]为“1” 前一个不是地雷,后一个是 
3 str[i]为“*” 地雷
4 两个地雷,即str[i]为“2” 
*/
char str[maxn];
int main()
{
    memset(dp,0,sizeof(dp));
    scanf("%s",&str);
    int len=strlen(str);
    if(str[0]=='0') dp[0][0]=1;
    else if(str[0]=='1')    dp[0][2]=1;
    else if(str[0]=='*')    dp[0][3]=1;
    else if(str[0]=='?')
    {
        dp[0][0]=1;
        dp[0][2]=1;
        dp[0][3]=1;
    }
    for(int i=1;i<len;i++)
    {
        if(str[i]=='0')
            dp[i][0]=(dp[i-1][0]+dp[i-1][1])%mod; 
        else if(str[i]=='1')
        {
            dp[i][1]=dp[i-1][3];
            dp[i][2]=(dp[i-1][0]+dp[i-1][1])%mod;
        }
        else if(str[i]=='2')
            dp[i][4]=dp[i-1][3];
        else if(str[i]=='*')
            dp[i][3]=(dp[i-1][2]+dp[i-1][4]+dp[i-1][3])%mod;
        else if(str[i]=='?')
        {
            dp[i][0]=(dp[i-1][0]+dp[i-1][1])%mod; 
            dp[i][1]=dp[i-1][3];
            dp[i][2]=(dp[i-1][0]+dp[i-1][1])%mod;
            dp[i][4]=dp[i-1][3];
            dp[i][3]=(dp[i-1][2]+dp[i-1][4]+dp[i-1][3])%mod;
        }
    }
    len--;
    printf("%lld\n",(dp[len][0]+dp[len][1]+dp[len][3])%mod);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值