codeforces Round #237(div2) D解题报告
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**1, 001***, 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 &x, int y) { x += y; if (x >= INF) x -= INF; } void solve() { dp[0][0] = dp[0][1] = 1; for (int i = 1; i <br> <br> </cstring></cstdio>

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











이 기사는 MySQL의 Alter Table 문을 사용하여 열 추가/드롭 테이블/열 변경 및 열 데이터 유형 변경을 포함하여 테이블을 수정하는 것에 대해 설명합니다.

기사는 인증서 생성 및 확인을 포함하여 MySQL에 대한 SSL/TLS 암호화 구성에 대해 설명합니다. 주요 문제는 자체 서명 인증서의 보안 영향을 사용하는 것입니다. [문자 수 : 159]

기사는 MySQL에서 파티셔닝, 샤딩, 인덱싱 및 쿼리 최적화를 포함하여 대규모 데이터 세트를 처리하기위한 전략에 대해 설명합니다.

기사는 MySQL Workbench 및 Phpmyadmin과 같은 인기있는 MySQL GUI 도구에 대해 논의하여 초보자 및 고급 사용자를위한 기능과 적합성을 비교합니다. [159 자].

이 기사에서는 Drop Table 문을 사용하여 MySQL에서 테이블을 떨어 뜨리는 것에 대해 설명하여 예방 조치와 위험을 강조합니다. 백업 없이는 행동이 돌이킬 수 없으며 복구 방법 및 잠재적 생산 환경 위험을 상세하게합니다.

InnoDB의 전체 텍스트 검색 기능은 매우 강력하여 데이터베이스 쿼리 효율성과 대량의 텍스트 데이터를 처리 할 수있는 능력을 크게 향상시킬 수 있습니다. 1) InnoDB는 기본 및 고급 검색 쿼리를 지원하는 역 색인화를 통해 전체 텍스트 검색을 구현합니다. 2) 매치 및 키워드를 사용하여 검색, 부울 모드 및 문구 검색을 지원합니다. 3) 최적화 방법에는 워드 세분화 기술 사용, 인덱스의 주기적 재건 및 캐시 크기 조정, 성능과 정확도를 향상시키는 것이 포함됩니다.

기사는 외국 열쇠를 사용하여 데이터베이스의 관계를 나타내고 모범 사례, 데이터 무결성 및 피할 수있는 일반적인 함정에 중점을 둡니다.

이 기사에서는 PostgreSQL, MySQL 및 MongoDB와 같은 다양한 데이터베이스에서 JSON 열에서 인덱스를 작성하여 쿼리 성능을 향상시킵니다. 특정 JSON 경로를 인덱싱하는 구문 및 이점을 설명하고 지원되는 데이터베이스 시스템을 나열합니다.
