Advertisement
TwITe

Untitled

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