Advertisement
Guest User

CR 164051 Revision 3

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