LightOJ1151---Snakes and Ladders (高斯消元+概率dp)

‘Snakes and Ladders’ or ‘Shap-Ludu’ is a game commonly played in Bangladesh. The game is so common that it would be tough to find a person who hasn’t played it. But those who haven’t played it (unlucky of course!) the rules are as follows.

  1. There is a 10 x 10 board containing some cells numbered from 1 to 100.

  2. You start at position 1.

  3. Each time you throw a perfect dice containing numbers 1 to 6.

  4. There are some snakes and some ladders in the board. Ladders will take you up from one cell to another. Snakes will take you down.

  5. If you reach a cell that contains the bottom part of a ladder, you will immediately move to the cell which contains the upper side of that ladder. Similarly if you reach a cell that has a snake-head you immediately go down to the cell where the tail of that snake ends.

  6. The board is designed so that from any cell you can jump at most once. (For example there is a snake from 62 to 19, assume that another is from 19 to 2. So, if you reach 62, you will first jump to 19, you will jump to 2. These kinds of cases will not be given)

  7. There is no snake head in the 100-th cell and no ladder (bottom part) in the first cell.

  8. If you reach cell 100, the game ends. But if you have to go outside the board in any time your move will be lost. That means you will not take that move and you have to throw the dice again.

Now given a board, you have to find the expected number of times you need to throw the dice to win the game. The cases will be given such that a result will be found.
Input

Input starts with an integer T (≤ 105), denoting the number of test cases.

The first line of a case is a blank line. The next line gives you an integer n denoting the number of snakes and ladders. Each of the next n lines contain two integers a and b (1 ≤ a, b ≤ 100, a ≠ b). If a < b, it means that there is a ladder which takes you from a to b. If a > b, it means that there is a snake which takes you from a to b. Assume that the given board follows the above restrictions.
Output

For each case of input, print the case number and the expected number of times you need to throw the dice. Errors less than 10-6 will be ignored.
Sample Input

Output for Sample Input

2

14

4 42

9 30

16 8

14 77

32 12

37 58

47 26

48 73

62 19

70 89

71 67

80 98

87 24

96 76

0

Case 1: 31.54880806

Case 2: 33.0476190476

Problem Setter: Jane Alam Jan

dp[i] 表示 在格子i时,结束游戏需要抛掷骰子的次数
当i处有snake或者ladders时
dp[i]=dp[go[i]]
否则
dp[i]= dp[a1]+dp[a2]+...+dp[an]+6cnt
因为题目中的一句话,如果抛骰子然后出界的话,需要重新再抛掷,所以转移到的位置个数不是6个,但是代价一定是6个(除a1->an以外的点被忽略掉,但是骰子还是抛掷了,所以次数还是要加上去)
发现方程中存在环,所以用高斯消元来解方程

/************************************************************************* > File Name: f.cpp > Author: ALex > Mail: [email protected] > Created Time: 2015年04月30日 星期四 20时21分20秒 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-12;
typedef long long LL;
typedef pair <int, int> PLL;

int go[110];
double mat[110][110];
int free_x[110];
double x[110];


int Gauss(int equ, int var) {
    int i, j, k;
    int max_r;
    int col;
    int ta, tb;
    int LCM;
    int temp;
    int free_index, free_num;
    memset(free_x, 1, sizeof(free_x));
    memset(x, 0, sizeof(x));
    for (k = col = 0; k < equ && col < var; ++k, ++col) {
        max_r = k;
        for (int i = k + 1; i < equ; ++i) {
            if (fabs(mat[i][col]) - fabs(mat[max_r][col]) > eps) {
                max_r = i;
            }
        }
        if (max_r != k) {
            for (int j = k; j < var + 1; ++j) {
                swap(mat[max_r][j], mat[k][j]);
            }
        }
        if (fabs(mat[k][col]) <= eps) {
            --k;
            continue;
        }
        for (int i = k + 1; i < equ; ++i) {
            if (fabs(mat[i][col]) <= eps) {
                continue;
            }
            double tmp = mat[i][col] / mat[k][col];
            for (int j = col; j < var + 1; ++j) {
                mat[i][j] -= mat[k][j] * tmp;
            }
        }
    }
    for (int i = k; i < equ; ++i) {
        if (fabs(mat[i][var]) > eps) {
            return 0;
        }
    }
    if (k < var) {
        for (int i = k - 1; i >= 0; --i) {
            free_num = 0;
            for (int j = 0; j < var; ++j) {
                if (fabs(mat[i][j]) > eps && free_x[j]) {
                    ++free_num;
                    free_index = j;
                }
            }
            if (free_num > 1) {
                continue;
            }
            double tmp = mat[i][var];
            for (int j = 0; j < var; ++j) {
                if (j != free_index && fabs(mat[i][j]) > eps) {
                    tmp -= mat[i][j] * x[j];
                }
            }
            free_x[free_index] = 0;
            x[free_index] = tmp / mat[i][free_index];
        }
        return var - k;
    }
    for (int i = var - 1; i >= 0; --i) {
        double tmp = mat[i][var];
        for (int j = i + 1; j < var; ++j) {
            if (fabs(mat[i][j]) > eps) {
                tmp -= x[j] * mat[i][j];
            }
        }
        x[i] = tmp / mat[i][i];
    }
    return 1;
}

int main() {
    int t;
    int icase = 1;
    scanf("%d", &t);
    while (t--) {
        int n;
        for (int i = 1; i <= 100; ++i) {
            go[i] = i;
        }
        scanf("%d", &n);
        int a, b;
        for (int i = 1; i <= n; ++i) {
            scanf("%d%d", &a, &b);
            go[a] = b;
        }
        memset(mat, 0, sizeof(mat));
        mat[100][100] = 1;
        mat[100][101] = 0;
        for (int i = 1; i < 100; ++i) {
            if (go[i] != i) {
                mat[i][i] = 1;
                mat[i][101] = 0;
                mat[i][go[i]] = -1;
            }
            else {
                int cnt = 0;
                for (int j = 1; j <= 6; ++j) {
                    if (i + j <= 100) {
                        ++cnt;
                        mat[i][i + j] = -1;
                    }
                }
                mat[i][i] = cnt;
                mat[i][101] = 6;
            }
        }
        Gauss(101, 101);
        printf("Case %d: %.12f\n", icase++, x[1]);
    }
    return 0;
}

你可能感兴趣的:(dp,高斯消元法)