Skip to content

Instantly share code, notes, and snippets.

@rui314
Created March 12, 2012 00:51
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rui314/2018964 to your computer and use it in GitHub Desktop.
Save rui314/2018964 to your computer and use it in GitHub Desktop.
N queen
int print_board(int board[][8]) {
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j])
printf("Q ");
else
printf(". ");
}
printf("\n");
}
printf("\n\n");
}
int conflict(int board[][8], int row, int col) {
for (int i = 0; i < row; i++) {
if (board[i][col])
return 1;
int j = row - i;
if (0 < col - j + 1)
if (board[i][col - j])
return 1;
if (col + j < 8)
if (board[i][col + j])
return 1;
}
return 0;
}
int solve(int board[][8], int row) {
if (row == 8) {
print_board(board);
return 0;
}
for (int i = 0; i < 8; i++) {
if (!conflict(board, row, i)) {
board[row][i] = 1;
solve(board, row + 1);
board[row][i] = 0;
}
}
}
int main() {
int board[64];
int i;
for (i = 0; i < 64; i++)
board[i] = 0;
solve(board, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment