Advertisement
Guest User

Untitled

a guest
Dec 1st, 2016
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cctype>
  5.  
  6. using namespace std;
  7.  
  8. struct Coord {
  9.     int x, y;
  10.  
  11.     Coord(int xIn = 0, int yIn = 0) : x(xIn), y(yIn) {}
  12.  
  13.     Coord operator+(const Coord &other) {
  14.         return Coord(x + other.x, y + other.y);
  15.     }
  16.  
  17.     Coord operator=(const Coord &other) {
  18.         x = other.x;
  19.         y = other.y;
  20.  
  21.         return *this;
  22.     }
  23.  
  24.     bool legal() {
  25.         return (x >= 0 && x < 3 && y >= 0 && y < 3);
  26.     }
  27. };
  28.  
  29. int main() {
  30.     fstream in("input.txt");
  31.     string line;
  32.  
  33.     // const int BUTTON[3][3] = {
  34.     //     {1, 2, 3},
  35.     //     {4, 5, 6},
  36.     //     {7, 8, 9}
  37.     // };
  38.  
  39.     const char BUTTON[5][5] = {
  40.         {'?', '?', '1', '?', '?'},
  41.         {'?', '2', '3', '4', '?'},
  42.         {'5', '6', '7', '8', '9'},
  43.         {'?', 'A', 'B', 'C', '?'},
  44.         {'?', '?', 'D', '?', '?'}
  45.     };
  46.  
  47.     // Coord location(1, 1);
  48.     Coord location(0, 2);
  49.     Coord movement;
  50.     while(in >> line) {
  51.         for(int i = 0; i < line.size(); i++) {
  52.             if(line.at(i) == 'U') {
  53.                 movement = Coord(0, -1);
  54.             }
  55.             else if(line.at(i) == 'R') {
  56.                 movement = Coord(1, 0);
  57.             }
  58.             else if(line.at(i) == 'D') {
  59.                 movement = Coord(0, 1);
  60.             }
  61.             else if(line.at(i) == 'L') {
  62.                 movement = Coord(-1, 0);
  63.             }
  64.             Coord tempLocation = location + movement;
  65.             char button = BUTTON[tempLocation.y][tempLocation.x];
  66.             // if(tempLocation.legal()) {
  67.             if(isalnum(button)) {
  68.                 location = tempLocation;
  69.             }
  70.         }
  71.         cout << BUTTON[location.y][location.x];
  72.     }
  73.  
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement