Advertisement
TwITe

Untitled

Jul 31st, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int counter = 0;
  4.  
  5. bool check(int board[100][100], int row, int column, int n) {
  6.     for (int i = 0; i < column; i++) {
  7.         if (board[row][i] == 1) {
  8.             return false;
  9.         }
  10.     }
  11.     /*for (int i = n - 1; i > row; i++) {
  12.         if ((board[i][column]) == 1) {
  13.             return false;
  14.         }
  15.     }*/
  16.     for (int i = 1; i <= n; i++) {
  17.         if (column - i < 0) {
  18.             break;
  19.         }
  20.         if (board[row - i][column - i] == 1) {
  21.             return false;
  22.         }
  23.     }
  24.     for (int i = 1; i <= n; i++) {
  25.         if (column - i < 0) {
  26.             break;
  27.         }
  28.         if (board[row + i][column - i] == 1) {
  29.             return false;
  30.         }
  31.     }
  32.     return true;
  33. }
  34.  
  35. void put(int board[100][100], int column, int n) {
  36.     if (column == n) {
  37.         counter++;
  38.         /*cout << "number of solutions: " << counter << endl;
  39.         for (int i = 0; i < n; i++) {
  40.             for (int j = 0; j < n; j++) {
  41.                 cout << board[i][j] << " ";
  42.             }
  43.             cout << endl;
  44.         }
  45.         cout << endl << endl;*/
  46.     }
  47.     for (int row = 0; row < n; row++) {
  48.         board[row][column] = 1;
  49.         if (check(board, row, column, n)) {
  50.             put(board, column + 1, n);
  51.         }
  52.         board[row][column] = 0;
  53.     }
  54. }
  55.  
  56. int main() { //Ферзи
  57.     int n;
  58.     cin >> n;
  59.     int board[100][100];
  60.     put(board, 0, n);
  61.     cout << counter;
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement