题解:UVA10409 Die Game

· · 题解

题解

UVA10409

题目传送门\small\texttt{(Luogu)}

题目传送门\small\texttt{(UVaOJ)}

人话描述。

骰子每次朝一个方向滚动时,各个面的数字会像“接力赛”一样交换位置。比如:

你的目标:记录骰子每次翻滚后的状态,最终揭晓顶面的数字!

解题思路:

骰子的“数字舞蹈”

骰子的每次翻滚都是一次“数字舞蹈”,我们只需记录每个面的数字如何传递。想象骰子的六个面是六个朋友,每次滚动时他们交换位置:

  1. 向北滚:
  1. 向东滚:

Tips:每次滚动只需更新个相关面的位置,其他面保持不动!

代码实现:让骰子“动”起来。

#include<iostream>
#include<string>
using namespace std;

int main()
{
    int n;
    while(cin >> n && n != 0)
    { // 直到输入0才结束
        // 骰子的六个面:顶、底、北、南、西、东
        int top = 1, bottom = 6, north = 2, south = 5, west = 3, east = 4;

        for(int i = 0; i < n; ++i)
        {
            string cmd;
            cin >> cmd;

            if(cmd == "north")
            {
                // 北滚:顶←南,南←底,底←北,北←顶
                int new_top = south;
                south = bottom;
                bottom = north;
                north = top;
                top = new_top;
            }
            else if(cmd == "south")
            {
                // 南滚:顶←北,北←底,底←南,南←顶
                int new_top = north;
                north = bottom;
                bottom = south;
                south = top;
                top = new_top;
            }
            else if(cmd == "east")
            {
                // 东滚:顶←西,西←底,底←东,东←顶
                int new_top = west;
                west = bottom;
                bottom = east;
                east = top;
                top = new_top;
            }
            else if(cmd == "west")
            {
                // 西滚:顶←东,东←底,底←西,西←顶
                int new_top = east;
                east = bottom;
                bottom = west;
                west = top;
                top = new_top;
            }
        }
        cout << top << endl; // 揭晓顶面数字!
    }
    return 0;
}

AC Recode