Guest User

Untitled

a guest
Jan 24th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <stack>
  5.  
  6. // moves
  7. const int d_size = 8;
  8. const int dx[d_size] = {+1, +1,  0, -1, -1, -1,  0, +1};
  9. const int dy[d_size] = { 0, +1, +1, +1,  0, -1, -1, -1};
  10.  
  11. struct point {
  12.     size_t x, y;
  13.     point() {
  14.         this->x = 0;
  15.         this->y = 0;
  16.     }
  17.     point(const size_t & x, const size_t & y) {
  18.         this->x = x;
  19.         this->y = y;
  20.     }
  21.     point& operator += (const point & other) {
  22.         this->x += other.x;
  23.         this->y += other.y;
  24.         return *this;
  25.     }
  26.     bool operator == (const point & other) const {
  27.         return (this->x == other.x && this->y == other.y);
  28.     }
  29. };
  30.  
  31. size_t n = 0, m = 0, ans = 0;
  32. std::vector< std::vector<int> > Field;
  33. point s, t;
  34. std::queue<point> Q;
  35.  
  36.  
  37.  
  38. bool good(const point & p) {
  39.     bool b1 = (p.x >= 0 && p.x < n);
  40.     bool b2 = (p.y >= 0 && p.y < m);
  41.  
  42.     return b1 && b2;
  43. }
  44.  
  45. void bfs(const point & p) {
  46.     for (size_t i = 0; i < d_size; ++i) {
  47.         point new_point = p;
  48.         while (good(new_point += point(dx[i], dy[i])) && Field[new_point.x][new_point.y] != -1)
  49.             if (Field[new_point.x][new_point.y] == 0) {
  50.                 Field[new_point.x][new_point.y] =  Field[p.x][p.y] + 1;
  51.                 Q.push(new_point);
  52.             }
  53.     }
  54. }
  55.  
  56. void rec(std::vector<point> & way) {
  57.     const point p = way[way.size() - 1];
  58.  
  59.     if (p == t) {
  60.         ans = true;
  61.         std::cout << way.size() - 1 << "\n";
  62.         for (std::vector<point>::const_iterator it = way.begin(); it != way.end(); ++it)
  63.             std::cout << it->x << " " << it->y << "\n";
  64.         return;
  65.     }
  66.  
  67.     if (Field[p.x][p.y] == -1 || Field[p.x][p.y] + way.size() - 1 != Field[s.x][s.y])
  68.         return;
  69.  
  70.     for (size_t i = 0; i < d_size; ++i) {
  71.         point new_point = p;
  72.         while (good(new_point += point(dx[i], dy[i])) && Field[new_point.x][new_point.y] != -1) {
  73.             way.push_back(new_point);
  74.             rec(way);
  75.             way.pop_back();
  76.         }
  77.     }
  78.  
  79. }
  80.  
  81. int main() {
  82.     freopen("input.txt", "r", stdin);
  83.     //freopen("output.txt", "w", stdout);
  84.     std::cin >> n >> m;
  85.    
  86.     Field.resize(n);
  87.     for (size_t i = 0; i < Field.size(); ++i)
  88.         Field[i].resize(m);
  89.  
  90.     size_t k = 0;
  91.     std::cin >> k;
  92.     for (size_t i = 0; i < k; ++i) {
  93.         size_t x = 0, y = 0;
  94.         std::cin >> x >> y;
  95.         Field[x][y] = -1;
  96.     }
  97.  
  98.     std::cin >> s.x >> s.y;
  99.     std::cin >> t.x >> t.y;
  100.    
  101.     Q.push(t);
  102.     Field[t.x][t.y] = 1;
  103.  
  104.     while (!Q.empty()) {
  105.         bfs(Q.front());
  106.         Q.pop();
  107.     }
  108.  
  109.     std::vector<point> way;
  110.     way.push_back(s);
  111.     rec(way);
  112.    
  113.     if (!ans)
  114.         std::cout << "no_solutions";
  115.     return 0;
  116. }
Add Comment
Please, Sign In to add comment