zoukankan      html  css  js  c++  java
  • LightOJ 1065 Island of Survival (概率DP?)

    题意:有 t 只老虎,d只鹿,还有一个人,每天都要有两个生物碰面,
    1.老虎和老虎碰面,两只老虎就会同归于尽 
    2.老虎和人碰面或者和鹿碰面,老虎都会吃掉对方 
    3.人和鹿碰面,人可以选择杀或者不杀该鹿
    4.鹿和鹿碰面,没事
    问人存活下来的概率

    析:最后存活肯定是老虎没了,首先可以用概率dp来解决,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 = 1e3 + 10;
    const int mod = 1e9 + 7;
    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;
    }
    
    double dp[maxn][maxn];
    
    int main(){
      int T;  cin >> T;
      for(int kase = 1; kase <= T; ++kase){
        scanf("%d %d", &n, &m);
        memset(dp, 0, sizeof dp);
        dp[n][m] = 1.0;
        for(int i = n; i; --i)
          for(int j = m; j >= 0; --j){
            double sum = i*(i-1)/2 + i*j + i;
            if(i >= 2)  dp[i-2][j] += dp[i][j]*i*(i-1)/2.0/sum;
            if(i > 0 && j > 0)  dp[i][j-1] += dp[i][j]*i*j/sum;
          }
        double ans = 0.0;
        for(int i = 0; i <= m; ++i)  ans += dp[0][i];
        printf("Case %d: %.10f
    ", kase, ans);
      }
      return 0;
    }
    

      

    #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 = 1e3 + 10;
    const int mod = 1e9 + 7;
    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;
    }
    
    double solve(int x){
      if(x & 1)  return 0.0;
      double ans = 1.0;
      while(x){
        ans *= (x-1.0) / (x+1.0);
        x -= 2;
      }
      return ans;
    }
    
    int main(){
      int T;  cin >> T;
      for(int kase = 1; kase <= T; ++kase){
        scanf("%d %d", &n, &m);
        printf("Case %d: %.10f
    ", kase, solve(n));
      }
      return 0;
    }
    

      

  • 相关阅读:
    【Gerrit】Gerrit与Jenkins/Hudson CI服务器搭建
    【Gerrit】Gerrit cmd query (gerrit命令行查询change信息)
    【python】jiraAPI使用教程 自动创建jira问题单并置状态为OPEN
    【Jenkins】jenkins简单搭建并执行任务
    【python】Redis介绍及简单使用
    【python】PIL 批量绘制图片矩形框工具
    【Flask】Flask快速玩框架
    C# split 几种使用方法
    40个有用的jQuery技术和教程
    jQuery性能优化
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7326992.html
Copyright © 2011-2022 走看看