#UVa:10409-Die Game

灆洢 2016-07-29 00:38:44

將骰子的各面狀態記住(上面1,北面2,西面3,每面與其對面和為7),接著照著指示去轉動即可得解。

C++(0.000)

/*******************************************************/
/* UVa 10409 Die Game                                  */
/* Author: Maplewing [at] knightzone.studio            */
/* Version: 2016/07/29                                 */
/*******************************************************/
#include <iostream>
#include <cstdio>
using namespace std;

struct Dice{
  int top, bottom;
  int north, south;
  int east, west;

  Dice(){
    top = 1;
    north = 2;
    west = 3;
    bottom = 6;
    south = 5;
    east = 4;
  }

  void rotate(string command){
    if( command == "north" ){
      swap(top, north);
      swap(south, top);
      swap(bottom, south);
    }
    else if( command == "south" ){
      swap(top, south);
      swap(north, top);
      swap(bottom, north);
    }
    else if( command == "west" ){
      swap(top, west);
      swap(east, top);
      swap(bottom, east);
    }
    else if( command == "east" ){
      swap(top, east);
      swap(west, top);
      swap(bottom, west);
    }
  }

};



int main(){
  int commandNumber;
  while( scanf("%d", &commandNumber) != EOF && commandNumber != 0 ){
    Dice dice;
    for( int i = 0 ; i < commandNumber ; ++i ){
      string command;
      cin >> command;
      dice.rotate(command);
    }

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

  return 0;
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料