Advertisement
vardgrig

Knight Tour (begginer C++ version)

Feb 13th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. bool c[8][8];
  5. int countFalses(int x, int y, bool c[8][8])
  6. {
  7.     int count = 0;
  8.     if (x + 1 <= 7 && y - 2 >= 0 && c[x + 1][y - 2] == false)
  9.         ++count;
  10.     if (x + 1 <= 7 && y + 2 <= 7 && c[x + 1][y + 2] == false)
  11.         ++count;
  12.     if (x + 2 <= 7 && y - 1 >= 0 && c[x + 2][y - 1] == false)
  13.         ++count;
  14.     if (x + 2 <= 7 && y + 1 <= 7 && c[x + 2][y + 1] == false)
  15.         ++count;
  16.     if (x - 1 >= 0 && y - 2 >= 0 && c[x - 1][y - 2] == false)
  17.         ++count;
  18.     if (x - 1 >= 0 && y + 2 <= 7 && c[x - 1][y + 2] == false)
  19.         ++count;
  20.     if (x - 2 >= 0 && y - 1 >= 0 && c[x - 2][y - 1] == false)
  21.         ++count;
  22.     if (x - 2 >= 0 && y + 1 <= 7 && c[x - 2][y + 1] == false)
  23.         ++count;
  24.     return count;
  25. }
  26. int main() {
  27.     int x, y;
  28.     cin >> x >> y;
  29.     vector<int> v(8);
  30.     for (int i = 1; i <= 64; ++i){
  31.         c[x][y] = true;
  32.         cout << i << ") " << x << " " << y << "\n";
  33.         int min = 8;
  34.         fill(v.begin(), v.end(), 8);
  35.         if (x + 1 <= 7 && y + 2 <= 7 && c[x + 1][y + 2] == false) // 0
  36.             v[0] = countFalses(x + 1, y + 2, c);
  37.         if (x - 1 >= 0 && y + 2 <= 7 && c[x - 1][y + 2] == false) // 1
  38.             v[1] = countFalses(x - 1, y + 2, c);
  39.         if (x + 2 <= 7 && y + 1 <= 7 && c[x + 2][y + 1] == false) // 2
  40.             v[2] = countFalses(x + 2, y + 1, c);
  41.         if (x - 2 >= 0 && y + 1 <= 7 && c[x - 2][y + 1] == false) // 3
  42.             v[3] = countFalses(x - 2, y + 1, c);
  43.         if (x - 2 >= 0 && y - 1 >= 0 && c[x - 2][y - 1] == false) // 4
  44.             v[4] = countFalses(x - 2, y - 1, c);
  45.         if (x + 2 <= 7 && y - 1 >= 0 && c[x + 2][y - 1] == false) // 5
  46.             v[5] = countFalses(x + 2, y - 1, c);
  47.         if (x - 1 >= 0 && y - 2 >= 0 && c[x - 1][y - 2] == false) // 6
  48.             v[6] = countFalses(x - 1, y - 2, c);
  49.         if (x + 1 <= 7 && y - 2 >= 0 && c[x + 1][y - 2] == false) // 7
  50.             v[7] = countFalses(x + 1, y - 2, c);
  51.         for (int j = 0; j < 8; ++j)
  52.             if(v[j]<min)
  53.                 min = v[j];
  54.         if (v[0] == min) { // 0
  55.             x += 1;
  56.             y += 2;
  57.         }
  58.         else if (v[1] == min) { // 1
  59.             x -= 1;
  60.             y += 2;
  61.         }
  62.         else if (v[2] == min) { // 2
  63.             x += 2;
  64.             y += 1;
  65.         }
  66.         else if (v[3] == min) { // 3
  67.             x -= 2;
  68.             y += 1;
  69.         }
  70.         else if (v[4] == min) { // 4
  71.             x -= 2;
  72.             y -= 1;
  73.         }
  74.         else if (v[5] == min) { // 5
  75.             x += 2;
  76.             y -= 1;
  77.         }
  78.         else if (v[6] == min) { // 6
  79.             x -= 1;
  80.             y -= 2;
  81.         }
  82.         else if (v[7] == min) { // 7
  83.             x += 1;
  84.             y -= 2;
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement