Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct Pos {
  6.     int x, y;
  7. };
  8.  
  9. int board[12][12];
  10.  
  11. void okMoves(Pos p, vector<Pos>& result)
  12. {
  13.     for (int i = -1; i <= 1; i += 2) {
  14.         for (int j = -2; j <= 2; j += 4) {
  15.             if (board[p.x + i][p.y + j] == 0) {
  16.                 Pos q;
  17.                 q.x = p.x + i;
  18.                 q.y = p.y + j;
  19.                 result.push_back(q);
  20.             }
  21.             if (board[p.x + j][p.y + i] == 0) {
  22.                 Pos q;
  23.                 q.x = p.x + j;
  24.                 q.y = p.y + i;
  25.                 result.push_back(q);
  26.             }
  27.         }
  28.     }
  29. }
  30. bool go(Pos p, int step)
  31. {
  32.     board[p.x][p.y] = step;
  33.     if (step == 64) {
  34.         return true;
  35.     }
  36.     vector<Pos> moves;
  37.     okMoves(p, moves);
  38.     sort(begin(moves), end(moves), [](Pos q1, Pos q2) {
  39.         vector<Pos> m1, m2;
  40.         okMoves(q1, m1);
  41.         okMoves(q2, m2);
  42.         return m1.size() < m2.size();
  43.     });
  44.     for (Pos q : moves) {
  45.         vector<Pos> m;
  46.         okMoves(q, m);
  47.         if(m.size() == 0 && step < 63) {
  48.             continue;
  49.         }
  50.         if(go(q, step + 1)) {
  51.             return true;
  52.         }
  53.     }
  54.     board[p.x][p.y] = 0;
  55.     return false;
  56. }
  57. int main()
  58. {
  59.     for (int i = 0; i < 12; i++) {
  60.         for (int j = 0; j < 12; j++) {
  61.             board[i][j] = 2 <= i && i <= 9 && 2 <= j && j <= 9 ? 0 : -1;
  62.         }
  63.     }
  64.     Pos p;
  65.     cout << "Pa vilken x-koordinat ska springaren borja? (heltal i intervallet [1,8]): ";
  66.     cin >> p.x;
  67.     cout << "Pa vilken y-koordinat ska springaren borja? (heltal i intervallet [1,8]): ";
  68.     cin >> p.y;
  69.     p.x++;
  70.     p.y++;
  71.     go(p, 1);
  72.     for(int i = 0; i < 37; i++) {
  73.         cout << "-";
  74.     }
  75.     cout << "\n";
  76.     for(int j = 0; j < 12; j++) {
  77.         cout << "|";
  78.         for(int i = 0; i < 12; i++) {
  79.             cout << setw(2) << board[i][j];
  80.             cout << "|";
  81.         }
  82.         cout << "\n|";
  83.         for(int i = 0; i < 35; i++){
  84.             cout << "-";
  85.         }
  86.         cout << "|\n";
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement