Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdlib.h>
  4. #include <vector>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. int** field;
  10. int* rows;
  11. int* cals;
  12. int* dia1;
  13. int* dia2;
  14. int n;
  15.  
  16. bool horse(int row) {
  17.  
  18.     // Выход из рекурсии
  19.     if (row == n)
  20.         return true;
  21.  
  22.     int cal = 0;
  23.  
  24.     while (cal < n)
  25.     {
  26.         if (cals[cal] == 0 && rows[row] == 0 && dia1[row + cal] == 0 && dia2[row + n - 1 - cal] == 0) {
  27.             cals[cal] = 1;
  28.             rows[row] = 1;
  29.             dia1[row + cal] = 1;
  30.             dia2[row + n - 1 - cal] = 1;
  31.             field[row][cal] = 1;
  32.             if (horse(row + 1))
  33.                 return true;
  34.             else
  35.             {
  36.                 cals[cal] = 0;
  37.                 rows[row] = 0;
  38.                 dia1[row + cal] = 0;
  39.                 dia2[row + n - 1 - cal] = 0;
  40.                 field[row][cal] = 0;
  41.             }
  42.         }
  43.         cal++;
  44.     }
  45.  
  46.     return false;
  47. }
  48.  
  49. int main(int argc, const char * argv[]) {
  50.  
  51.     fstream fin;
  52.     fin.open("input.txt", ios::in);
  53.  
  54.     if (fin.is_open()) {
  55.  
  56.         fin >> n;
  57.  
  58.         field = new int*[n];
  59.         for (int i = 0; i < n; i++)
  60.         {
  61.             field[i] = new int[n];
  62.         }
  63.  
  64.         for (int i = 0; i < n; i++)
  65.         {
  66.             for (int j = 0; j < n; j++)
  67.             {
  68.                 field[i][j] = 0;
  69.             }
  70.         }
  71.  
  72.  
  73.         cals = new int[n];
  74.         rows = new int[n];
  75.         dia1 = new int[2 * n - 1];
  76.         dia2 = new int[2 * n - 1];
  77.  
  78.         for (int i = 0; i < n; i++)
  79.         {
  80.             cals[i] = 0;
  81.             rows[i] = 0;
  82.         }
  83.  
  84.         for (int i = 0; i < 2 * n - 1; i++)
  85.         {
  86.             dia1[i] = 0;
  87.             dia2[i] = 0;
  88.         }
  89.  
  90.         horse(0);
  91.  
  92.         fstream fout;
  93.         fout.open("output.txt", ios::out);
  94.  
  95.         for (int i = 0; i < n - 1; i++)
  96.         {
  97.             for (int j = 0; j < n; j++)
  98.             {
  99.                 fout << field[i][j];
  100.             }
  101.             fout << endl;
  102.         }
  103.         for (int j = 0; j < n; j++)
  104.         {
  105.             fout << field[n - 1][j];
  106.         }
  107.  
  108.         fout.close();
  109.     }
  110.  
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement