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 ...
随机推荐
- 欧拉函数 and 大数欧拉 (初步)
前两天总结了素数筛法,其中就有Eular筛法.现在他又来了→→ φ(n),一般被称为欧拉函数.其定义为:小于n的正整数中与n互质的数的个数. 毕竟是伟大的数学家,所以以他名字命名的东西很多辣. 对于φ ...
- ZOJ Anagrams by Stack(堆栈中的搜索)
个人心得:算法书中的第一个例题就来了一个下马威,虽然题意很好理解但是做起来确实这么不顺手,所以自己对于搜索和堆栈理解的并不是很好, 以前也是很多这样的题目无法实施,这题要做的很明确就是输出正确的能依靠 ...
- swing之gridlayout
package gui1; import java.awt.FlowLayout; import java.awt.GridLayout; import javax.swing.JButton; im ...
- markdown的学习
开始 语法 编辑器 sublime配置 图床 体验 开始 昨天晚上加上今天上午,折腾了算是一天的markdown编辑器. 原因是,为了写博客.在博客园写的东西,想法不到简书里,结果发现有部分乱码,以及 ...
- 洛谷 3803 【模板】多项式乘法(FFT)
题目:https://www.luogu.org/problemnew/show/P3803 第一道FFT! https://www.cnblogs.com/zwfymqz/p/8244902.htm ...
- ExtJs中获得当前选中行号(Grid中多选或者是单选)及Grid的反选(取消选中行)
多选,如何获得每行的行号: function getlineNum(){ var sm=titleGird.getSelectionModel(); // 获得grid的SelectionMod ...
- sourcetree 安装与操作
sourcetree操作 http://www.jianshu.com/p/be9f0484af9d SourceTree安装教程和GitLab配置详解 http://www.cnblogs.com/ ...
- Spring Boot整合Rabbitmq
Spring Boot应用中整合RabbitMQ,并实现一个简单的发送.接收消息的例子来对RabbitMQ有一个直观的感受和理解. 在Spring Boot中整合RabbitMQ是一件非常容易的事,因 ...
- 第八课 go的条件语句
1 if ... else package main import "fmt" func main() { flag:= { fmt.Println("flag > ...
- 第九章 Servlet工作原理解析(待续)
从 Servlet容器说起 创建 Servlet实例 Servlet体系结构 Servlet如何工作 Servlet中的Listener Filter如何工作 Servlet中的url-pattern