Advertisement
STANAANDREY

dame cls

Oct 19th, 2020 (edited)
931
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  4. #define NMAX 20
  5. using namespace std;
  6. ofstream fout("DAME.TXT");
  7. int n, usedCol[NMAX], st[NMAX];
  8. char board[NMAX][NMAX];
  9.  
  10. void display() {
  11.     for (int i = 1; i <= n; i++)
  12.         for (int j = 1; j <= n; j++)
  13.             board[i][j] = '*';
  14.     for (int i = 1; i <= n; i++)
  15.         board[i][st[i]] = 'D';
  16.     for (int i = 1; i <= n; i++)
  17.         fout << (board[i] + 1) << endl;
  18.     fout << endl;
  19. }
  20.  
  21. int attackOnDiag(int line, int col) {
  22.     for (int i = 1; i < line; i++)
  23.         if (abs(line - i) == abs(col - st[i]))
  24.             return 1;
  25.     return 0;
  26. }
  27.  
  28. void bktr(int k) {
  29.     if (k == n + 1)
  30.         display();
  31.     else
  32.         for (int i = 1; i <= n; i++)
  33.             if (!usedCol[i] && !attackOnDiag(k, i)) {
  34.                 usedCol[i] = 1;
  35.                 st[k] = i;
  36.                 bktr(k + 1);
  37.                 usedCol[i] = 0;
  38.             }
  39. }
  40.  
  41. int main() {
  42.     cin >> n;
  43.     bktr(1);
  44.     return 0;
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement