Advertisement
alansam

Loop breaking II.

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