Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <stdlib.h>
- #include <vector>
- #include <string>
- using namespace std;
- int** field;
- int* rows;
- int* cals;
- int* dia1;
- int* dia2;
- int n;
- bool horse(int row) {
- // Выход из рекурсии
- if (row == n)
- return true;
- int cal = 0;
- while (cal < n)
- {
- if (cals[cal] == 0 && rows[row] == 0 && dia1[row + cal] == 0 && dia2[row + n - 1 - cal] == 0) {
- cals[cal] = 1;
- rows[row] = 1;
- dia1[row + cal] = 1;
- dia2[row + n - 1 - cal] = 1;
- field[row][cal] = 1;
- if (horse(row + 1))
- return true;
- else
- {
- cals[cal] = 0;
- rows[row] = 0;
- dia1[row + cal] = 0;
- dia2[row + n - 1 - cal] = 0;
- field[row][cal] = 0;
- }
- }
- cal++;
- }
- return false;
- }
- int main(int argc, const char * argv[]) {
- fstream fin;
- fin.open("input.txt", ios::in);
- if (fin.is_open()) {
- fin >> n;
- field = new int*[n];
- for (int i = 0; i < n; i++)
- {
- field[i] = new int[n];
- }
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < n; j++)
- {
- field[i][j] = 0;
- }
- }
- cals = new int[n];
- rows = new int[n];
- dia1 = new int[2 * n - 1];
- dia2 = new int[2 * n - 1];
- for (int i = 0; i < n; i++)
- {
- cals[i] = 0;
- rows[i] = 0;
- }
- for (int i = 0; i < 2 * n - 1; i++)
- {
- dia1[i] = 0;
- dia2[i] = 0;
- }
- horse(0);
- fstream fout;
- fout.open("output.txt", ios::out);
- for (int i = 0; i < n - 1; i++)
- {
- for (int j = 0; j < n; j++)
- {
- fout << field[i][j];
- }
- fout << endl;
- }
- for (int j = 0; j < n; j++)
- {
- fout << field[n - 1][j];
- }
- fout.close();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement