Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

**Knight's Tour**

AlexrAlexr Member Posts: 6
This program deals with "Knight's Tour" problem. Have you heard of it? This problem concerns with the movements of a Knight on a chessboard(L-shaped). The board I have already defined to be a constant 2 Dimensional Array. The user is prompted for a starting location for the knight anywhere on the board.
So for example if they say 7 1, that would mean row 7 and column 1 on the board. Now the knight is suppose to move to as many possible positions on the board as possible starting from that position defined by user(on a one way trip, with no square being landed on twice). The box the Knight starts in is given a inital value of 1, and the next box he lands on is 2, and so on until he gets stuck. All the boxes he lands on in his trip is given a corresponding value, and a new Array Matrix board with all the spots with their values on it is displayed. The following is what I have done so far. The first two functions are completed and is ok. Function GetMove determines the location of the knight's next move and this is not called by the main function but by function CreateBoard. The following is what I have done so far is all correct. I am just not sure how to do the last two functions.


#include
#include "UTILITY2.H"

typedef int Matrix[12][12];
const int ACCESS [12][12]=
{{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,2,3,4,4,4,4,3,2,0,0},
{0,0,3,4,6,6,6,6,4,3,0,0},
{0,0,4,6,8,8,8,8,6,4,0,0},
{0,0,4,6,8,8,8,8,6,4,0,0},
{0,0,4,6,8,8,8,8,6,4,0,0},
{0,0,4,6,8,8,8,8,6,4,0,0},
{0,0,3,4,6,6,6,6,4,3,0,0},
{0,0,2,3,4,4,4,4,3,2,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0}};

void InitializedBoard(Matrix Board, int &StartRow, int &StartCol);
void CreateBoard(Matrix Board, int StartRow, int StartCol, int &N);
void DisplayBoard(Matrix Board, int StartRow, int StartCol, int N);

void main()
{
Matrix Board;
int StartRow, StartCol;
int N;
OutputSelection();
Heading H("25B", "09-09-02", "100", "KNIGHT'S TOUR");
Execution();
do
{
InitializeBoard(Board, StartRow, StartCol);
CreateBoard(Board,StartRow,StartCol,N);
DisplayBoard(Board,StartRow,StartCol,N);
}
while (NotFinished());
Terminate();
}

void InitializedBoard(Matrix Board, int &S tartRow, int &StartCol)
{
Skip(4);
Title("INITIALIZE BOARD FUNCTION");
Skip(2);
int Row, Col;
cout<<"Enter starting Row and Col values ===>> ";
cin>>StartRow>>StartCol;
cout<<"Enter starting Row and Col values ===>> "
<<StartRow<<" "<<StartCol<<endl;
StartRow++; StartCol++;
for (Row=0; Row<=11; Row++)
for (Col=0; Col<=11; Col++)
Board[Row][Col} = -1;
for (Row=2; Row<=9; Row++)
for (Col=2; Col<=9; Col++)
Board[Row][Col]=0;
}

void DisplayBoard(Matrix Board, int StartRow, int StartCol, int N)
{
Skip(4);
Title("DISPLAY BOARD FUNCTION");
Skip(2);
int Row, Col;
for (Row=2; Row<=9; Row++)
{
for (Col=2; Col<=9; Col++)
cout<<setw(4)<<Board[Row][Col];
cout<<endl;
}
Skip(2);
StartRow--;
StartCol--;
cout<<"From position ["<<StartRow<<"]["<<StartCol<<"]"
<<" there are "<<N<<" moves possible"<<endl;
getch();
}

void GetMove(Matrix Board, int &Row, int &Col, bool &Possible)
{

}

void CreateBoard(Matrix Board, int StartRow, int StartCol, int &N)
{
Skip(4);
Title("Create Board Function");
Skip(2);
for (
void GetMove(Matrix Board, int &Row, int &Col, bool &Possible)

}



An example of the result would be:
Enter starting Row and Col values ===>> 6 2

15 4 17 8 13 28 0 30
18 9 14 3 24 31 26 0
5 16 7 12 27 0 29 0
10 19 2 23 32 25 0 0
35 6 11 0 0 0 0 0
20 1 22 33 0 0 0 0
0 34 0 0 0 0 0 0
0 21 0 0 0 0 0 0

From position [6][2] there are 35 moves possible

Comments

  • Andre YoungAndre Young USAMember Posts: 0

    _______ / http://forcoder.org / free video tutorials and ebooks about || Ruby, Scratch, Perl, Swift, C++, Delphi, PL/SQL, Objective-C, Visual Basic .NET, Assembly, PHP, C, JavaScript, Python, Go, C#, Java, R, Visual Basic, MATLAB F#, FoxPro, Lisp, Awk, Apex, Fortran, ABAP, Erlang, Logo, Alice, VBScript, Rust, Bash, COBOL, Hack, Julia, SAS, D, Crystal, Scheme, Ada, Kotlin, Transact-SQL, Lua, Scala, ML, Dart, Prolog, LabVIEW, Clojure || ________

Sign In or Register to comment.