static bool isSafe(int[,] board, int row, int col) { int i, j; for (i = 0; i < col; i++) if (board[row, i] == 1) return false; for (i = row, j = col; i >= 0 && j >= 0; i--, j--) if (board[i, j] == 1) return false; for (i = row, j = col; j >= 0 && i < 8; i++, j--) if (board[i, j] == 1) return false; return true; } int l=0; // int[,] board = new int[8, 8]; int[,] big = new int[8, 8]; bool solveNQUtil(int[,] board, int col) { if (col >= 8) { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { big[i, j] = board[i, j]; } } // x += 8; return true; } bool res = false; for (int i = 0; i < 8; i++) { if (isSafe(board, i, col)) { board[i, col] = 1; res = solveNQUtil(board, col + 1 ) || res; board[i, col] = 0; // BACKTRACK } } return res; } void solveNQ3() { int[,] board = new int[8, 8]; // solveNQUtil(board, 0); if (solveNQUtil(board, 0) == false) { // Console.Write("Solution does not exist"); return ; } return ; } public void GoiY() { solveNQ3(); /* for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (Matrix[i][j].BackgroundImage == hinhconco1) { } } }*/ for (int row =0; row <8; row++) { for (int col =0; col <8; col++) { if (big[row,col]==1 && Matrix[row][col].BackgroundImage == null) { //if (CheckNonHave(row, col) ) { Matrix[row][col].BackgroundImage = hinhconco2; Application.DoEvents(); Thread.Sleep(1000); Matrix[row][col].BackgroundImage = null; return; } } } } MessageBox.Show("Không còn đường đi. Bạn đã thua!"); return; }