Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- struct Pos {
- int x, y;
- };
- int board[12][12];
- void okMoves(Pos p, vector<Pos>& result)
- {
- for (int i = -1; i <= 1; i += 2) {
- for (int j = -2; j <= 2; j += 4) {
- if (board[p.x + i][p.y + j] == 0) {
- Pos q;
- q.x = p.x + i;
- q.y = p.y + j;
- result.push_back(q);
- }
- if (board[p.x + j][p.y + i] == 0) {
- Pos q;
- q.x = p.x + j;
- q.y = p.y + i;
- result.push_back(q);
- }
- }
- }
- }
- bool go(Pos p, int step)
- {
- board[p.x][p.y] = step;
- if (step == 64) {
- return true;
- }
- vector<Pos> moves;
- okMoves(p, moves);
- sort(begin(moves), end(moves), [](Pos q1, Pos q2) {
- vector<Pos> m1, m2;
- okMoves(q1, m1);
- okMoves(q2, m2);
- return m1.size() < m2.size();
- });
- for (Pos q : moves) {
- vector<Pos> m;
- okMoves(q, m);
- if(m.size() == 0 && step < 63) {
- continue;
- }
- if(go(q, step + 1)) {
- return true;
- }
- }
- board[p.x][p.y] = 0;
- return false;
- }
- int main()
- {
- for (int i = 0; i < 12; i++) {
- for (int j = 0; j < 12; j++) {
- board[i][j] = 2 <= i && i <= 9 && 2 <= j && j <= 9 ? 0 : -1;
- }
- }
- Pos p;
- cout << "Pa vilken x-koordinat ska springaren borja? (heltal i intervallet [1,8]): ";
- cin >> p.x;
- cout << "Pa vilken y-koordinat ska springaren borja? (heltal i intervallet [1,8]): ";
- cin >> p.y;
- p.x++;
- p.y++;
- go(p, 1);
- for(int i = 0; i < 37; i++) {
- cout << "-";
- }
- cout << "\n";
- for(int j = 0; j < 12; j++) {
- cout << "|";
- for(int i = 0; i < 12; i++) {
- cout << setw(2) << board[i][j];
- cout << "|";
- }
- cout << "\n|";
- for(int i = 0; i < 35; i++){
- cout << "-";
- }
- cout << "|\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement