網頁

2014年1月21日 星期二

UVA 10409 - Die Game

 #include <iostream>
 #include <string>
 using namespace std;
 
 class Dice{
  public:
   int up, down, north, east, south, west ;
   void init (){
     up=1, down=6, north=2, west=3, east=4, south=5 ;  
   }
   void rotate (string input){
     int tmp = 0 ; 
     if (input=="north")
       tmp=up, up=south, south=down, down=north, north=tmp;
     if (input=="south")
       tmp=up, up=north, north=down, down=south, south=tmp;
     if (input=="east")
       tmp=up, up=west, west=down, down=east, east=tmp;
     if (input=="west")
       tmp=up, up=east, east=down, down=west, west=tmp;
   }
 }; 
 
 int main(int argc, char *argv[])
 {
   int t ;
   string input;
   Dice dice ;
   
  while (cin >> t && t){
    dice.init() ; 
    while (t--){
      cin >> input ;
      dice.rotate(input) ;  
    }
    cout << dice.up << endl;
  }
  return 0;
}
 #include <iostream>
 #include <string>
 #include <map>
 using namespace std;
 
 class Dice{
  public:
   int tmp, up, down, north, east, south, west ;
   map<string, void (Dice::*)()> m;
 
   void init (){
     tmp=0, up=1, down=6, north=2, west=3, east=4, south=5;
     m["north"] = &Dice::rotateNorth;
     m["south"] = &Dice::rotateSouth;
     m["west"] = &Dice::rotateWest;
     m["east"] = &Dice::rotateEast;
   }
   void rotate (string input){
     (this->*m[input])(); 
   }
 
   void rotateNorth(){
     tmp=up, up=south, south=down, down=north, north=tmp;
   }
   void rotateSouth(){
     tmp=up, up=north, north=down, down=south, south=tmp;
   }
   void rotateEast(){
     tmp=up, up=west, west=down, down=east, east=tmp;
   }
   void rotateWest(){
     tmp=up, up=east, east=down, down=west, west=tmp;
   }
 }; 
 
 int main(int argc, char *argv[])
 {
   int t ;
   string input;
  Dice dice ;
  
  while (cin >> t && t){
    dice.init() ; 
    while (t--){
      cin >> input ;
      dice.rotate(input) ;  
    }
    cout << dice.up << endl;
  }
  return 0;
}

沒有留言:

張貼留言