Guest User

Untitled

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