UVA10409 ZOJ1667 Die Game【模拟】

232 篇文章 0 订阅
96 篇文章 5 订阅

Life is not easy. Sometimes it is beyond your control. Now, as contestantsof ACM ICPC, you might be just tasting the bitter of life. But don’t worry!Do not look only on the dark side of life, but look also on the bright side.Life may be an enjoyable game of chance, like throwing dice. Do or die!Then, at last, you might be able to find the route to victory.


  This problem comes from a game using a die. By the way, do you knowa die? It has nothing to do with ”death.” A die is a cubic object with sixfaces, each of which represents a different number from one to six and ismarked with the corresponding number of spots. Since it is usually usedin pair, ”a die” is a rarely used word. You might have heard a famous phrase ”the die is cast,” though.

  When a game starts, a die stands still on a flat table. During the game, the die is tumbled in alldirections by the dealer. You will win the game if you can predict the number seen on the top face atthe time when the die stops tumbling.

  Now you are requested to write a program that simulates the rolling of a die. For simplicity, weassume that the die neither slips nor jumps but just rolls on the table in four directions, that is, north,east, south, and west. At the beginning of every game, the dealer puts the die at the center of thetable and adjusts its direction so that the numbers one, two, and three are seen on the top, north, andwest faces, respectively. For the other three faces, we do not explicitly specify anything but tell youthe golden rule: the sum of the numbers on any pair of opposite faces is always seven.

   Your program should accept a sequence of commands, each of which is either “north”, “east”,“south”, or “west”. A “north” command tumbles the die down to north, that is, the top face becomesthe new north, the north becomes the new bottom, and so on. More precisely, the die is rotated aroundits north bottom edge to the north direction and the rotation angle is 90 degrees. Other commands alsotumble the die accordingly to their own directions. Your program should calculate the number finallyshown on the top after performing the commands in the sequence. Note that the table is sufficientlylarge and the die never falls off during the game.

Input

The input consists of one or more command sequences, each of which corresponds to a single game. Thefirst line of a command sequence contains a positive integer, representing the number of the followingcommand lines in the sequence. You may assume that this number is less than or equal to 1024. A linecontaining a zero indicates the end of the input. Each command line includes a command that is oneof ‘north’, ‘east’, ‘south’, and ‘west’. You may assume that no white space occurs in any lines.

Output

For each command sequence, output one line containing solely the number on the top face at the timewhen the game is finished.

Sample Input

1

north

3

north

east

south

0

Sample Output

5

1


问题链接UVA10409 ZOJ1667 Die Game

问题简述:(略)

问题分析

  这个问题应该说是代数中的置换问题,每一个操作可以用一个置换来实现。置换也可以用程序来实现,也可以使用置换数组来实现。

  只有使用置换来实现,才能说用的是数学计算的方法。

  做这个题,还需要知道初始状态,似乎题目中并没有讲清楚。

程序说明

  这个程序采用模拟实现,用函数来实现置换。

题记:(略)

参考链接:(略)


AC的C语言程序如下:

/* UVA10409 ZOJ1667 Die Game */

#include <stdio.h>

#define N 8
char s[N];

int top, bottom, north, south, east, west;

void toNorth()
{
    int temp = south;
    south = bottom;
    bottom = north;
    north = top;
    top = temp;
}

void toSouth()
{
    int temp = top;
    top = north;
    north = bottom;
    bottom = south;
    south = temp;
}

void toEast()
{
    int temp = top;
    top = west;
    west = bottom;
    bottom = east;
    east = temp;
}

void toWest()
{
    int temp = top;
    top = east;
    east = bottom;
    bottom = west;
    west = temp;
}

int main()
{
    int n;

    while(~scanf("%d", &n) && n) {
        top = 1, bottom = 6, north = 2, south = 5, east = 4, west = 3;

        while(n--) {
            scanf("%s", s);

            if(s[0] == 'e')
                toEast();
            else if(s[0] == 'w')
                toWest();
            else if(s[0] == 'n')
                toNorth();
            else if(s[0] == 's')
                toSouth();
        }

        printf("%d\n", top);
    }

    return 0;
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值