GithubHelp home page GithubHelp logo

rust-n-queens's Introduction

N-Queens in Rust

A super-fast, parallel backtracking algorithm for n-queens implemented in rust.

The 30,000 foot overview:

Our inner helper function that does all of the work only takes a few arguments: three integers which represent the spots on the current row that are blocked by previous queens.

The "secret sauce" here is that we can avoid passing around the board or even the locations of the previous queens and instead we use this information to infer the conflicts for the next row.

Once we know the conflicts in our current row we can simply recurse over all of the open spots and profit.

We can then use bit magic to significantly speed up the process over arrays - almost a 50x speedup.

Time for n=12: 500k ns. Highest calculated result: n=17

Please see nqueens.rs for a significantly more detailed explanation of the algorithm including a thorough explanation of all bit magic.

For the same algorithm implemented in js (about 200x slower) see my n-queens.js repo.

rust-n-queens's People

Contributors

reem avatar theptrk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

rust-n-queens's Issues

compile error

It is a little bit old, rust has changes a lot:

error: expected one of ), ,, ., ?, or an operator, found {
--> src/main.rs:120:22
|
120 | spawn(proc() {
| -^ expected one of ), ,, ., ?, or an operator
| |
| help: missing ,

error: invalid suffix u for number literal
--> src/main.rs:129:23
|
129 | let mut results = 0u;
| ^^ invalid suffix u
|
= help: the suffix must be one of the numeric types (u32, isize, f32, etc.)

error: invalid suffix u for number literal
--> src/main.rs:141:25
|
141 | for num in range(0, 9u) {
| ^^ invalid suffix u
|
= help: the suffix must be one of the numeric types (u32, isize, f32, etc.)

error: invalid suffix u for number literal
--> src/main.rs:149:25
|
149 | for num in range(0, 9u) {
| ^^ invalid suffix u
|
= help: the suffix must be one of the numeric types (u32, isize, f32, etc.)

error[E0412]: cannot find type uint in this scope
--> src/main.rs:21:24
|
21 | fn n_queens(n: i32) -> uint {
| ^^^^ not found in this scope

error[E0412]: cannot find type uint in this scope
--> src/main.rs:23:39
|
23 | return n_queens_helper((1 << n as uint) -1, 0, 0, 0);
| ^^^^ not found in this scope

error[E0412]: cannot find type uint in this scope
--> src/main.rs:45:87
|
45 | fn n_queens_helper(all_ones: i32, left_diags: i32, columns: i32, right_diags: i32) -> uint {
| ^^^^ not found in this scope

error[E0412]: cannot find type uint in this scope
--> src/main.rs:100:50
|
100 | return solutions + ((columns == all_ones) as uint)
| ^^^^ not found in this scope

error[E0412]: cannot find type uint in this scope
--> src/main.rs:108:33
|
108 | fn parallel_n_queens(n: i32) -> uint {
| ^^^^ not found in this scope

error[E0412]: cannot find type uint in this scope
--> src/main.rs:109:31
|
109 | let all_ones = (1 << n as uint) - 1;
| ^^^^ not found in this scope

error[E0425]: cannot find function channel in this scope
--> src/main.rs:115:20
|
115 | let (tx, rx) = channel();
| ^^^^^^^ not found in this scope
|
help: consider importing this function
|
4 | use std::sync::mpsc::channel;
|

error[E0425]: cannot find function spawn in this scope
--> src/main.rs:120:9
|
120 | spawn(proc() {
| ^^^^^ not found in this scope
|
help: consider importing this function
|
4 | use std::thread::spawn;
|

error[E0425]: cannot find function proc in this scope
--> src/main.rs:120:15
|
120 | spawn(proc() {
| ^^^^ not found in this scope

error[E0412]: cannot find type uint in this scope
--> src/main.rs:130:38
|
130 | for value in rx.iter().take(n as uint) {
| ^^^^ not found in this scope

error[E0412]: cannot find type uint in this scope
--> src/main.rs:133:48
|
133 | return results + ((columns == all_ones) as uint)
| ^^^^ not found in this scope

error: use of unstable library feature 'test': bench is a part of custom test frameworks which are unstable
--> src/main.rs:154:3
|
154 | #[bench]
| ^^^^^
|
= note: #[deny(soft_unstable)] on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #64266 rust-lang/rust#64266

error: use of unstable library feature 'test': bench is a part of custom test frameworks which are unstable
--> src/main.rs:159:3
|
159 | #[bench]
| ^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #64266 rust-lang/rust#64266

warning: unnecessary parentheses around function argument
--> src/main.rs:90:13
|
90 | (columns | spot),
| ^ ^
|
= note: #[warn(unused_parens)] on by default
help: remove these parentheses
|
90 - (columns | spot),
90 + columns | spot,
|

warning: unnecessary parentheses around function argument
--> src/main.rs:124:17
|
124 | (columns | spot),
| ^ ^
|
help: remove these parentheses
|
124 - (columns | spot),
124 + columns | spot,
|

Some errors have detailed explanations: E0412, E0425.
For more information about an error, try rustc --explain E0412.
warning: nqueens (bin "nqueens") generated 2 warnings
error: could not compile nqueens due to 17 previous errors; 2 warnings emitted

Thanks,

Jianshu

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.