add import feature
This commit is contained in:
7
test/nqueen/exported
Normal file
7
test/nqueen/exported
Normal file
@@ -0,0 +1,7 @@
|
||||
nqueen
|
||||
fn .solve 296 0 1 1
|
||||
0
|
||||
int 1 8 56
|
||||
8
|
||||
int 1 8 0
|
||||
|
||||
35
test/nqueen/nqueen.yo
Normal file
35
test/nqueen/nqueen.yo
Normal file
@@ -0,0 +1,35 @@
|
||||
package nqueen
|
||||
|
||||
export fn solve(n int) int {
|
||||
arr := [16]int{};
|
||||
return nqueen(0, 0, arr[:n]);
|
||||
};
|
||||
|
||||
fn nqueen(r,ans int, arr []int) int {
|
||||
if r == len(arr) {
|
||||
return ans + 1;
|
||||
};
|
||||
for c:=0; c < len(arr); c=c+1 {
|
||||
arr[r] = c;
|
||||
if canput(r, arr) {
|
||||
ans = nqueen(r+1, ans, arr);
|
||||
};
|
||||
};
|
||||
return ans;
|
||||
};
|
||||
|
||||
fn canput(r int, arr []int) bool {
|
||||
for i:=0; i < r; i=i+1 {
|
||||
if arr[r] == arr[i] || abs(arr[r] - arr[i]) == abs(r - i) {
|
||||
return false;
|
||||
};
|
||||
};
|
||||
return true;
|
||||
};
|
||||
|
||||
fn abs(x int) int {
|
||||
if x < 0 {
|
||||
return x * -1;
|
||||
};
|
||||
return x;
|
||||
};
|
||||
BIN
test/nqueen/obj
Normal file
BIN
test/nqueen/obj
Normal file
Binary file not shown.
Reference in New Issue
Block a user