Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cmath>
- #define NMAX 20
- using namespace std;
- ofstream fout("DAME.TXT");
- int n, usedCol[NMAX], st[NMAX];
- char board[NMAX][NMAX];
- void display() {
- for (int i = 1; i <= n; i++)
- for (int j = 1; j <= n; j++)
- board[i][j] = '*';
- for (int i = 1; i <= n; i++)
- board[i][st[i]] = 'D';
- for (int i = 1; i <= n; i++)
- fout << (board[i] + 1) << endl;
- fout << endl;
- }
- int attackOnDiag(int line, int col) {
- for (int i = 1; i < line; i++)
- if (abs(line - i) == abs(col - st[i]))
- return 1;
- return 0;
- }
- void bktr(int k) {
- if (k == n + 1)
- display();
- else
- for (int i = 1; i <= n; i++)
- if (!usedCol[i] && !attackOnDiag(k, i)) {
- usedCol[i] = 1;
- st[k] = i;
- bktr(k + 1);
- usedCol[i] = 0;
- }
- }
- int main() {
- cin >> n;
- bktr(1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement