// dama.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include using namespace std; const int N = 4; int a [N][N]; void nuluj() { for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) a[i][j] = -1; } int cnt = 0; void tiskni() { cnt ++; cout << "Vysledek " << cnt << endl; for (int j = 0; j < N; j++) cout << "--"; cout << "-" << endl; for (int i = 0; i < N; i++) { cout << "|"; for (int j = 0; j < N; j++) { if (a[i][j] == -2) cout << "@"; else if (a[i][j] == 1) cout << "*"; else cout << " "; cout << "|"; } cout << endl; for (int j = 0; j < N; j++) cout << "--"; cout << "-" << endl; } cout << endl; } bool bez (int i, int j, int dx, int dy) { bool ok = true; bool stop = false; // cout << "Bez " << i << "," << j << " --> " << dx << "," << dy ; i += dy; // i je cislo radky, tedy smer y j += dx; // j je cislo sloupce, tedy smer x while (i >= 0 && i < N && j >= 0 && j < N && ! stop && ok) { if (a[i][j] == 1) ok = false; if (a[i][j] == -2) stop = true; i += dy; j += dx; } // cout << " = " << ok << " (" << i-dy << "," << j-dx << ")" << endl; return ok; } void playQueen (int i, int j) { // cout << "Play " << i << "," << j << endl; if (a[i][j] != -2) { bool ok = bez (i, j, -1, 0) && // vlevo bez (i, j, -1, -1) && bez (i, j, -1, +1) && bez (i, j, 0, -1); // nahoru if (ok) { a[i][j] = 1; // cout << "Umistim " << i << "," << j << endl; if (i+1 < N) playQueen (i+1, j); else if (j+1 < N) playQueen (0 , j+1); else tiskni (); a[i][j] = 0; } } if (i+1 < N) playQueen (i+1, j); else if (j+1 < N) playQueen (0, j+1); else tiskni (); } int _tmain(int argc, _TCHAR* argv[]) { nuluj(); a[1][0] = -2; playQueen(0, 0); cout << "Konec - stisknete enter" << endl; char c; cin >> c; return 0; }