Advertisement
alansam

Loop breaking.

Jan 15th, 2024 (edited)
720
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.06 KB | Source Code | 0 0
  1. /*
  2.  * Trying to refactor someone else's code from codingame website.
  3.  * I am trying to replace a massive if else block with a switch statement.
  4.  * I cannot figure out why my statement is not equivalent to there's.
  5.  * Sorry for the long post. My Git seems broken lately.
  6.  * Can someone explain please?
  7.  */
  8.  
  9. #include <iostream>
  10. #include <string>
  11. #include <vector>
  12. #include <algorithm>
  13.  
  14. #define SWITCH_
  15.  
  16. int L = 8;
  17. int C = 8;
  18. int N;
  19.  
  20. std::vector<std::string> rows = {
  21.   {"########"},
  22.   {"# @    #"},
  23.   {"#     X#"},
  24.   {"# XXX  #"},
  25.   {"#   XX #"},
  26.   {"#   XX #"},
  27.   {"#     $#"},
  28.   {"########"}
  29. };
  30.  
  31. std::pair<int, int> move(std::string s, std::pair<int, int> lpos) {
  32.   if      (s == "SOUTH") { lpos.second++; }
  33.   else if (s == "WEST")  { lpos.first--; }
  34.   else if (s == "EAST")  { lpos.first++; }
  35.   else if (s == "NORTH") { lpos.second--; }
  36.   return lpos;
  37. }
  38.  
  39. bool isPassable(std::string dir, std::pair<int, int> pos, bool dis) {
  40.   std::pair<int, int> npos = move(dir, pos);
  41.   if (npos.first < 0 || npos.second < 0 || npos.first >= C || npos.second >= L) {
  42.     return false;
  43.   }
  44.   auto c = rows[npos.second][npos.first];
  45.   return !(c == '#' || (c == 'X' && !dis));
  46. }
  47.  
  48. int main() {
  49.   std::pair<int, int> pos;
  50.   std::vector<std::string> directions = { "SOUTH", "EAST", "NORTH", "WEST" };
  51.   std::vector<std::pair<int, int>> tl;
  52.   std::string out = "";
  53.   bool BM {};
  54.   rows.resize(L);
  55.   for (auto i = 0ul; i < L; i++) {
  56.     auto f = rows[i].find("@");
  57.     if (f != std::string::npos) {
  58.       pos = std::make_pair(f, i);
  59.     }
  60.     for (auto j = 0ul; j < rows[i].size(); j++) {
  61.       if (rows[i][j] == 'T') {
  62.         tl.emplace_back(j, i);
  63.       }
  64.     }
  65.   }
  66.  
  67.   std::string dir = "SOUTH";
  68.   /* loop forever */ while (true) {
  69.     if (!isPassable(dir, pos, BM)) {
  70.       for (auto const & ds : directions) {
  71.         if (isPassable(ds, pos, BM)) {
  72.           dir = ds;
  73.           break;
  74.         }
  75.       }
  76.     }
  77.  
  78.     out += dir + "\n";
  79.     pos = move(dir, pos);
  80.  
  81.     char block = rows[pos.second][pos.first];
  82. #ifdef SWITCH_
  83.     //My Way...Seems to loop infinitely.
  84.     auto complete { false }; /* flag for loop termination - initially false */
  85.     switch (block) {
  86.       case 'X':
  87.         rows[pos.second].replace(pos.first, 1, " ");
  88.         break;
  89.  
  90.       case '$':
  91.         /* loop should terminate - set flag */
  92.         complete = true;
  93.         break;
  94.  
  95.       default:
  96.         for (auto const & ds : directions) {
  97.           if (ds[0] == block) {
  98.             dir = ds;
  99.             break;
  100.           }
  101.         }
  102.     }
  103.     if (complete) { /* terminate forever loop */ break; }
  104. #else
  105.     //Their Way...Works
  106.     if (block == 'X') {
  107.       rows[pos.second].replace(pos.first, 1, " ");
  108.     }
  109.     else if (block == '$') {
  110.       break;
  111.     }
  112.     else {
  113.       for (auto const & ds : directions) {
  114.         if (ds[0] == block) {
  115.           dir = ds;
  116.           /* terminate forever loop */  break;
  117.         }
  118.       }
  119.     }
  120. #endif
  121.  
  122.     // Loop check
  123.     N++;
  124.     if (N > C * L) {
  125.       out = "LOOP\n";
  126.       break;
  127.     }
  128.   }
  129.   std::cout << out;
  130. }
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement