algorithms

Algorithm implementations
git clone git://git.laack.co/algorithms.git
Log | Files | Refs | README

solution.cpp (1016B)


      1 #include <iostream>
      2 #include <vector>
      3 
      4 using namespace std;
      5 int main() {
      6     char kCol, tCol;
      7     int kRow, tRow;
      8 
      9     cin >> kCol;
     10     cin >> kRow;
     11 
     12     cin >> tCol;
     13     cin >> tRow;
     14 
     15     int step = 0;
     16     vector<string> steps{};
     17 
     18     while(kCol != tCol || kRow != tRow) {
     19         
     20         string colMove, rowMove;
     21         colMove = "";
     22         rowMove = "";
     23 
     24         if(kCol != tCol) {
     25             if(kCol > tCol) {
     26                 kCol -= 1;
     27                 colMove = "L";
     28             }
     29             else {
     30                 kCol += 1;
     31                 colMove = "R";
     32             }
     33 
     34         }
     35         if(kRow != tRow) {
     36             if(kRow > tRow) {
     37                 kRow -= 1;
     38                 rowMove = "D";
     39             }
     40             else {
     41                 kRow += 1;
     42                 rowMove = "U";
     43             }
     44         }
     45 
     46         string res = colMove + rowMove;
     47         steps.push_back(res);
     48         step += 1;
     49 
     50     }
     51 
     52     cout << step << endl;
     53 
     54     for(auto step : steps) {
     55         cout << step << endl;
     56     }
     57 
     58 
     59 }