题目大意:
就是现在有N个人标号从1到N排队, 每次队首的4个人开始游戏, 4个人中每个人胜利的可能性是相等的, 现在胜利的人会留在队首, 继续游戏, 输的人会回到队尾(回到队尾的顺序与进入游戏idea顺序一致, 比如说1,2,3,4比赛, 2获胜, 则2留在队首, 1,3,4在队尾的顺序依旧是1,3,4(4在最后)现在如果某个人连续赢了M局游戏, 那个人就是最终得获胜者, 问长度为N的队伍当中, 初始位置在第K个的人获胜的概率 (K <= N <= 10, M <= 10)
大致思路:
首先很明显的是队伍长度不变, 且胜率和当前队首的人已经连续赢了多少把有关, 那么用dp[i][j]表示当队首的人已经连续赢了i次时, 在队列第j个位置的人获胜的概率
那么久很容易找到状态转移方程, 这样得到一个最多110个未知数的方程组, 用Gauss消元求解即可
具体状态转移方程见代码注释部分
代码如下:
Result : Accepted Memory : 1264 KB Time : 0 ms
/*
* Author: Gatevin
* Created Time: 2014/12/26 20:27:35
* File Name: Sora_Kasugano.cpp
*/
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;
double a[110][110];//矩阵
double x[110];//解
int equ, var;//方程个数, 变量个数
/*
* 用dp[i][j]表示当前队首的玩家连续赢了i局时, 站在队列第j个位置的人获胜的概率
* 那么首先有dp[M][1] = 1, dp[M][2~N] = 0;
* 当i < M时
* 对于j > 4 dp[i][j] = dp[i + 1][j - 3]*0.25 + dp[1][j - 3]*0.75;
* 对于j == 4 dp[i][j] = dp[i + 1][N]*0.25 + dp[1][N]*0.5 + dp[1][1]*0.25;
* 对于j == 3 dp[i][j] = dp[i + 1][N - 1]*0.25 + dp[1][N - 1]*0.25 + dp[1][1]*0.25 + dp[1][N]*0.25;
* 对于j == 2 dp[i][j] = dp[i + 1][N - 2]*0.25 + dp[1][1]*0.25 + dp[1][N - 1]*0.5;
* 对于j == 1 dp[i][j] = dp[i + 1][1]*0.25 + dp[1][N - 2]*0.75;
* 那么dp[0][K]即为最终的解
*/
int Gauss()
{
for(int row = 0, col = 0; row < equ && col < var; row++, col++)
{
int max_r = row;
for(int i = row + 1; i < equ; i++)
if(fabs(a[i][col]) > fabs(a[max_r][col]))
max_r = i;
if(fabs(a[max_r][col]) < eps) return 0;
if(max_r != row)
{
for(int i = col; i < var; i++) swap(a[max_r][i], a[row][i]);
swap(x[max_r], x[row]);
}
for(int i = col + 1; i < var; i++) a[row][i] /= a[row][col];
x[row] /= a[row][col];
a[row][col] = 1;
for(int i = 0; i < equ; i++)
if(i != row)
{
for(int j = col + 1; j < var; j++) a[i][j] -= a[i][col]*a[row][j];
x[i] -= x[row]*a[i][col];
a[i][col] = 0;
}
}
return 1;
}
int N, M, K;
int hash[11][11];//将dp[i][j]映射为第hash[i][j]个变量, 解存储在x[hash[i][j]]中
void check(int tx, int ty)
{
if(hash[tx][ty] == -1)
hash[tx][ty] = var++;
return;
}
int main()
{
int t;
scanf("%d", &t);
for(int cas = 1; cas <= t; cas++)
{
scanf("%d %d %d", &N, &M, &K);
memset(hash, -1, sizeof(hash));
memset(a, 0, sizeof(a));
equ = 0;
var = 0;
for(int i = 0; i <= M; i++)
for(int j = 1; j <= N; j++)
{
check(i, j);
a[equ][hash[i][j]] = 1;
if(j > 4)
{
if(i != M)
{
check(i + 1, j - 3); check(1, j - 3);
a[equ][hash[i + 1][j - 3]] += -0.25;
a[equ][hash[1][j - 3]] += -0.75;
x[equ] = 0;
}
else
x[equ] = 0;
}
else if(j == 4)
{
if(i != M)
{
check(i + 1, N); check(1, N); check(1, 1);
a[equ][hash[i + 1][N]] += -0.25;
a[equ][hash[1][N]] += -0.5;
a[equ][hash[1][1]] += -0.25;
x[equ] = 0;
}
else
x[equ] = 0;
}
else if(j == 3)
{
if(i != M)
{
check(1, 1); check(i + 1, N - 1); check(1, N); check(1, N - 1);
a[equ][hash[1][1]] += -0.25;
a[equ][hash[i + 1][N - 1]] += -0.25;
a[equ][hash[1][N]] += -0.25;
a[equ][hash[1][N - 1]] += -0.25;
x[equ] = 0;
}
else
x[equ] = 0;
}
else if(j == 2)
{
if(i != M)
{
check(i + 1, N - 2); check(1, 1); check(1, N - 1);
a[equ][hash[i + 1][N - 2]] += -0.25;
a[equ][hash[1][1]] += -0.25;
a[equ][hash[1][N - 1]] += -0.5;
x[equ] = 0;
}
else
x[equ] = 0;
}
else if(j == 1)
{
if(i != M)
{
check(i + 1, 1); check(1, N - 2);
a[equ][hash[i + 1][1]] += -0.25;
a[equ][hash[1][N - 2]] += -0.75;
x[equ] = 0;
}
else
x[equ] = 1;
}
equ++;
}
Gauss();
printf("Case #%d: %.6f\n", cas, x[hash[0][K]]);
}
return 0;
}