bool isSafe(int[,] board, int row, int col)
{
int i, j;
/* Check this row on left side */
for (i = 0; i < col; i++)
if (board[row, i] == 1)
return false;
/* Check upper diagonal on left side */
for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
if (board[i, j] == 1)
/* Check lower diagonal on left side */
for (i = row, j = col; j >= 0 && i < 8; i++, j--)
return true;
}
/* A recursive utility function
to solve N Queen problem */
bool solveNQUtil(int[,] board, int col)
/* base case: If all queens are placed
then return true */
if (col == 8)
// printSolution(board);
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
if(board[i,j]==1)
Matrix[i][j].BackgroundImage = hinhconco1;
/* Consider this column and try placing
this queen in all rows one by one */
bool res = false;
/* Check if queen can be placed on
board[i,col] */
if (isSafe(board, i, col))
/* Place this queen in board[i,col] */
board[i, col] = 1;
// Make result true if any placement
// is possible
res = solveNQUtil(board, col + 1) || res;
/* If placing queen in board[i,col]
doesn't lead to a solution, then
remove queen from board[i,col] */
board[i, col] = 0; // BACKTRACK
/* If queen can not be place in any row in
this column col then return false */
return res;