<Problem>
http://ntucoder.net/Problem/Details/115
//#include <windows.h>
#include <iostream>
using namespace std;
#define N 8
int y;
int x;
// ham kiem tra viec dat hau tai vi tris (x,y) co ok khong
bool isSafe(int board[][N], int row, int col) {
// neu phia ben trai hang hien tai da co quan hau
for (int i = 0; i < N; i++) {
if (board[row][i] == 1) {
return false;
}
}
// kiem tra duong cheo tren (trai)
for (int i = row, j = col; i >= 0 && j >= 0; i--, j--) {
if (board[i][j] == 1) {
return false;
}
}
// kiem tra duong cheo tren (phai)
for (int i = row, j = col; i >= 0 && j < N; i--, j++) {
if (board[i][j] == 1) {
return false;
}
}
// kiem tra duong cheo duoi (trai)
for (int i = row, j = col; i < N && j >= 0; i++, j--) {
if (board[i][j] == 1) {
return false;
}
}
// kiem tra duong cheo duoi (phai)
for (int i = row, j = col; i < N && j < N; i++, j++) {
if (board[i][j] == 1) {
return false;
}
}
// ok
return true;
}
bool solQueen(int board[][N], int col) {
void showResult(int board[][N]);
/*cout << "==========\n";
showResult(board);
Sleep(1000);*/
/*if (col < 0) {
return false;
}*/
if (col >= N) {
return true;
}
if (col == x - 1) solQueen(board, col + 1);
else {
// xet tung cot tren ban co lan luot dat hau o cac hang phu hop
for (int i = 0; i < N; i++) {
if (isSafe(board, i, col)) {
board[i][col] = 1; // dat hau o vi tri (i, col)
if (solQueen(board, col + 1)) { // dat hau o cot tiep theo dan toi 1 loi giai
return true; // thong bao co loi giai
}
else {
board[i][col] = 0;
}
}
}
//if (col - 1 == x - 1) solQueen(board, col - 2);
return false;
}
}
void showResult(int board[][N]) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (board[i][j] == 1) {
cout << "w";
}
else {
cout << ".";
}
}
cout << endl;
}
}
int main() {
cin >> y;
cin >> x;
int board[N][N] = { 0 };
board[y - 1][x - 1] = 1;
bool result = solQueen(board, 0);
if (result) {
showResult(board);
}
else {
cout << "Loi\n";
}
return 0;
}