Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added nqueens test #83

Merged
merged 3 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions e2e_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ if (DEFINED BESM666__E2E_TESTS_BUILD)
besm666_e2etest_asm(./mult-test.s)
besm666_e2etest_c(./bubblesort-test.c)
besm666_e2etest_c(./primenumber-test.c)
besm666_e2etest_c(./nqueens-test.c)
besm666_e2etest_asm(./privillege-change.s)
endif()
55 changes: 55 additions & 0 deletions e2e_test/nqueens-test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#define SIZE 14 // N queens.

int board[SIZE][SIZE];
int results_count = 0;

int tryQueen(int a, int b)
{
for(int i = 0; i < a; ++i)
{
if(board[i][b])
{
return 0;
}
}

for(int i = 1; i <= a && b-i >= 0; ++i)
{
if(board[a-i][b-i])
{
return 0;
}
}

for(int i = 1; i <= a && b+i < SIZE; i++)
{
if(board[a-i][b+i])
{
return 0;
}
}

return 1;
}

void setQueen(int a)
{
for(int i = 0; i < SIZE; ++i)
{
if(tryQueen(a, i))
{
board[a][i] = 1;
setQueen(a+1);
board[a][i] = 0;
}
}

return;
}

int start()
{
setQueen(0);

return 1;
}