CodeForces 404D Minesweeper 1D (DP)

Posted dwtfukgv

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)的主要内容,如果未能解决你的问题,请参考以下文章

计数dp cf404d

计数dp cf404d

计数dp cf404d

《游戏学习》Java版仿windows扫雷小游戏源码

《游戏学习》Java版仿windows扫雷小游戏源码

Win32 Loadimage 生成错误 1812 和 1813