ZOJ-1208 Roll the Die!

Roll the Die!


Time Limit: 2 Seconds      MemoryLimit: 65536 KB


A gambler's die(Figure 1) is a cube whose six faces are labeled with 1, 2, 3, 4, 5, and 6dots, respectively.

Each of the faces 1, 2, and 3 is adjacent to the other two.

If the die is viewed so that faces 1, 2, and 3 are visible, face 1 is on top,and face 2 is in front, then face 3 will be seen on the right side of the die,as in the figure on the right.

Faces 4, 5, and 6 are opposite to faces 3, 2, and 1, respectively.

In the scenario ofthis problem, a die will be placed on top of a horizontal xy-plane (Figure 2).

One unit length in the xy-plane is equal to the side length of the die.

Initially, the center of the bottom face of the die will coincide with point(0, 0)of the xy-plane.

The xy-plane is situated so that the positive y-axis points to the north, andthe positive x-axis points to the east.

The die will always be observed from the south (looking north), so that thefront side of the die is the one facing south.

The action whichwill be performed on the die will be referred to as "rolling thedie". This action, however, will be very different from the conventionalmeaning of rolling a die.

The die will always be rolled in one of the four directions north, south, east,and west.

Rolling the die to the north, for example, will mean that the die is rotatedthrough an angle of 90 degrees about its north bottom edge.

Starting withposition and configuration shown in Figure 2, let us roll the die to the north.

The position of the die will now change from (0, 0) to (0, 1).

The face 2 will now be on top and the face 6 will be in front.

The resulting position and configuration are shown in Figure 3.

A further roll tothe east will change the position to (1, 1) and bring face 4 to the top, asshown in Figure 4.

Here is an exampleof the input to your program:

1 2 W W W N
1 2 N W W W
1 6 S S S W W W W W
3 3 N N S
4 6 E W W S S S S E S S S S S S S E E S S S S S S E

Each line of the input consists of:

Two integers in the range 1..6, separated by one blank space.

These integers specify the initial orientation of the die. (As stated above,the initial position of the die is always (0, 0), so it does not need to bespecified.)

The first of the two integers specifies the top face of the die, the second onespecifies its front face.

One or more (but not more than 35) upper case letters, each of which is 'N','S', 'E', or 'W'.

Each letter is preceded by one blank space.

These letters specify a sequence of rolls of the die. Each roll will occur tothe north, south, east, or west, as indicated by the corresponding upper caseletter.

Given the aboveinput, your program will write the following output:

Problem 2 by team x

Initialorientation:            top = 1 front = 2
Moves: W W W N 
Final orientation and position:  top = 2  front = 3  x = -3  y =   1

Initialorientation:            top =1  front = 2
Moves: N W W W 
Final orientation and position:  top = 4  front = 6  x = -3  y =   1

Invalid initial orientation:    top = 1  front = 6

Invalid initialorientation:     top = 3  front = 3

Initialorientation:            top = 4 front = 6
Moves: E W W S S S S E S S S S S S S E E S S S S S S E 
Final orientation and position:  top = 2  front = 4  x =  3  y = -17
End of problem 2 by team x

For each line ofinput that contains a valid initial orientation (such as lines 1, 2, and 4 inthe above example), there will be exactly four lines of output:

A blank line.

A line specifying the initial orientation, read from the input.

A line specifying the sequence of moves, read from the input.

A line specifying the final orientation and position of the die. You may assumethat the values of x and y will be in the range -99..99.

For each line of input that contains an invalid initial orientation (the twogiven faces of the die are not adjacent, or the same face is given for the topand the front), there will be exactly two lines of output:

A blank line.

A line stating that the initial orientation is invalid and specifies the valuesof top and front, read from the input.

Observe everydetail of the output, such as the exact wording and punctuation of statements,upper/lower case variations, blank lines and blank spaces. Note in particular,that the values of top and front are printed in fields of width 2, whereas thevalues of x and y are printed in fields of width 4 (including the blank spacewhich follows the equal sign).

A few lines of the above output will be reproduced here with formattingtemplates:

         1        2        3        4        5        6         7
1234567890123456789012345678901234567890123456789012345678901234567890
Invalid initial orientation:     top = 1  front = 6

Initialorientation:            top = 4 front = 6
Moves: E W W S S S S E S S S S S S S E E S S S S S S E 
         1        2        3        4         5        6         7
1234567890123456789012345678901234567890123456789012345678901234567890
Final orientation and position:  top = 2  front = 4  x=   3  y = -17
End of problem 2 by team x

对色子的初始情况进行预处理


#include <stdio.h>
#include <string.h>

void moveOperation(char, int *, int *, int *, int *);
int die[7] = {0, 6, 5, 4, 3 ,2 ,1};
int left[7][7] = {{0, 0, 0, 0, 0, 0, 0}, {0, 0, 4, 2, 5, 3, 0}, {0, 3, 0, 6, 1, 0, 4}, {0, 5, 1, 0, 0, 6, 2}, {0, 2, 6, 0, 0, 1, 5}, {0, 4, 0, 1, 6, 0, 3}, {0, 0, 3, 5, 2, 4, 0}};
int right[7][7] = {{0, 0, 0, 0, 0, 0, 0}, {0, 0, 3, 5, 2, 4, 0}, {0, 4, 0, 1, 6, 0, 3}, {0, 2, 6, 0, 0, 1, 5}, {0, 5, 1, 0, 0, 6, 2}, {0, 3, 0, 6, 1, 0, 4}, {0, 0, 4, 2, 5, 3, 0}};

int main()
{
      int i, j, x, y;
      int top, front, ttop, tfront;
      char move[50], str[100];
      printf("Problem 2 by team x\n");
      while(gets(str) != NULL) {
	    top = str[0] - '0';
	    front = str[2] - '0';
	    for(i = 3, j = 0; i < strlen(str); i++) {
		  if(str[i] != ' ') {
			move[j++] = str[i];
		  }
	    }
	    if(top == front || top == die[front]) {
		  printf("\n");
		  printf("Invalid initial orientation:     top =%2d  front =%2d\n", top, front);
		  continue;
	    }
	    ttop = top;
	    tfront = front;
	    move[49] = j;
	    x = 0;
	    y = 0;
	    for(i = 0; i < move[49]; i++) {
		  moveOperation(move[i], &top, &front, &x, &y);
	    }
	    printf("\n");
	    printf("Initial orientation:             top =%2d  front =%2d\n", ttop, tfront);
	    printf("Moves:");
	    for(i = 0; i < move[49]; i++) {
		  printf(" %c", move[i]);
	    }
	    printf("\n");
	    printf("Final orientation and position:  top =%2d  front =%2d  x =%4d  y =%4d\n", top, front, x, y);
      }
      printf("End of problem 2 by team x\n");
      return 0;
}

void moveOperation(char cmd, int *top, int *front, int *x, int *y)
{
      int temp;
      if(cmd == 'N') {
	    (*y)++;
	    temp = *top;
	    *top = *front;
	    *front = die[temp];
      }
      else if(cmd == 'S') {
	    (*y)--;
	    temp = *top;
	    *top = die[*front];
	    *front = temp;
      }
      else if(cmd == 'E') {
	    (*x)++;
	    *top = left[*top][*front];
      }
      else {
	    (*x)--;
	    *top = right[*top][*front];
      }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值