登录  /  注册

codeforces Round #237(div2) D解题报告

php中文网
发布: 2016-06-07 15:18:53
原创
1016人浏览过

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 square

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.


题目大意:

        扫雷游戏的一维版(也就是说在一条线上,不是常玩的矩阵类型)

        每个格子有几种情况:

                '?'  表示还没有填充

               '0'  表示左右两边都没有雷

               '1'  表示两边有某个地方有雷

               '2'  表示两边都有雷

               '*'  表示该地方就是一个雷

        求出有多少种不同的填充'?'的方案


解法:

       本题有递推关系,即i位置的放置情况,跟前一个雷有关.然而有部分不怎么符合严格递推,那就是'1'这个情况,有可能是左边有雷,也有可能是右边有雷.那我们就把'1'这种情况拆开来,即左边有雷和右边有雷.然而就可以写出递推关系式:

               0表示两边没有雷;

               1表示右边有雷;

               2表示左边有雷;

               3表示两边都有雷;

               4表示当前位置有雷;

               dp[i][0] = dp[i-1][0] + dp[i-1][2];

               dp[i][1] = dp[i-1][0] + dp[i-1][2];

               dp[i][2] = dp[i-1][4];

               dp[i][3] = dp[i-1][4];

               dp[i][4] = dp[i-1][3] + dp[i-1][4] + dp[i-1][1];

               递推前置条件: dp[0][0] = 1, dp[0][2] = 1;

       最后输出最有一个格子的所有总方案数,由于是最后一个,所以不存在1右边有雷和3两边有雷的情况.


做题的时候,由于判断情况较多(多了一个'?'),写的很乱,网上看到某位写的代码比较简练而且巧妙,所以贴他的代码好了

#include <cstdio>
#include <cstring>

using namespace std;

const int INF = 1e9+7;


int n;
int dp[1000010][5];
char st[1000010];

void init() {
	scanf("%s",st+1);
	n = strlen(st+1);
}

void add(int &amp;x, int y) {
	x += y;
	if (x &gt;= INF) x -= INF;
}

void solve() {
	dp[0][0] = dp[0][1] = 1;

	for (int i = 1; i <br><br></cstring></cstdio>
登录后复制
智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号