Advertisement
TwITe

Untitled

Jul 31st, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. bool check(int board[8][8], int row, int j, int n) {
  2.     for (int i = 0; i < n; i++) {
  3.         if (board[row][i] == 1) {
  4.             return false;
  5.         }
  6.     }
  7.     for (int i = 0; i < n; i++) {
  8.         if ((board[i][j]) == 1) {
  9.             return false;
  10.         }
  11.     }
  12.     for (int i = 1; i >= 0; i++) {
  13.         if (j - i < 0) {
  14.             break;
  15.         }
  16.             if (board[row - i][j - i] == 1) {
  17.                 return false;
  18.             }
  19.     }
  20.     for (int i = 1; i >= 0; i++) {
  21.         if (j - i < 0) {
  22.             break;
  23.         }
  24.         if (board[row + i][j - i] == 1) {
  25.             return false;
  26.         }
  27.     }
  28.     return true;
  29. }
  30.  
  31. int put(int board[8][8], int column, int count, int n) {
  32.     if (column == n) {
  33.         count++;
  34.     }
  35.     else {
  36.         for (int i = 0; i < n; i++) {
  37.             board[i][column] = 1;
  38.             if (check(board, i, column, n)) {
  39.                 put(board, column + 1, count, n);
  40.             }
  41.         }
  42.     }
  43.     return count;
  44. }
  45.  
  46. int task16() { //Ферзи
  47.     int n, count = 0;
  48.     cin >> n;
  49.     int board[8][8];
  50.     cout << put(board, 0, count, n);
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement