码迷,mamicode.com
首页 > 其他好文 > 详细

LightOJ 1065 Island of Survival (概率DP?)

时间:2017-08-09 19:14:40      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:stdout   天都   algo   include   val   --   link   logs   mem   

题意:有 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?)

标签:stdout   天都   algo   include   val   --   link   logs   mem   

原文地址:http://www.cnblogs.com/dwtfukgv/p/7326992.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!