mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
update
This commit is contained in:
parent
c76eb387ac
commit
e742280a87
@ -1,27 +1,27 @@
|
||||
#include <iostream>
|
||||
#define n 4
|
||||
#define inc_loop(var, start, stop, step) for (int var=start; var <= stop; var+=step)
|
||||
#define dec_loop(var, start, stop, step) for (int var=start; var >= stop; var-=step)
|
||||
#define inc_loop(var, start, stop) for (int var=start; var <= stop; var++)
|
||||
#define dec_loop(var, start, stop) for (int var=start; var >= stop; var--)
|
||||
void PrintSol(int Board[n][n]) {
|
||||
inc_loop(i,0,n-1,1) {
|
||||
inc_loop(j,0,n-1,1)
|
||||
inc_loop(i,0,n-1) {
|
||||
inc_loop(j,0,n-1)
|
||||
std::cout << Board[i][j] << " ";
|
||||
std::cout << std::endl;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
if (n%2==0 || (n%2==1 && Board[n/2+1][0]!=1)){
|
||||
inc_loop(i,0,n-1,1){
|
||||
dec_loop(j,n-1,0,1)
|
||||
std::cout<<Board[i][j]<<" ";
|
||||
std::cout<<std::endl;
|
||||
if (n%2 == 0 || (n%2 == 1 && Board[n/2+1][0] != 1)){
|
||||
inc_loop(i,0,n-1){
|
||||
dec_loop(j,n-1,0)
|
||||
std::cout << Board[i][j] << " ";
|
||||
std::cout << std::endl;
|
||||
}
|
||||
std::cout<<std::endl;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
bool CanIMove(int Board[n][n], int row, int col) {
|
||||
/// check in the row
|
||||
for (int i = 0; i < col; i++) {
|
||||
inc_loop (i, 0, col-1) {
|
||||
if (Board[row][i] == 1)
|
||||
return false;
|
||||
}
|
||||
@ -54,20 +54,19 @@ void NQueenSol(int Board[n][n], int col) {
|
||||
|
||||
int main() {
|
||||
int Board[n][n] = {0};
|
||||
if (n%2==0) {
|
||||
inc_loop(i,0,n/2-1,1) {
|
||||
if (n%2 == 0) {
|
||||
inc_loop(i, 0, n/2-1) {
|
||||
if (CanIMove(Board, i, 0)) {
|
||||
Board[i][0] = 1;
|
||||
NQueenSol(Board,1);
|
||||
NQueenSol(Board, 1);
|
||||
Board[i][0] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
inc_loop(i,0,n/2,1) {
|
||||
if (CanIMove(Board, i,0)) {
|
||||
} else {
|
||||
inc_loop(i, 0, n/2) {
|
||||
if (CanIMove(Board, i, 0)) {
|
||||
Board[i][0] = 1;
|
||||
NQueenSol(Board,1);
|
||||
NQueenSol(Board, 1);
|
||||
Board[i][0] = 0;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user