網頁

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;
}

2014年1月20日 星期一

UVA 10188 minesweeper

 #include <iostream>
 #include <cstdio>
 #include <cstring> 
 #define INF 0x7fffffff
 
 using namespace std ;
 
 int main(int argc, char *argv[])
 {
   int map[102][102] ;
   char ch ; 
   int n, m;
   for (int t=1 ; cin >> n >> m && n && m ; ++t){
     if(t>1) puts("") ;
     memset(map, 0, sizeof(map)) ;
     for (int i=1; i<=n ; ++i){
       for(int j=1 ; j<=m ; ++j){
         cin >> ch; 
         if (ch ==  '.'){
           map[i][j] = 0 ;
         }else {
           map[i][j] = -INF ;
         }
       }
     }
     
     for(int i=1 ; i<=n ; ++i){
       for(int j=1 ; j<=m ; ++j){
         if(map[i][j]<0){
           for(int k=-1 ; k<=1 ; ++k)
             for(int l=-1 ; l<=1 ; ++l)
               map[i+k][j+l]++ ; 
         }
      }
    }

    printf("Field #%d:\n", t) ; 
    for(int i=1 ; i<=n ; ++i){
      for(int j=1 ; j<=m ; ++j){
        if (map[i][j] < 0) {
          printf("*") ; 
        } else {
          printf("%d", map[i][j]) ; 
        }
      }
      puts(""); 
    }
  }
  return 0;
}