update
This commit is contained in:
parent
1f1ad49427
commit
6f050a029e
2496 changed files with 37609 additions and 3031 deletions
100
Task/N-queens-problem/C++/n-queens-problem-1.cpp
Normal file
100
Task/N-queens-problem/C++/n-queens-problem-1.cpp
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
#include <windows.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
using namespace std;
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
class point
|
||||
{
|
||||
public:
|
||||
int x, y;
|
||||
point(){ x = y = 0; }
|
||||
void set( int a, int b ){ x = a; y = b; }
|
||||
};
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
class nQueens
|
||||
{
|
||||
public:
|
||||
void solve( int c )
|
||||
{
|
||||
_count = c; int len = ( c + 1 ) * ( c + 1 ); _queens = new bool[len]; memset( _queens, 0, len );
|
||||
_cl = new bool[c]; memset( _cl, 0, c ); _ln = new bool[c]; memset( _ln, 0, c );
|
||||
point pt; pt.set( rand() % c, rand() % c ); putQueens( pt, c ); displayBoard();
|
||||
delete [] _queens; delete [] _ln; delete [] _cl;
|
||||
}
|
||||
|
||||
private:
|
||||
void displayBoard()
|
||||
{
|
||||
system( "cls" ); string t = "+---+", q = "| Q |", s = "| |";
|
||||
COORD c = { 0, 0 }; HANDLE h = GetStdHandle( STD_OUTPUT_HANDLE );
|
||||
for( int y = 0, cy = 0; y < _count; y++ )
|
||||
{
|
||||
int yy = y * _count;
|
||||
for( int x = 0; x < _count; x++ )
|
||||
{
|
||||
SetConsoleCursorPosition( h, c ); cout << t;
|
||||
c.Y++; SetConsoleCursorPosition( h, c );
|
||||
if( _queens[x + yy] ) cout << q; else cout << s;
|
||||
c.Y++; SetConsoleCursorPosition( h, c );
|
||||
cout << t; c.Y = cy; c.X += 4;
|
||||
}
|
||||
cy += 2; c.X = 0; c.Y = cy;
|
||||
}
|
||||
}
|
||||
|
||||
bool checkD( int x, int y, int a, int b )
|
||||
{
|
||||
if( x < 0 || y < 0 || x >= _count || y >= _count ) return true;
|
||||
if( _queens[x + y * _count] ) return false;
|
||||
if( checkD( x + a, y + b, a, b ) ) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool check( int x, int y )
|
||||
{
|
||||
if( _ln[y] || _cl[x] ) return false;
|
||||
if( !checkD( x, y, -1, -1 ) ) return false;
|
||||
if( !checkD( x, y, 1, -1 ) ) return false;
|
||||
if( !checkD( x, y, -1, 1 ) ) return false;
|
||||
if( !checkD( x, y, 1, 1 ) ) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool putQueens( point pt, int cnt )
|
||||
{
|
||||
int it = _count;
|
||||
while( it )
|
||||
{
|
||||
if( !cnt ) return true;
|
||||
if( check( pt.x, pt.y ) )
|
||||
{
|
||||
_queens[pt.x + pt.y * _count] = _cl[pt.x] = _ln[pt.y] = true;
|
||||
point tmp = pt; if( ++tmp.x >= _count ) tmp.x = 0; if( ++tmp.y >= _count ) tmp.y = 0;
|
||||
if( putQueens( tmp, cnt - 1 ) ) return true;
|
||||
_queens[pt.x + pt.y * _count] = _cl[pt.x] = _ln[pt.y] = false;
|
||||
}
|
||||
if( ++pt.x >= _count ) pt.x = 0;
|
||||
it--;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int _count;
|
||||
bool* _queens, *_ln, *_cl;
|
||||
};
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
int main( int argc, char* argv[] )
|
||||
{
|
||||
nQueens n; int nq;
|
||||
while( true )
|
||||
{
|
||||
system( "cls" ); cout << "Enter board size bigger than 3 (0 - 3 to QUIT): "; cin >> nq;
|
||||
if( nq < 4 ) return 0; n.solve( nq ); cout << endl << endl;
|
||||
system( "pause" );
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
88
Task/N-queens-problem/C++/n-queens-problem-2.cpp
Normal file
88
Task/N-queens-problem/C++/n-queens-problem-2.cpp
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
#include <windows.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
using namespace std;
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
typedef unsigned int uint;
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
class nQueens_Heuristic
|
||||
{
|
||||
public:
|
||||
void solve( uint n ) { makeList( n ); drawBoard( n ); }
|
||||
|
||||
private:
|
||||
void drawBoard( uint n )
|
||||
{
|
||||
system( "cls" ); string t = "+---+", q = "| Q |", s = "| |";
|
||||
COORD c = { 0, 0 }; HANDLE h = GetStdHandle( STD_OUTPUT_HANDLE );
|
||||
uint w = 0;
|
||||
for( uint y = 0, cy = 0; y < n; y++ )
|
||||
{
|
||||
for( uint x = 0; x < n; x++ )
|
||||
{
|
||||
SetConsoleCursorPosition( h, c ); cout << t;
|
||||
c.Y++; SetConsoleCursorPosition( h, c );
|
||||
if( x + 1 == solution[w] ) cout << q; else cout << s;
|
||||
c.Y++; SetConsoleCursorPosition( h, c );
|
||||
cout << t; c.Y = cy; c.X += 4;
|
||||
}
|
||||
cy += 2; c.X = 0; c.Y = cy; w++;
|
||||
}
|
||||
solution.clear(); odd.clear(); evn.clear();
|
||||
}
|
||||
|
||||
void makeList( uint n )
|
||||
{
|
||||
uint r = n % 6;
|
||||
for( uint x = 1; x <= n; x++ )
|
||||
{
|
||||
if( x & 1 ) odd.push_back( x );
|
||||
else evn.push_back( x );
|
||||
}
|
||||
if( r == 2 )
|
||||
{
|
||||
swap( odd[0], odd[1] );
|
||||
odd.erase( find( odd.begin(), odd.end(), 5 ) );
|
||||
odd.push_back( 5 );
|
||||
}
|
||||
else if( r == 3 )
|
||||
{
|
||||
odd.erase( odd.begin() ); odd.erase( odd.begin() );
|
||||
odd.push_back( 1 ); odd.push_back( 3 );
|
||||
evn.erase( evn.begin() ); evn.push_back( 2 );
|
||||
}
|
||||
vector<uint>::iterator it = evn.begin();
|
||||
while( it != evn.end() )
|
||||
{
|
||||
solution.push_back( ( *it ) );
|
||||
it++;
|
||||
}
|
||||
it = odd.begin();
|
||||
while( it != odd.end() )
|
||||
{
|
||||
solution.push_back( ( *it ) );
|
||||
it++;
|
||||
}
|
||||
}
|
||||
|
||||
vector<uint> odd, evn, solution;
|
||||
};
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
int main( int argc, char* argv[] )
|
||||
{
|
||||
uint n; nQueens_Heuristic nQH;
|
||||
while( true )
|
||||
{
|
||||
cout << "Enter board size bigger than 3 (0 - 3 to QUIT): "; cin >> n;
|
||||
if( n < 4 ) return 0;
|
||||
nQH.solve( n ); cout << endl << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
31
Task/N-queens-problem/Haskell/n-queens-problem-1.hs
Normal file
31
Task/N-queens-problem/Haskell/n-queens-problem-1.hs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import Control.Monad
|
||||
import Data.List
|
||||
|
||||
-- given n, "queens n" solves the n-queens problem, returning a list of all the
|
||||
-- safe arrangements. each solution is a list of the columns where the queens are
|
||||
-- located for each row
|
||||
queens :: Int -> [[Int]]
|
||||
queens n = map fst $ foldM oneMoreQueen ([],[1..n]) [1..n] where
|
||||
|
||||
-- foldM :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
|
||||
-- foldM folds (from left to right) in the list monad, which is convenient for
|
||||
-- "nondeterminstically" finding "all possible solutions" of something. the
|
||||
-- initial value [] corresponds to the only safe arrangement of queens in 0 rows
|
||||
|
||||
-- given a safe arrangement y of queens in the first i rows, and a list of
|
||||
-- possible choices, "oneMoreQueen y _" returns a list of all the safe
|
||||
-- arrangements of queens in the first (i+1) rows along with remaining choices
|
||||
oneMoreQueen (y,d) _ = [ (x:y, d\\[x]) | x <- d, safe x y]
|
||||
|
||||
-- "safe x y" tests whether a queen at column x is safe from previous
|
||||
-- queens as recorded in y
|
||||
safe x y = and [ x /= c && x /= c + n && x /= c - n | (n,c) <- zip [1..] y]
|
||||
|
||||
-- prints what the board looks like for a solution; with an extra newline
|
||||
printSolution y = do
|
||||
let n = length y
|
||||
mapM_ (\x -> putStrLn [if z == x then 'Q' else '.' | z <- [1..n]]) y
|
||||
putStrLn ""
|
||||
|
||||
-- prints all the solutions for 6 queens
|
||||
main = mapM_ printSolution $ queens 6
|
||||
11
Task/N-queens-problem/Haskell/n-queens-problem-2.hs
Normal file
11
Task/N-queens-problem/Haskell/n-queens-problem-2.hs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import Control.Monad (foldM)
|
||||
import Data.List ((\\))
|
||||
|
||||
main :: IO ()
|
||||
main = mapM_ print $ queens 8
|
||||
|
||||
queens :: Int -> [[Int]]
|
||||
queens n = foldM f [] [1..n]
|
||||
where
|
||||
f qs k = [q:qs | q <- [1..n] \\ qs, q `notDiag` qs]
|
||||
q `notDiag` qs = and [abs (q - qi) /= i | (qi,i) <- qs `zip` [1..]]
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
n=8;cnt=1;per=Permutations[Range[n],{n}];(* All Permutations of length n *)
|
||||
Do[per[[q]]=Partition[Riffle[Reverse[Range[n]],per[[q]]],2],{q,1,Length[per]}];(* Riffled in the reverse of [range n] partitioned into pairs*)
|
||||
Do[w=Subsets[per[[t]],{2}];(* This is a full subset of the previous set of pairs taken 2 at a time *)
|
||||
tot=0;
|
||||
Do[y=Abs[w[[q,1,1]]-w[[q,2,1]]];x=Abs[w[[q,1,2]]-w[[q,2,2]]];If[x==y,tot++],{q,1,Length[w]}];(* x and y are the abs values of x1-y1 and x2-y2 if equal they are on same diagonal *)
|
||||
If[tot==0,g=Grid[Table[" ",{n},{n}],Alignment->Center,Frame->All,Spacings->{1.2,1}];(* If no clashing diagonals setup an array and print the permutation and the grid*)
|
||||
Do[g[[1,per[[t,w,1]],per[[t,w,2]]]]="Q",{w,1,n}];
|
||||
Print[cnt," ",per[[t]]," ",g];cnt++],{t,1,Length[per]}]
|
||||
23
Task/N-queens-problem/Prolog/n-queens-problem-4.pro
Normal file
23
Task/N-queens-problem/Prolog/n-queens-problem-4.pro
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
% 8 queens problem.
|
||||
% q(Row) represents a queen, allocated one per row. No rows ever clash.
|
||||
% The columns are chosen iteratively from available columns held in a
|
||||
% list, reduced with each allocation, so we need never check verticals.
|
||||
% For diagonals, we check prior to allocation whether each newly placed
|
||||
% queen will clash with any of the prior placements. This prevents
|
||||
% most invalid permutations from ever being attempted.
|
||||
can_place(_, []) :- !. % success for empty board
|
||||
can_place(q(R,C),Board) :- % check diagonals against allocated queens
|
||||
member(q(Ra,Ca), Board), abs(Ra-R) =:= abs(Ca-C), !, fail.
|
||||
can_place(_,_). % succeed if no diagonals failed
|
||||
|
||||
queens([], [], Board, Board). % found a solution
|
||||
queens([q(R)|Queens], Columns, Board, Solution) :-
|
||||
nth0(_,Columns,C,Free), can_place(q(R,C),Board), % find all solutions
|
||||
queens(Queens,Free,[q(R,C)|Board], Solution). % recursively
|
||||
|
||||
queens :-
|
||||
findall(q(N), between(0,7,N), Queens), findall(N, between(0,7,N), Columns),
|
||||
findall(B, queens(Queens, Columns, [], B), Boards), % backtrack over all
|
||||
length(Boards, Len), writef('%w solutions:\n', [Len]), % Output solutions
|
||||
member(R,Boards), reverse(R,Board), writef(' - %w\n', [Board]), fail.
|
||||
queens.
|
||||
30
Task/N-queens-problem/Racket/n-queens-problem-1.rkt
Normal file
30
Task/N-queens-problem/Racket/n-queens-problem-1.rkt
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#lang racket
|
||||
|
||||
(struct Q (x y) #:transparent)
|
||||
|
||||
;; returns true if given q1 and q2 do not conflict
|
||||
(define (safe? q1 q2)
|
||||
(match* (q1 q2)
|
||||
[((Q x1 y1) (Q x2 y2))
|
||||
(not (or (= x1 x2) (= y1 y2)
|
||||
(= (abs (- x1 x2)) (abs (- y1 y2)))))]))
|
||||
|
||||
;; returns true if given q doesn't conflict with anything in given list of qs
|
||||
(define (safe-lst? q qs) (for/and ([q2 qs]) (safe? q q2)))
|
||||
|
||||
(define (nqueens n)
|
||||
;; qs is partial solution; x y is current position to try
|
||||
(let loop ([qs null] [x 0] [y 0])
|
||||
(cond [(= (length qs) n) qs] ; found a solution
|
||||
[(>= x n) (loop qs 0 (add1 y))] ; go to next row
|
||||
[(>= y n) #f] ; current solution is invalid
|
||||
[else
|
||||
(define q (Q x y))
|
||||
(if (safe-lst? q qs) ; is current position safe?
|
||||
(or (loop (cons q qs) 0 (add1 y)) ; optimistically place a queen
|
||||
; (and move pos to next row)
|
||||
(loop qs (add1 x) y)) ; backtrack if it fails
|
||||
(loop qs (add1 x) y))])))
|
||||
|
||||
(nqueens 8)
|
||||
; => (list (Q 3 7) (Q 1 6) (Q 6 5) (Q 2 4) (Q 5 3) (Q 7 2) (Q 4 1) (Q 0 0))
|
||||
10
Task/N-queens-problem/Racket/n-queens-problem-2.rkt
Normal file
10
Task/N-queens-problem/Racket/n-queens-problem-2.rkt
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
(require htdp/show-queen)
|
||||
|
||||
(define (show-nqueens n)
|
||||
(define qs (time (nqueens n)))
|
||||
(show-queen
|
||||
(for/list ([row n])
|
||||
(for/list ([col n])
|
||||
(if (member (Q row col) qs) #t #f)))))
|
||||
|
||||
(show-nqueens 8)
|
||||
45
Task/N-queens-problem/Racket/n-queens-problem-3.rkt
Normal file
45
Task/N-queens-problem/Racket/n-queens-problem-3.rkt
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
#lang racket
|
||||
|
||||
(struct Q (x y) #:transparent)
|
||||
|
||||
(define-syntax-rule (lcons x y) (cons x (lazy y)))
|
||||
|
||||
(define (lazy-filter p? lst)
|
||||
(define flst (force lst))
|
||||
(if (null? flst) '()
|
||||
(let ([x (car flst)])
|
||||
(if (p? x)
|
||||
(lcons x (lazy-filter p? (cdr flst)))
|
||||
(lazy-filter p? (cdr flst))))))
|
||||
|
||||
(define (lazy-foldr f base lst)
|
||||
(define flst (force lst))
|
||||
(if (null? flst) base
|
||||
(f (car flst) (lazy (lazy-foldr f base (cdr flst))))))
|
||||
|
||||
(define (tails lst)
|
||||
(if (null? lst) '(())
|
||||
(cons lst (tails (cdr lst)))))
|
||||
|
||||
(define (safe? q1 q2)
|
||||
(match* (q1 q2)
|
||||
[((Q x1 y1) (Q x2 y2))
|
||||
(not (or (= x1 x2) (= y1 y2)
|
||||
(= (abs (- x1 x2)) (abs (- y1 y2)))))]))
|
||||
|
||||
(define (safe-lst? lst)
|
||||
(or (null? lst)
|
||||
(let ([q1 (car lst)])
|
||||
(for/and ([q2 (cdr lst)]) (safe? q1 q2)))))
|
||||
|
||||
(define (valid? lst) (andmap safe-lst? (tails lst)))
|
||||
|
||||
(define (nqueens n)
|
||||
(define all-possible-solutions
|
||||
(for/fold ([qss-so-far '(())]) ([row (in-range n)])
|
||||
(lazy-foldr
|
||||
(λ (qs new-qss)
|
||||
(append (for/list ([col (in-range n)]) (cons (Q row col) qs))
|
||||
new-qss))
|
||||
'() qss-so-far)))
|
||||
(lazy-filter valid? all-possible-solutions))
|
||||
2
Task/N-queens-problem/Racket/n-queens-problem-4.rkt
Normal file
2
Task/N-queens-problem/Racket/n-queens-problem-4.rkt
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
(car (nqueens 8))
|
||||
;; => (list (Q 7 3) (Q 6 1) (Q 5 6) (Q 4 2) (Q 3 5) (Q 2 7) (Q 1 4) (Q 0 0))
|
||||
101
Task/N-queens-problem/Racket/n-queens-problem-5.rkt
Normal file
101
Task/N-queens-problem/Racket/n-queens-problem-5.rkt
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
(define (force-and-print qs)
|
||||
(define forced (force qs))
|
||||
(unless (null? forced)
|
||||
(printf "~v\n" (car forced))
|
||||
(force-and-print (cdr forced))))
|
||||
|
||||
(force-and-print (nqueens 8))
|
||||
|
||||
; =>
|
||||
;(list (Q 7 3) (Q 6 1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 4 4) (Q 3 1) (Q 2 7) (Q 1 2) (Q 0 6))
|
||||
;(list (Q 7 5) (Q 6 2) (Q 5 0) (Q 4 7) (Q 3 4) (Q 2 1) (Q 1 3) (Q 0 6))
|
||||
;(list (Q 7 4) (Q 6 2) (Q 5 0) (Q 4 5) (Q 3 7) (Q 2 1) (Q 1 3) (Q 0 6))
|
||||
;(list (Q 7 3) (Q 6 1) (Q 5 7) (Q 4 5) (Q 3 0) (Q 2 2) (Q 1 4) (Q 0 6))
|
||||
;(list (Q 7 5) (Q 6 2) (Q 5 4) (Q 4 6) (Q 3 0) (Q 2 3) (Q 1 1) (Q 0 7))
|
||||
;(list (Q 7 5) (Q 6 3) (Q 5 6) (Q 4 0) (Q 3 2) (Q 2 4) (Q 1 1) (Q 0 7))
|
||||
;(list (Q 7 3) (Q 6 6) (Q 5 4) (Q 4 1) (Q 3 5) (Q 2 0) (Q 1 2) (Q 0 7))
|
||||
;(list (Q 7 4) (Q 6 6) (Q 5 1) (Q 4 5) (Q 3 2) (Q 2 0) (Q 1 3) (Q 0 7))
|
||||
56
Task/N-queens-problem/Racket/n-queens-problem-6.rkt
Normal file
56
Task/N-queens-problem/Racket/n-queens-problem-6.rkt
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
#lang racket
|
||||
(define (remove x lst)
|
||||
(for/list ([i (in-range (length lst))]
|
||||
#:when (not (= x i)))
|
||||
(list-ref lst i)))
|
||||
|
||||
(define (switch-pairs lst)
|
||||
(cond [(null? lst) '()]
|
||||
[(null? (cdr lst)) (list '() (car lst))]
|
||||
[else (append (list (cadr lst) (car lst))
|
||||
(switch-pairs (cddr lst)))]))
|
||||
|
||||
(define (switch-places a1 a2 lst)
|
||||
(for/list ([i (length lst)])
|
||||
(list-ref lst (cond [(= a1 i) a2] [(= a2 i) a1] [else i]))))
|
||||
|
||||
(define (position-queens n)
|
||||
(cond [(= 1 n) (list (list 1))]
|
||||
[(> 4 n) #f]
|
||||
[else (possible-queens n)]))
|
||||
|
||||
(define (possible-queens n)
|
||||
(define rem (remainder n 12))
|
||||
(define lst (build-list n add1))
|
||||
(define evens (filter even? lst))
|
||||
(define odds (filter odd? lst))
|
||||
(cond [(or (= rem 9) (= rem 3)) (case3or9 evens odds)]
|
||||
[(= rem 8) (case8 evens odds)]
|
||||
[(= rem 2) (case2 evens odds)]
|
||||
[else (append evens odds)]))
|
||||
|
||||
(define (case3or9 evens odds)
|
||||
(for/fold ([acum (append (cdr evens) (list (car evens)) odds)])
|
||||
([i (in-list '(1 3))])
|
||||
(append (remove (list-ref acum i) acum) (list i))))
|
||||
|
||||
(define (case8 evens odds)
|
||||
(append evens (switch-pairs odds)))
|
||||
|
||||
(define (case2 evens odds)
|
||||
(define nums (append evens odds))
|
||||
(define idx (map (λ(i) (list-ref nums i)) '(1 3 5)))
|
||||
(append (remove (caddr idx)
|
||||
(switch-places (car idx) (cadr idx) nums))
|
||||
'(5)))
|
||||
|
||||
(define (queens n)
|
||||
(define position-numbers (position-queens n))
|
||||
(define positions-on-board
|
||||
(for/list ([i n]) (cons i (sub1 (list-ref position-numbers i)))))
|
||||
(for/list ([x n])
|
||||
(for/list ([y n])
|
||||
(if (member (cons x y) positions-on-board) "Q" "."))))
|
||||
|
||||
(define (print-queens n)
|
||||
(for ([x (queens n)]) (displayln (string-join x))))
|
||||
112
Task/N-queens-problem/Run-BASIC/n-queens-problem.run
Normal file
112
Task/N-queens-problem/Run-BASIC/n-queens-problem.run
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
[loop]
|
||||
input "How many queens (N>=4)";n
|
||||
if n < 4 then
|
||||
print "Must be greater than 4"
|
||||
goto [loop]
|
||||
end if
|
||||
|
||||
dim plot$(100,100)
|
||||
dim q(n+20)
|
||||
dim e(n+20)
|
||||
dim o(n+20)
|
||||
r=n mod 6
|
||||
if r<>2 and r<>3 then
|
||||
gosub [samp]
|
||||
goto [shoBoard]
|
||||
end if
|
||||
for i=1 to int(n/2)
|
||||
e(i) = 2 * i
|
||||
next
|
||||
for i=1 to int((n/2)+.5)
|
||||
o(i) = 2 *i-1
|
||||
next
|
||||
if r = 2 then gosub [edt2]
|
||||
if r = 3 then gosub [edt3]
|
||||
s = 1
|
||||
for i=1 to n
|
||||
if e(i)>0 then
|
||||
q(s) = e(i)
|
||||
s = s+1
|
||||
end if
|
||||
next
|
||||
for i=1 to n
|
||||
if o(i) > 0 then
|
||||
q(s) = o(i)
|
||||
s = s + 1
|
||||
end if
|
||||
next
|
||||
' print board
|
||||
[shoBoard]
|
||||
cls
|
||||
for i = 1 to n
|
||||
plot$(i,26-q(i)) = "*"
|
||||
plot$(i,24-n) = chr$(96+i)
|
||||
plot$(n+1,26-i) = str$(i)
|
||||
next i
|
||||
for ii = 1 to 100
|
||||
for jj = 1 to 100
|
||||
print left$(plot$(jj,ii)+" ",1);
|
||||
next jj
|
||||
print
|
||||
next ii
|
||||
end
|
||||
|
||||
' the simple case
|
||||
[samp]
|
||||
p = 1
|
||||
for i = 1 to n
|
||||
if i mod 2=0 then
|
||||
q(p) = i
|
||||
p = p + 1
|
||||
end if
|
||||
next i
|
||||
for i = 1 to n
|
||||
if i mod 2 then
|
||||
q(p) = i
|
||||
p = p + 1
|
||||
end if
|
||||
next
|
||||
return
|
||||
' edit list when remainder is 2
|
||||
[edt2]
|
||||
for i=1 to n
|
||||
if o(i) = 3 then
|
||||
o(i) = 1
|
||||
else
|
||||
if o(i)=1 then o(i) = 3
|
||||
end if
|
||||
if o(i) = 5 then
|
||||
o(i)= o(i) -1
|
||||
else
|
||||
if o(i) = 0 then
|
||||
o(i) = 5
|
||||
return
|
||||
end if
|
||||
end if
|
||||
next
|
||||
|
||||
' edit list when remainder is 3
|
||||
[edt3]
|
||||
for i = 1 to n
|
||||
if e(i) = 2 then
|
||||
e(i) = e(i)-1
|
||||
else
|
||||
if e(i) = 0 then
|
||||
e(i) = 2
|
||||
goto [more]
|
||||
end if
|
||||
end if
|
||||
next i
|
||||
' edit list some more
|
||||
[more]
|
||||
for i = 1 to n
|
||||
if (o(i)=1 or o(i)=3) then
|
||||
o(i) = o(i)-1
|
||||
else
|
||||
if o(i) = 0 then
|
||||
o(i) = 1
|
||||
o(i+1) = 3
|
||||
return
|
||||
end if
|
||||
end if
|
||||
next
|
||||
51
Task/N-queens-problem/Seed7/n-queens-problem.seed7
Normal file
51
Task/N-queens-problem/Seed7/n-queens-problem.seed7
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
$ include "seed7_05.s7i";
|
||||
|
||||
var array integer: board is 8 times 0;
|
||||
var integer: solutionNum is 0;
|
||||
|
||||
const func boolean: safe (in integer: y) is func
|
||||
result
|
||||
var boolean: safe is TRUE;
|
||||
local
|
||||
var integer: i is 1;
|
||||
begin
|
||||
while i < y and safe do
|
||||
safe := board[y - i] <> board[y] and
|
||||
board[y - i] <> board[y] - i and
|
||||
board[y - i] <> board[y] + i;
|
||||
incr(i);
|
||||
end while;
|
||||
end func;
|
||||
|
||||
const proc: putBoard is func
|
||||
local
|
||||
var integer: y is 0;
|
||||
begin
|
||||
incr(solutionNum);
|
||||
writeln;
|
||||
writeln("Solution " <& solutionNum);
|
||||
for y range 1 to 8 do
|
||||
writeln("|_" mult pred(board[y]) <& "|Q" <& "|_" mult (8 - board[y]) <& "|");
|
||||
end for;
|
||||
end func;
|
||||
|
||||
const proc: main is func
|
||||
local
|
||||
var integer: y is 1;
|
||||
begin
|
||||
while y >= 1 do
|
||||
repeat
|
||||
incr(board[y]);
|
||||
until board[y] > 8 or safe(y);
|
||||
if board[y] <= 8 then
|
||||
if y < 8 then
|
||||
incr(y);
|
||||
board[y] := 0;
|
||||
else
|
||||
putBoard;
|
||||
end if;
|
||||
else
|
||||
decr(y);
|
||||
end if;
|
||||
end while;
|
||||
end func;
|
||||
Loading…
Add table
Add a link
Reference in a new issue