LightOJ 1065 Island of Survival (概率DP?)

Posted dwtfukgv

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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\n", 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\n", kase, solve(n));
  }
  return 0;
}

  

以上是关于LightOJ 1065 Island of Survival (概率DP?)的主要内容,如果未能解决你的问题,请参考以下文章

LightOJ - 1265 Island of Survival 期望

LightOJ 1268 Island of Survival 数学神解/不用写那些guapi概率dp

LightOJ 1418 Trees on My Island (Pick定理)

LightOj1418 - Trees on My Island(Pick定理)

LeetCode Max Area of Island

Max Area of Island