allia

конь гамильтонов

Jan 13th, 2021 (edited)
778
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. using namespace std;
  4.  
  5. const int inf = 100;
  6. struct kletka
  7. {
  8.  int x, y, cost;
  9.  void new_kletka(int x, int y, int cost)
  10.  {
  11.    this->x = x;
  12.    this->y = y;
  13.    this->cost = cost;
  14.  }
  15.  void print_kletka()
  16.  {
  17.    cout << x+1 << " " << y+1 << endl;
  18.  }
  19. };
  20.  
  21. class Graph
  22. {
  23.   queue<kletka> Q;
  24.     int n, m, *dx, *dy, limit;
  25.     bool **arr;
  26.   kletka *path, start;
  27.  
  28. public:
  29.     Graph(int x,  int a)
  30.     {
  31.      n = x;
  32.      m = a;
  33.      limit = n*m-1;
  34.      dx = new int[8]{2, 1, -2, -1, 1, -1, 2, -2};
  35.      dy = new int[8]{-1, -2, 1, 2, 2, -2, 1, -1};
  36.  
  37.      arr = new bool*[n]{0};
  38.       for (int i = 0; i < n; i++)
  39.         arr[i] = new bool[m]{0};
  40.  
  41.       arr[0][0] = true;
  42.       start.new_kletka(0, 0, 0);
  43.       path = new kletka[limit];
  44.     }
  45.  
  46.   void poisk_path();
  47.   int poisk_min (kletka *watched);
  48.   void get_result();
  49.   int variant(kletka a);
  50.   void get_arr();
  51. };
  52.  
  53. int Graph::variant(kletka a)
  54. {
  55.   int x = 0;
  56.   if(a.x >= 0 && a.x < n && a.y >= 0 && a.y < m && arr[a.x][a.y] == 0)
  57.    x++;
  58.   return x;
  59. }
  60.  
  61. int Graph::poisk_min ( kletka *watched)
  62. {
  63.   int min = inf, result = -1;
  64.  
  65.   for (int i = 0; i < 8; i++)
  66.    if (watched[i].cost <= min && watched[i].cost > -1)
  67.     {
  68.       min = watched[i].cost;
  69.       result = i;
  70.     }
  71. return result;
  72. }
  73.  
  74. void Graph::poisk_path()
  75. {
  76.   int shet = 0;
  77.   Q.push(start);
  78.  
  79.  while (shet != limit && !Q.empty())
  80.  {
  81.    kletka current = Q.front();
  82.    Q.pop();
  83.    kletka *visited = new kletka[8];
  84.  
  85.    for (int i = 0; i < 8; i++)
  86.    {
  87.      kletka check;
  88.      check.new_kletka (current.x + dx[i], current.y + dy[i], -1);
  89.      if (variant(check))
  90.       {
  91.         check.cost = 0;
  92.         for (int p = 0; p < 8; p++)
  93.         {
  94.           kletka possible_path;
  95.           possible_path.new_kletka(check.x + dx[p], check.y + dy[p], -1);
  96.           check.cost += variant(possible_path);
  97.         }
  98.       }
  99.      visited[i] = check;
  100.    }
  101.    int index = poisk_min(visited);
  102.    if (index != -1)
  103.    {
  104.      kletka a = visited[index];
  105.      arr[a.x][a.y] = 1;
  106.      path[shet] = a;
  107.      shet++;
  108.      Q.push(a);
  109.    }
  110.  }
  111.   if (shet != limit)
  112.    cout << -1;
  113.   else get_result();
  114. }
  115.  
  116. void Graph::get_result()
  117. {
  118.   start.print_kletka();
  119.   for (int i = 0; i < limit; i++)
  120.    path[i].print_kletka();
  121. }
  122.  
  123. void Graph::get_arr()
  124. {
  125.   for (int i= 0; i < n; i++)
  126.   {
  127.     for (int j = 0; j < m; j++)
  128.     {
  129.       cout.width(3);
  130.       cout << arr[i][j] << " ";
  131.     }
  132.     cout << endl;
  133.   }
  134. }
  135. int main()
  136. {
  137.   int n, m;
  138.   cin >> n >> m;
  139.   Graph object(n, m);
  140.   object.poisk_path();
  141. }
Add Comment
Please, Sign In to add comment