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
b8bcd82c26
commit
c76eb387ac
@ -2,20 +2,15 @@
|
||||
#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)
|
||||
|
||||
void PrintSol(int Board[n][n])
|
||||
{
|
||||
inc_loop(i,0,n-1,1)
|
||||
{
|
||||
void PrintSol(int Board[n][n]) {
|
||||
inc_loop(i,0,n-1,1) {
|
||||
inc_loop(j,0,n-1,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)
|
||||
{
|
||||
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;
|
||||
@ -48,8 +43,7 @@ void NQueenSol(int Board[n][n], int col) {
|
||||
PrintSol(Board);
|
||||
return;
|
||||
}
|
||||
inc_loop(i,0,n-1,1)
|
||||
{
|
||||
inc_loop(i,0,n-1,1) {
|
||||
if (CanIMove(Board, i, col)) {
|
||||
Board[i][col] = 1;
|
||||
NQueenSol(Board, col + 1);
|
||||
@ -60,10 +54,8 @@ 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,1) {
|
||||
if (CanIMove(Board, i, 0)) {
|
||||
Board[i][0] = 1;
|
||||
NQueenSol(Board,1);
|
||||
@ -71,10 +63,8 @@ int main() {
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
inc_loop(i,0,n/2,1)
|
||||
{
|
||||
else {
|
||||
inc_loop(i,0,n/2,1) {
|
||||
if (CanIMove(Board, i,0)) {
|
||||
Board[i][0] = 1;
|
||||
NQueenSol(Board,1);
|
||||
|
Loading…
Reference in New Issue
Block a user