zoukankan      html  css  js  c++  java
  • 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;
    }
    

      

  • 相关阅读:
    Linux磁盘分区、格式化和挂载
    通过Fiddler监控Java应用发送请求及相应数据
    使用Apache Commons IO组件读取大文件
    export default 和 export 的使用方式
    vue+webpack4 脚手架搭建
    async/await中reject的问题
    NodeJS 开发博客(五) 使用express脚手架
    常考JS题笔记
    让你弄懂 call、apply、bind的应用和区别
    JS闭包解析
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7117221.html
Copyright © 2011-2022 走看看