Skip to content

Conversation

@arsalan0c
Copy link
Owner

@arsalan0c arsalan0c commented Apr 5, 2020

This PR adds two solutions for the n-queens puzzle using the Source 3 non-deterministic language. Each of them makes use of the generate-and-test paradigm.


The first (queens_slow) uses non-determinism for generating both the row and column for each position. It does so in an optimized manner, testing the corresponding condition as soon as the choice is generated, thereby reducing the search space. However, this is not enough to yield a quick solution when N = 8. This solution also gives repeated solutions (i.e different permutations that have all the same positions) upon backtracking.


The second (queens_fast) uses non-determinism in picking only the row and not the column of each position, with the columns being fixed. This further reduces the search space, yielding a quick solution when N = 8.

Example:
image

@arsalan0c arsalan0c requested a review from anubh-v April 5, 2020 16:14
@arsalan0c arsalan0c self-assigned this Apr 5, 2020
@arsalan0c arsalan0c added the enhancement New feature or request label Apr 5, 2020
@arsalan0c arsalan0c changed the title Add nqueens solutions Add nqueens solutions (SICP JS Ex 4.35) Apr 5, 2020
require(num_queens <= board_size);
const possible_positions = enum_list(1, board_size);

const result = accumulate(
Copy link
Collaborator

@anubh-v anubh-v Apr 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason to use accumulate, other than the fact that we want to perform the process num_queens times?
If not, can \ should we use for_each ?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, accumulate gives us the positions that we have already, with the variable so_far. It is used in checking whether potential new rows and columns can be added.

@arsalan0c arsalan0c changed the title Add nqueens solutions (SICP JS Ex 4.35) Add SICP JS solutions Apr 27, 2020
@arsalan0c
Copy link
Owner Author

See source-academy#16

@arsalan0c arsalan0c closed this Apr 27, 2020
arsalan0c pushed a commit that referenced this pull request May 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants