24h購物| | PChome| 登入
2013-06-29 21:55:38| 人氣1,189| 回應0 | 上一篇 | 下一篇

[UVA] 11459 - Snakes and Ladders

推薦 0 收藏 0 轉貼0 訂閱站台

Problem D: Snakes and Ladders

Snakes and Ladders is a board game played on a 10 by 10 grid. The squares of the grid are numbered 1 to 100. Each player has a token which is placed on the square numbered 1 at the beginning of the game. Players take turns rolling a die which provides a random number between 1 and 6. After rolling, the player advances his or her token the number of squares shown on the die. If this would put the token past the square numbered 100, the token is advanced to 100. After advancing, if the token is on a square containing the bottom of a ladder, the token must be moved to the square containing the top of the ladder. Similarly, if the token is on a square containing the mouth of a snake, the token must be moved to the square containing the tail of the snake. No square contains more than one endpoint of any snake or ladder. The square numbered 100 does not contain the mouth of a snake or the bottom of a ladder. A player wins when his or her token reached the square numbered 100. At that point, the game ends.

Given the configuration of the snakes and ladders on a game board and a sequence of die rolls, you are to determine the positions of all the tokens on the game board. The sequence of die rolls need not be complete (i.e. it need not lead to any player winning). The sequence of die rolls may also continue after the game is over; in this case, the die rolls after the end of the game should be ignored.

Input Specification

The first line is the number of test cases to follow. The test cases follow, one after another; the format of each test case is the following:

The first line contains three positive integers: the number a of players, the number b of snakes or ladders, and the number c of die rolls. There will be no more than 1000000 players and no more than 1000000 die rolls. Each of the next b lines contains two integers specifying a snake or ladder. The first integer indicates the square containing the mouth of the snake or the bottom of the ladder. The second integer indicates the square containing the tail of the snake or the top of the ladder. The following c lines each contain one integer giving number rolled on the die.

Sample Input

1
3 1 3
4 20
3
4
5

Output Specification

For each player, output a single line containing a string of the form Position of player N is P., where N is replaced with the number of the player and P is replaced with the final position of the player.

Output for Sample Input

Position of player 1 is 20.
Position of player 2 is 5.
Position of player 3 is 6.

Ondřej Lhoták

題目描述:

蛇梯遊戲, 為了這題還特地去玩了一下, 否則還真的有點難理解, 遇到梯子可以跳格, 遇到蛇則會退格, 有點像是賞罰一樣, 然後擲骰子前進, 如果超過 100 算 100, 而當其中一名宣告勝利時, 遊戲變會結束。
已經知道骰子依序會骰出什麼, 模擬過程, 輸出最後停留的位置。

題目解法:


有可能會有多餘的骰子剩餘。

#include <stdio.h>
int pos[1000005];
inline int readchar() {
    const int N = 1048576;
    static char buf[N];
    static char *p = buf, *end = buf;
    if(p == end) {
        if((end = buf + fread(buf, 1, N, stdin)) == buf) return EOF;
        p = buf;
    }
    return *p++;
}
inline int ReadInt(int *x) {
    static char c, neg;
    while((c = readchar()) < '-')    {if(c == EOF) return 0;}
    neg = (c == '-') ? -1 : 1;
    *x = (neg == 1) ? c-'0' : 0;
    while((c = readchar()) >= '0')
        *x = (*x << 3) + (*x << 1) + c-'0';
    *x *= neg;
    return 1;
}
int main() {
    /*freopen("in.txt", "r+t", stdin);
    freopen("out.txt", "w+t", stdout);*/
    int testcase;
    int n, m, q, i;
    ReadInt(&testcase);
    //scanf("%d", &testcase);
    while(testcase--) {// n players
        //scanf("%d %d %d", &n, &q, &m);
        ReadInt(&n);
        ReadInt(&q);
        ReadInt(&m);
        int x, y, link[105] = {};
        for(i = 1; i <= n; i++)
            pos[i] = 1;
        while(q--) {
            //scanf("%d %d", &x, &y);
            ReadInt(&x);
            ReadInt(&y);
            link[x] = y;
        }
        int turn = 1, roll, winner = 0;
        while(m--) {
            ReadInt(&roll);
            //scanf("%d", &roll);
            pos[turn] += roll;
            if(pos[turn] >= 100)
                pos[turn] = 100;
            if(link[pos[turn]])
                pos[turn] = link[pos[turn]];
            if(pos[turn] >= 100)
                pos[turn] = 100, winner = 1;
            turn++;
            if(turn > n)    turn = 1;
            if(winner) {
                while(m--)
                    ReadInt(&roll);
                    //scanf("%d", &roll);
                break;
            }
        }
        for(i = 1; i <= n; i++)
            printf("Position of player %d is %d.\n", i, pos[i]);
    }
    return 0;
}

台長: Morris
人氣(1,189) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][最短路] 11492 - Babel
此分類上一篇:[UVA] 11308 - Bankrupt Baker

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文