Sicily 2610/1897. Chutter and Ladder

2610. Chutter and Ladder

Constraints

Time Limit: 1 secs, Memory Limit: 256 MB

Description

Here is a simpler version of the interesting game “Chutter and Ladder”:
 Given a sequence of small grids, numbered from 0 to n-1. As the player of this game, your starting position is grid 0, and your destination is grid n-1. Each time you can drop a dice and move 1-6 steps forward according to the number on top of the dice. As the dice is perfect, each number from 1 to 6 can evenly show on top of the dice.
 However, each grid i is marked with an integer a[i], that means if you move into that grid, then you will immediately jump |a[i]| grids more (if a[i] > 0, the direction is forward, otherwise it’s backward). For example, suppose grid 5 is marked with integer 3, -3 respectively, then when you come to grid 5, you will immediately move to grid 8 or 2 correspondingly. Of course, if a[i] is 0, then you will stay at grid i. It’s guaranteed that you will always jump into a grid marked with 0.
 What’s worse, the sequence is cyclic, that means when you are moving to a grid k which is out of the range of 0 and n-1, you will actually move to grid k%n.

 Now, given the configuration of the grids, please write a program to calculate the expected number of moves to reach grid n-1.

Input

 Input may contain several test cases. The first lines is a positive integer, T, (1<=T<=20), the number of test cases below. Each test cases starts with a positive integer n, (1<=n<=100), the number of grids in the game, followed by n integers a[i] (-n<=a[i]<=n, 1<=i<=n). It’s guaranteed that grid 0 and grid n-1 is always marked with 0.

Output

For each test case, please output the expected number of moves to reach grid n-1, round to 3 digits after the decimal point. If it’s impossible to reach grid n-1, simply output “-1”.

Sample Input

2
3
0 0 0
8
0 -1 -2 -3 -4 -5 -6 0

Sample Output

3.000
-1

Problem Source

系列热身赛2@2011年上半学期算法考试和4+2选拔赛

// Problem#: 2610
// Submission#: 3593228
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
#include <string.h>
#include <math.h>

const int MAXN = 101;
const double EPS = 1e-9;

int n, jump[MAXN];
double a[MAXN][MAXN], b[MAXN], x[MAXN];

int GaussEimination() {
    int i, j, k, p, q[MAXN];
    double max, l;
    memset(q, 0, sizeof(q));
    for (k = 1; k <= n; k++) {
        p = 0;
        max = 0;
        for (i = 1; i <= n; i++)
            if (!q[i] && max + EPS < fabs(a[i][k])) max = fabs(a[p = i][k]);
        if (!p) return 0;
        else q[p] = 1;
        for (i = 1; i <= n; i++)
            if (i != p) {
                l = a[i][k] / a[p][k];
                for (j = 1; j <= n; j++) a[i][j] -= l * a[p][j];
                b[i] -= l * b[p];
            }
    }
    for (i = 1; i <= n; i++)
        for (j = 1; j <= n; j++)
            if (fabs(a[i][j]) > EPS) x[j] = b[i] / a[i][j];
    return 1;
}

void modeling() {
    int i, j;
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    memset(x, 0, sizeof(x));
    for (i = 0; i < n - 1; i++) {
        for (j = 1; j <= 6; j++) {
            int k = (i + j) % n;
            k = ((k + jump[k]) % n + n) % n;
            a[i + 1][k + 1] += -1.0 / 6;
        }
        a[i + 1][i + 1] += 1.0;
        b[i + 1] = 1.0;
    }
    a[n][n] = 1.0;
}

int main() {
    int tn, i;
    scanf("%d", &tn);
    while (tn--) {
        scanf("%d", &n);
        for (i = 0; i < n; i++) scanf("%d", jump + i);
        modeling();
        if (!GaussEimination()) printf("-1\n");
        else printf("%.3lf\n", x[1]);
    }
    return 0;
}                                 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值