 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
whitehatmiracle@gmail.com Guest
|
Posted: Fri Jan 27, 2006 10:29 am Post subject: knight's tour helpppppp |
|
|
im stuck, thers a prob with the backtrack function.
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define size 5
void move();
void printBoard();
int checkMove();
void backtrack();
int board[5][5];
int x,y, tempx, tempy,markx=-1, marky=-1, a,step =1, row, col, flag,
chk_move;
int l[8] = {-2,-2,-1,-1,1,1,2,2};
int m[8] = {-1,1,-2,2,-2,2,-1,1};
int main()
{
clrscr();
x=0; y=0;
cout<<"Press any key to see the knight's moves to cover a whole chess
boardn";
board[x][y]=1;
move();
printBoard();
getch();
return 0;
}
void move()
{
int t=0;
while (step!=25){ /////////////////squares
flag = step;
for (int i=0, j =0; i<8, j<8; i++, j++){
tempx= x + l[i];
tempy= y + m[j];
if ( ((tempx < size) && (tempx >= 0) && (tempy < size) && (tempy >= 0)
)
&& (board[tempx][tempy] == 0 ) ) { /////// move validity check
x=tempx;
y=tempy;;
step++;
board[x][y]=step;
board[markx][marky]=0;
t=1;
// printBoard();
// getch();
if (step == 1
getch();
move();
}
}
if (flag==step){
if (t==0)
board[markx][marky]=0;
markx=x;
marky=y;
backtrack();
}
}//while
}
void backtrack()
{
// board[x][y]=0; // no steps possible, ini current position
for (int i=0, j =0; i<8, j<8; i++, j++){
tempx= x + l[i];
tempy= y + m[j];
if ( ((tempx < size) && (tempx >= 0) && (tempy < size) && (tempy >= 0)
)
&& (board[tempx][tempy] == step-1 ) ) { /////// move validity check
x=tempx;
y=tempy;;
step--;
board[x][y]=step;
break;
}
// break;
}
// move();
}
void printBoard()
{
clrscr();
cout<
for (int i=0; i<5;i++){
for (int j=0;j<5;j++)
printf("%5d",board[i][j]);
cout<
}
getch();
}
|
|
| Back to top |
|
 |
benben Guest
|
Posted: Fri Jan 27, 2006 11:33 am Post subject: Re: knight's tour helpppppp |
|
|
[email]whitehatmiracle (AT) gmail (DOT) com[/email] wrote:
| Quote: | im stuck, thers a prob with the backtrack function.
|
What sort of a prob?
Ben
|
|
| Back to top |
|
 |
Heinz Ozwirk Guest
|
Posted: Fri Jan 27, 2006 3:43 pm Post subject: Re: knight's tour helpppppp |
|
|
<whitehatmiracle (AT) gmail (DOT) com> schrieb im Newsbeitrag
news:1138357777.035897.291480 (AT) f14g2000cwb (DOT) googlegroups.com...
| Quote: | im stuck, thers a prob with the backtrack function.
#include <iostream.h
|
There is no such header (any more). Use
| Quote: | #include <conio.h
#include
#include
#include
#include
#define size 5
void move();
void printBoard();
int checkMove();
void backtrack();
int board[5][5];
|
Why did you define size if you don't use it?
| Quote: | int x,y, tempx, tempy,markx=-1, marky=-1, a,step =1, row, col, flag,
chk_move;
int l[8] = {-2,-2,-1,-1,1,1,2,2};
int m[8] = {-1,1,-2,2,-2,2,-1,1};
|
It would be better to replace these two arrays with one single array of
structs:
struct
{
int dx;
int dy;
} distance[] = {{-2, -1},{-2, 1},...};
| Quote: |
int main()
{
clrscr();
x=0; y=0;
cout<<"Press any key to see the knight's moves to cover a whole chess
boardn";
board[x][y]=1;
move();
printBoard();
getch();
return 0;
}
void move()
{
int t=0;
while (step!=25){ /////////////////squares
flag = step;
for (int i=0, j =0; i<8, j<8; i++, j++){
|
Why are you using to different variables that always have the same value?
| Quote: | tempx= x + l[i];
tempy= y + m[j];
if ( ((tempx < size) && (tempx >= 0) && (tempy < size) && (tempy >= 0)
)
&& (board[tempx][tempy] == 0 ) ) { /////// move validity check
x=tempx;
y=tempy;;
step++;
board[x][y]=step;
board[markx][marky]=0;
|
When the program comes here for the first time, markx and marky are both
less than zero. This is undefined behaviour and anything may happen from now
on.
HTH
Heinz
|
|
| Back to top |
|
 |
Gabriel Guest
|
Posted: Fri Jan 27, 2006 4:51 pm Post subject: Re: knight's tour helpppppp |
|
|
[email]whitehatmiracle (AT) gmail (DOT) com[/email] wrote:
<lots of stuff you might do in C>
I see this so often in this newsgroup. People keep using C-style arrays
and are later surprised why things get difficult. C-style arrays are
an expert feature to perform special tasks:
- calling some C APIs (though the most you can still beat with std::vector)
- performing some high-end optimizations
- nothing else that I can think of right now.
Don't.
Decide wether you want to use C or C++, but be aware that they differ
very much. If you use C++, use the STL containers.
--
Who is General Failure and why is he reading my hard disk?
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|