mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-15 03:10:07 +08:00
formatting source-code for 153fb7b8a5
This commit is contained in:
@@ -2,18 +2,14 @@
|
||||
#define n 4
|
||||
#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)
|
||||
{
|
||||
void PrintSol(int Board[n][n]) {
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
@@ -21,40 +17,32 @@ void PrintSol(int Board[n][n])
|
||||
}
|
||||
}
|
||||
|
||||
bool CanIMove(int Board[n][n], int row, int col)
|
||||
{
|
||||
bool CanIMove(int Board[n][n], int row, int col) {
|
||||
/// check in the row
|
||||
inc_loop(i, 0, col - 1)
|
||||
{
|
||||
inc_loop(i, 0, col - 1) {
|
||||
if (Board[row][i] == 1)
|
||||
return false;
|
||||
}
|
||||
/// check the first diagonal
|
||||
for (int i = row, j = col; i >= 0 && j >= 0; i--, j--)
|
||||
{
|
||||
for (int i = row, j = col; i >= 0 && j >= 0; i--, j--) {
|
||||
if (Board[i][j] == 1)
|
||||
return false;
|
||||
}
|
||||
/// check the second diagonal
|
||||
for (int i = row, j = col; i <= n - 1 && j >= 0; i++, j--)
|
||||
{
|
||||
for (int i = row, j = col; i <= n - 1 && j >= 0; i++, j--) {
|
||||
if (Board[i][j] == 1)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void NQueenSol(int Board[n][n], int col)
|
||||
{
|
||||
if (col >= n)
|
||||
{
|
||||
void NQueenSol(int Board[n][n], int col) {
|
||||
if (col >= n) {
|
||||
PrintSol(Board);
|
||||
return;
|
||||
}
|
||||
inc_loop(i, 0, n - 1)
|
||||
{
|
||||
if (CanIMove(Board, i, col))
|
||||
{
|
||||
inc_loop(i, 0, n - 1) {
|
||||
if (CanIMove(Board, i, col)) {
|
||||
Board[i][col] = 1;
|
||||
NQueenSol(Board, col + 1);
|
||||
Board[i][col] = 0;
|
||||
@@ -62,27 +50,19 @@ void NQueenSol(int Board[n][n], int col)
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int Board[n][n] = {0};
|
||||
if (n % 2 == 0)
|
||||
{
|
||||
inc_loop(i, 0, n / 2 - 1)
|
||||
{
|
||||
if (CanIMove(Board, i, 0))
|
||||
{
|
||||
if (n % 2 == 0) {
|
||||
inc_loop(i, 0, n / 2 - 1) {
|
||||
if (CanIMove(Board, i, 0)) {
|
||||
Board[i][0] = 1;
|
||||
NQueenSol(Board, 1);
|
||||
Board[i][0] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
inc_loop(i, 0, n / 2)
|
||||
{
|
||||
if (CanIMove(Board, i, 0))
|
||||
{
|
||||
} else {
|
||||
inc_loop(i, 0, n / 2) {
|
||||
if (CanIMove(Board, i, 0)) {
|
||||
Board[i][0] = 1;
|
||||
NQueenSol(Board, 1);
|
||||
Board[i][0] = 0;
|
||||
|
||||
Reference in New Issue
Block a user