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 ...
随机推荐
- hdu 1937 Finding Seats
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- Eclipse之Web工程探究以及格式化
1. 关于部署 只要配置了Web Deployment Assembly,可以不需要手工拷贝引用jar到/WEB-INF/lib里面了,之前失败是因为引用工程的output路径有问题导致的,修改完成后 ...
- facebook注册不了无法打开官网的解决办法
上周有一个朋友问到我一个问题,问怎么facebook注册不了,facebook官网也无法打开?这个问题不知道有没有人遇到过,以前这个问题也困扰了我挺长时间的,其实想想也挺简单的,由于facebook, ...
- Salesforce注册教程
打开网址 https://developer.salesforce.com/,点击右上角的Sign up(注册) 在页面输入基本信息 >Name:First(姓名) Last(姓) >E ...
- Python 算法之冒泡排序
冒泡排序 冒泡排序算法的原理如下:(从后往前) 1.比较相邻的元素.如果第一个比第二个大,就交换他们两个. 2.对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对.在这一点,最后的元素应该会是 ...
- dockerfile http_php
FROM centos6.6-php5.5:0.0.1 MAINTAINER syberos:wangmo RUN mv /etc/php.ini /etc/php.ini.bak COPY ./ph ...
- WPF中DataGrid控件的过滤(Filter)性能分析及优化
DataGrid控件是一个列表控件, 可以进行过滤,排序等.本文主要针对DataGrid的过滤功能进行分析, 并提供优化方案. 1)DataGrid的过滤过程: 用户输入过滤条件 ...
- 差分IO标准
差分标准 和单端IO不同的是,差分电平使用两根信号线来传达信号,这两根信号线在传输过程中如果遇到同样的噪声源(共模噪声)干扰,在接收端,这样的共模噪声会在两个信号相减时消除,这样并不会给接收电平造成影 ...
- AJAX,jQuery Ajax和Deferred
AJAX全称为“Asynchronous JavaScript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用,改善用户体验,实现无刷新效果的技术. 使用AJAX的优 ...
- java代码写个进程条
总结:运用JProgressBar类.还有线程相关 package com.v; import java.awt.image.*; import java.awt.Color; import java ...