CodeForces 404D Minesweeper 1D (DP)
题意:给定一个序列,*表示雷,1表示它旁边有一个雷,2表示它旁边有两个雷,0表示旁边没有雷,?表示未知,求有多少情况。
析:dp[i][j] 表示第 i 个放 j 状态,有多少种情况,然后很简单的DP就可以搞定。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1000000 + 10;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
LL dp[2][5];
char s[maxn];
/*
0 - 0
1 - *1
2 - 1*
3 - *2*
4 - *
*/ int main(){
cin >> s+1;
n = strlen(s+1);
int cnt = 0;
if(s[1] == '0') dp[cnt][0] = 1;
else if(s[1] == '1') dp[cnt][2] = 1;
else if(s[1] == '*') dp[cnt][4] = 1;
else if(s[1] == '?') dp[cnt][0] = dp[cnt][2] = dp[cnt][4] = 1;
cnt ^= 1; for(int i = 2; i <= n; ++i, cnt ^= 1){
memset(dp[cnt], 0, sizeof dp[cnt]);
if(s[i] == '0')
dp[cnt][0] = (dp[cnt^1][0] + dp[cnt^1][1]) % mod;
else if(s[i] == '1'){
dp[cnt][1] = dp[cnt^1][4];
dp[cnt][2] = (dp[cnt^1][0] + dp[cnt^1][1]) % mod;
}
else if(s[i] == '2')
dp[cnt][3] = dp[cnt^1][4];
else if(s[i] == '*')
dp[cnt][4] = (dp[cnt^1][2] + dp[cnt^1][3] + dp[cnt^1][4]) % mod;
else {
dp[cnt][0] = (dp[cnt^1][0] + dp[cnt^1][1]) % mod;
dp[cnt][1] = dp[cnt^1][4];
dp[cnt][2] = (dp[cnt^1][0] + dp[cnt^1][1]) % mod;
dp[cnt][3] = dp[cnt^1][4];
dp[cnt][4] = (dp[cnt^1][2] + dp[cnt^1][3] + dp[cnt^1][4]) % mod;
}
} LL ans = dp[cnt^1][0] + dp[cnt^1][1] + dp[cnt^1][4];
cout << ans % mod << endl;
return 0;
}
CodeForces 404D Minesweeper 1D (DP)的更多相关文章
- Codeforces 404D Minesweeper 1D
题意: 给定字符串,其中'*'表示地雷,'1'表示左/右边有一个地雷相邻,'2'表示左右两边均有地雷相邻,'0'表示左右均无地雷相邻,'?'表示待定,可填入0,1,2或者地雷,有多少种表示方法使字母串 ...
- [Codeforces 1201D]Treasure Hunting(DP)
[Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列 ...
- 【codeforces 404D】Minesweeper 1D
[题目链接]:http://codeforces.com/problemset/problem/404/D [题意] 让你玩一个1维的扫雷游戏; 游戏的描述由数字0..2以及符号*表示; 分别表示这个 ...
- Codeforces 404D [DP]
/* 我是一个习惯后悔,但是没办法忍受内疚感的二货== 这题是个无脑dp,但是比赛大概20min没出...其实最后5min我好好想想简单化边界条件,可以出的. 题意: 给你一个长度为1e6的由?*01 ...
- codeforces Minesweeper 1D
题意:就是挖地雷,给你一个字符串,‘*’代表地雷,‘1’代表在它的周围有1个地雷,‘2’代表在左右都有个地雷,‘?’代表不确定是不是地雷,可以是1,2,*,问你最后有几种方式确定所有的的地雷. 思路: ...
- Codeforces Global Round 1D(DP,思维)
#include<bits/stdc++.h>using namespace std;int dp[1000007][7][7];int cnt[1000007];int main(){ ...
- codeforces Hill Number 数位dp
http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits: 5000 MS Memory Limits: ...
- codeforces Educational Codeforces Round 16-E(DP)
题目链接:http://codeforces.com/contest/710/problem/E 题意:开始文本为空,可以选择话费时间x输入或删除一个字符,也可以选择复制并粘贴一串字符(即长度变为两倍 ...
- codeforces #round363 div2.C-Vacations (DP)
题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...
随机推荐
- 「长乐集训 2017 Day1」区间 线段树
题目 对于两个区间\((a,b),(c,d)\),若\(c < a < d\)或\(c < b < d\)则可以从\((a,b)\)走到\((c,d)\)去,现在有以下两种操作 ...
- UI及物体渲染顺序
1.决定UI渲染在所有物体前,ZTest Always,Canvas中的RenderMode影响该值. 2.都是ZTest Always 时影响覆盖的因素: 父子及先后关系: 渲染队列: sortin ...
- 学习动态性能表(16)--v$rowcache
学习动态性能表 第16篇--V$ROWCACHE 2007.6.12 本视图显示数据字典缓存(也叫rowcache)的各项统计.每一条记录包含不同类型的数据字典缓存数据统计,注意数据字典缓存有层次差 ...
- Mxgraph使用总结一
一.Mxgraph介绍: mxGraph 是一个 JS 绘图组件适用于需要在网页中设计/编辑 Workflow/BPM流程图.图表.网络图和普通图形的 Web 应用程序.mxgraph 下载包中包括j ...
- set和get的用法
import { Map} from 'immutable'; let a = Map({ select: 'users', filter: Map({ name: 'Cam' }) }) let b ...
- SqlServer 用户和权限操作
use [master] GO --创建用户,Test,密码Test CREATE LOGIN [TestUser] WITH PASSWORD=N'Test', DEFAULT_DATABASE=[ ...
- Contiki学习笔记
http://blog.chinaunix.net/uid-9112803-id-2975824.html
- mina写入数据的过程
mina架构图 写数据.读数据触发点: 写数据: 1.写操作很简单,是调用session的write方法,进行写数据的,写数据的最终结果保存在一个缓存队列里面,等待发送,并把当前session放入f ...
- 忽略‘Chrome正在受到自动软件的控制’的提示语,以及后台静默模式启动。
一.使用Chrome做的时候,会看到浏览器上方出现‘Chrome正在受到自动软件的控制’的提示语, 若想忽略此提示信息,在浏览器配置里加个参数:disable_infobars 代码如下 : # co ...
- $timeout()定时器
非常不幸的一点是,人们似乎常常将AngularJS中的$timeOut()函数看做是一个内置的.无须在意的函数.但是,如果你忘记了$timeOut()的回调函数将会造成非常不好的影响,你可能会因此遇到 ...