tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
57
Task/N-queens-problem/Objeck/n-queens-problem.objeck
Normal file
57
Task/N-queens-problem/Objeck/n-queens-problem.objeck
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
bundle Default {
|
||||
class NQueens {
|
||||
b : static : Int[];
|
||||
s : static : Int;
|
||||
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
b := Int->New[8];
|
||||
s := 0;
|
||||
|
||||
y := 0;
|
||||
b[0] := -1;
|
||||
|
||||
while (y >= 0) {
|
||||
do {
|
||||
b[y]+=1;
|
||||
}
|
||||
while((b[y] < 8) & Unsafe(y));
|
||||
|
||||
if(b[y] < 8) {
|
||||
if (y < 7) {
|
||||
b[y + 1] := -1;
|
||||
y += 1;
|
||||
}
|
||||
else {
|
||||
PutBoard();
|
||||
};
|
||||
}
|
||||
else {
|
||||
y-=1;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
function : Unsafe(y : Int) ~ Bool {
|
||||
x := b[y];
|
||||
for(i := 1; i <= y; i+=1;) {
|
||||
t := b[y - i];
|
||||
if(t = x | t = x - i | t = x + i) {
|
||||
return true;
|
||||
};
|
||||
};
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
function : PutBoard() ~ Nil {
|
||||
IO.Console->Print("\n\nSolution ")->PrintLine(s + 1);
|
||||
s += 1;
|
||||
for(y := 0; y < 8; y+=1;) {
|
||||
for(x := 0; x < 8; x+=1;) {
|
||||
IO.Console->Print((b[y] = x) ? "|Q" : "|_");
|
||||
};
|
||||
"|"->PrintLine();
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue