Die Game

Life is not easy. Sometimes it is beyond your control. Now, as contestants of 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 know a die? It has nothing to do with "death." A die is a cubic object with six faces, each of which represents a different number from one to six and is marked with the corresponding number of spots. Since it is usually used in 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 all directions by the dealer. You will win the game if you can predict the number seen on the top face at the time when the die stops tumbling.

Now you are requested to write a program that simulates the rolling of a die. For simplicity, we assume 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 the table and adjusts its direction so that the numbers one, two, and three are seen on the top, north, and west faces, respectively. For the other three faces, we do not explicitly specify anything but tell you the 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 becomes the new north, the north becomes the new bottom, and so on. More precisely, the die is rotated around its north bottom edge to the north direction and the rotation angle is 90 degrees. Other commands also tumble the die accordingly to their own directions. Your program should calculate the number finally shown on the top after performing the commands in the sequence. Note that the table is sufficiently large 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. The first line of a command sequence contains a positive integer, representing the number of the following command lines in the sequence. You may assume that this number is less than or equal to 1024. A line containing a zero indicates the end of the input. Each command line includes a command that is one of 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 time when the game is finished.

Sample Input

1

north

3

north

east

south

0

Output for the Sample Input

5

1


0

0

1

0

0

0

2

0

1

3

6

4

0

0

5

0

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Dice

{

    class Program

    {

        static void Main(string[] args)

        {

            int[,] dice = {

                              {0, 0, 1, 0},

                              {0, 0, 2, 0},

                              {1, 3, 6, 4},

                              {0, 0, 5, 0}

                          };

            RotateNorth(dice);

            RotateWest(dice);

            RotateWest(dice);

            Console.WriteLine("Topmost element is " + dice[2, 0]);

        }

        public static void RotateNorth(int[,] dice)

        {

            int temp = dice[3, 2];

            for (int i = 3; i >= 1; i–)

            {

                dice[i, 2] = dice[i – 1, 2];

            }

            dice[0, 2] = temp;

            dice[2, 0] = dice[0, 2];

        }

        public static void RotateSouth(int[,] dice)

        {

            int temp = dice[0, 2];

            for (int i = 0; i <= 2; i++)

            {

                dice[i, 2] = dice[i + 1, 2];

            }

            dice[3, 2] = temp;

            dice[2, 0] = dice[0, 2];

        }

        public static void RotateEast(int[,] dice)

        {

            int temp = dice[2, 0];

            for (int j = 0; j <= 2; j++)

            {

                dice[2, j] = dice[2, j + 1];

            }

            dice[2, 3] = temp;

            dice[0, 2] = dice[2, 0];

        }

        public static void RotateWest(int[,] dice)

        {

            int temp = dice[2, 3];

            for (int j = 3; j >= 1; j–)

            {

                dice[2, j] = dice[2, j – 1];

            }

            dice[2, 0] = temp;

            dice[0, 2] = dice[2, 0];

        }

    }

}

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s