Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help on backtracking, recursion

Status
Not open for further replies.

yozzz

Programmer
Dec 4, 2002
1
LT
Problem : Finding minimum knights needed to cover a NxN chess board.

NxN chess board has NxN squares which i numbered from 0 to NxN - 1. I've defined the following predicates.

allKnightsAttack(N, KnightsList, AttackedSquaresList)

this predicate gives Attacked Squares List.

Example:
if N = 4, KnightsList = [0, 5] gives us :
AttackedSquaresList = [3,11,12,14,5,6,9,0].
Graphical equivalent:
X--O
-XO-
-O-O
O-O-
X - knight, O - attacked square(squares held by knights are also "attacked").


next(KnightsList, Next, N)

this predicate given a KnightsList finds "next" list, which is found like that :
/* this KnightList's ALWAYS ASCENDING */
if last knight position < NxN-1 then increase its position;
if it reached NxN-1 then increase second last's knights position, and the last becomes next to second last and so on.

Examples: N = 4.
[0, 1, 2, 3] - next is [0, 1, 2, 4]
[0, 1, 2, 15] - next is [0, 1, 3, 4]
[0, 5, 14, 15] - next is [0, 6, 7, 8]
.........

/* with next you can get every position of knights placed on the board - if starting list [0,1,2,3] and N=4, &quot;next'ing&quot; to [12, 13, 14, 15] would give us all possible positions of four knights on a 4x4 board */

I also defined a few predicates to operate lists : removing adding, concatenating ...

I've done a solution which tells if a set of knights IS covering a board(TRUE/FALSE).

Can anyone help to define a predicate
knights(N, Knights)

which given N would output a set of Knights which cover a NxN board.

Example :
goal: knights(3, Knights)
Knights = [0,1,2,4].

XXX
-X-
---






 
try doing a search on the knights tour problem
there are a few solutions around which my help
solve your problem
ie save you from re-inventing the wheel

I have seen/got some from somewhere on the net
try colin barker at wannado
or google search on chess OR knights toour
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top