Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <string>
- #include <vector>
- #include <set>
- struct Point
- {
- Point() {};
- Point(int a, int b) :
- x(a),
- y(b)
- {};
- Point(const Point &p) :
- x(std::move(p.x)),
- y(std::move(p.y))
- {};
- Point operator+(const Point &rhs)
- {
- Point lhs;
- lhs.x = this->x + rhs.x;
- lhs.y = this->y + rhs.y;
- return lhs;
- }
- bool operator<(const Point& rhs) const
- {
- if (x != rhs.x)
- {
- return x < rhs.x;
- }
- else
- {
- return y < rhs.y;
- }
- }
- int x = 0;
- int y = 0;
- };
- const std::vector<std::vector<char>> matrix =
- {
- {'A', 'B', 'C', 'E'},
- {'S', 'F', 'C', 'S'},
- {'A', 'D', 'E', 'E'},
- };
- const std::vector<Point> directions =
- {
- Point(-1, 0),
- Point(0, 1),
- Point(1, 0),
- Point(0, -1),
- };
- std::set<Point> used;
- bool find_next_match(std::string input, Point cur_pos)
- {
- input.erase(0, 1);
- if (input.empty()) return true;
- used.insert(cur_pos);
- for (const auto &dir : directions)
- {
- Point new_pos(cur_pos + dir);
- if (new_pos.x < 0 || new_pos.x > matrix.size() - 1) continue;
- if (new_pos.y < 0 || new_pos.y > matrix[0].size() - 1) continue;
- if (used.count(new_pos)) continue;
- if (matrix[new_pos.x][new_pos.y] != input[0]) continue;
- if (find_next_match(input, new_pos)) return true;
- }
- used.erase(cur_pos);
- return false;
- }
- int main (int argc, char **argv)
- {
- std::ifstream infile(argv[1], std::ios::in | std::ifstream::binary);
- std::string line;
- while (infile.good() && getline(infile, line))
- {
- bool found = false;
- for (size_t m = 0; m < matrix.size(); ++m)
- {
- for (size_t n = 0; n < matrix[0].size(); ++n)
- {
- if (matrix[m][n] == line[0])
- {
- used.clear();
- if (find_next_match(line, Point(m, n)))
- {
- found = true;
- break;
- }
- }
- if (found) break;
- }
- }
- std::cout << (found ? "True" : "False") << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement