Solved

c++ knights tour

Posted on 1998-03-31
8
1,163 Views
Last Modified: 2012-06-21
what is the best way to code the knights tour ( recusively etc. )?
does any one have the code for the the next best move one?
0
Comment
Question by:cart123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1183980
Always taking the branch with the minimum number of moves seems to be a simple and effective search heuristic.
0
 

Author Comment

by:cart123
ID: 1183981
Edited text of question
0
 

Author Comment

by:cart123
ID: 1183982
Edited text of question
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:Srw
ID: 1183983
It seems to me that the easiest way to write it would be recursively.  I'm sure you can do it iteratively too, but I'd think the recursive solution is more elegant.

As for finding the next *best* move, you'd need to do some looking ahead.  But, looking ahead is more or less what the recursive method does.

Although, just thinking about it, I would seem that if I was on move 5, and a possible move 6 would dead-end me, then move 5 was bad too.  If after the proposed move 6, there is no place to go, then given an alternate move 6, you would never be able to get back to the target square of the initial move 6, hence move 5 must be bad too.  Think about it a little, I don't see any flaws in this thought, maybe someone else does.
0
 

Author Comment

by:cart123
ID: 1183984
give me some code
0
 
LVL 2

Expert Comment

by:Srw
ID: 1183985
The Knight's tour is a common problem often assigned in programming classes.  I doubt you'll find anybody here to write your class assignments for you.

They'll be happy to give general advice and discuss the merits of various algorithms, but you're on your own to write the program.

If you have a specific question about part of the program, or a question about recursion, ask away.  Everybody here is glad to answer those questions.
0
 
LVL 2

Expert Comment

by:mitchell042997
ID: 1183986
Tee hee!  What university do you go to?  This was an assignment for a class called Data Structures I at my school.  :)
0
 
LVL 1

Accepted Solution

by:
issamwd earned 50 total points
ID: 1183987
The Knight's tour is solved using the "Back tracking algorithm ", this algorithm is a systematic , exhaustive search of the solution .
read the algorithm carfully & iplement it by C++
declaration of variables are left to your capabilities & knowledge in C++.
more information about back tracking algorithm are found in (design of algorithms) books
best wishes

Data representation:

chess board :    matrix ( h )
we need also a boolean value denoting the occupation of the board .

Algorithm (pseudocode):

const n = 8 ; /* size of board */
const nSqr = n * n ;

h = array [1..n ,1..n] of integers ; // int h[8,8]

function to be used :


void TryMove(i, x , y ,q) // i : integer records move number
                                   // x,y two dimentional array index
                                    // q : boolean variable
{
initialize selection of moves;
   repeat until ( move was successful or no more candidates)
       {
       set moves that was not successful // try all moves
       select next candidate move from a list contains next possible moves
       if candidate accepted then
                {
                record move  # i
                if reached the last move
                      set move was succesful
                else
                       {
                       TryMove(i+1 , v ,u , q2) // recursive
                        if not successful then
                                 erase move #i recorded
                        }
                }
   }
}  
       

 
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Getting IP address 8 126
C++ Class Serialization, Encapsulation and Formatted Output 8 110
Dialogbox API leak? 18 119
Are the two lines the same? If not what is the difference? 2 31
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question