Advertisement
Guest User

CR 164051 Revision 2

a guest
May 24th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <string>
  5. #include <vector>
  6. #include <set>
  7.  
  8. struct Point
  9. {
  10.     Point() {};
  11.  
  12.     Point(int a, int b) :
  13.         x(a),
  14.         y(b)
  15.     {};
  16.  
  17.     Point(const Point &p) :
  18.         x(std::move(p.x)),
  19.         y(std::move(p.y))
  20.     {};
  21.  
  22.     Point operator+(const Point &rhs)
  23.     {
  24.         Point lhs;
  25.  
  26.         lhs.x = this->x + rhs.x;
  27.         lhs.y = this->y + rhs.y;
  28.  
  29.         return lhs;
  30.     }
  31.  
  32.     bool operator<(const Point& rhs) const
  33.     {
  34.         if (x != rhs.x)
  35.         {
  36.             return x < rhs.x;
  37.         }
  38.         else
  39.         {
  40.             return y < rhs.y;
  41.         }
  42.     }
  43.  
  44.     int x = 0;
  45.     int y = 0;
  46. };
  47.  
  48. const std::vector<std::vector<char>> matrix =
  49. {
  50.     {'A', 'B', 'C', 'E'},
  51.     {'S', 'F', 'C', 'S'},
  52.     {'A', 'D', 'E', 'E'},
  53. };
  54.  
  55. const std::vector<Point> directions =
  56. {
  57.     Point(-1, 0),
  58.     Point(0, 1),
  59.     Point(1, 0),
  60.     Point(0, -1),
  61. };
  62.  
  63. std::set<Point> used;
  64.  
  65. bool find_next_match(std::string input, Point cur_pos)
  66. {
  67.     input.erase(0, 1);
  68.  
  69.     if (input.empty()) return true;
  70.  
  71.     used.insert(cur_pos);
  72.  
  73.     for (const auto &dir : directions)
  74.     {
  75.         Point new_pos(cur_pos + dir);
  76.  
  77.         if (new_pos.x < 0 || new_pos.x > matrix.size() - 1) continue;
  78.         if (new_pos.y < 0 || new_pos.y > matrix[0].size() - 1) continue;
  79.  
  80.         if (used.count(new_pos)) continue;
  81.  
  82.         if (matrix[new_pos.x][new_pos.y] != input[0]) continue;
  83.  
  84.         if (find_next_match(input, new_pos)) return true;
  85.     }
  86.  
  87.     used.erase(cur_pos);
  88.  
  89.     return false;
  90. }
  91.  
  92. int main (int argc, char **argv)
  93. {
  94.     std::ifstream infile(argv[1], std::ios::in | std::ifstream::binary);
  95.  
  96.     std::string line;
  97.     while (infile.good() && getline(infile, line))
  98.     {
  99.         bool found = false;
  100.         for (size_t m = 0; m < matrix.size(); ++m)
  101.         {
  102.             for (size_t n = 0; n < matrix[0].size(); ++n)
  103.             {
  104.                 if (matrix[m][n] == line[0])
  105.                 {
  106.                     used.clear();
  107.                     if (find_next_match(line, Point(m, n)))
  108.                     {
  109.                         found = true;
  110.                         break;
  111.                     }
  112.                 }
  113.                 if (found) break;
  114.             }
  115.         }
  116.  
  117.         std::cout << (found ? "True" : "False") << "\n";
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement