Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <cctype>
- using namespace std;
- struct Coord {
- int x, y;
- Coord(int xIn = 0, int yIn = 0) : x(xIn), y(yIn) {}
- Coord operator+(const Coord &other) {
- return Coord(x + other.x, y + other.y);
- }
- Coord operator=(const Coord &other) {
- x = other.x;
- y = other.y;
- return *this;
- }
- bool legal() {
- return (x >= 0 && x < 3 && y >= 0 && y < 3);
- }
- };
- int main() {
- fstream in("input.txt");
- string line;
- // const int BUTTON[3][3] = {
- // {1, 2, 3},
- // {4, 5, 6},
- // {7, 8, 9}
- // };
- //
- const char BUTTON[5][5] = {
- {'?', '?', '1', '?', '?'},
- {'?', '2', '3', '4', '?'},
- {'5', '6', '7', '8', '9'},
- {'?', 'A', 'B', 'C', '?'},
- {'?', '?', 'D', '?', '?'}
- };
- // Coord location(1, 1);
- Coord location(2, 2);
- Coord movement;
- while(in >> line) {
- // cout << line << endl;
- for(int i = 0; i < line.size(); i++) {
- if(line.at(i) == 'U') {
- movement = Coord(0, -1);
- }
- else if(line.at(i) == 'R') {
- movement = Coord(1, 0);
- }
- else if(line.at(i) == 'D') {
- movement = Coord(0, 1);
- }
- else if(line.at(i) == 'L') {
- movement = Coord(-1, 0);
- }
- Coord tempLocation = location + movement;
- char button = BUTTON[tempLocation.y][tempLocation.x];
- // if(tempLocation.legal()) {
- if(isalnum(button)) {
- location = tempLocation;
- }
- }
- cout << BUTTON[location.y][location.x];
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement