i am trying to make a knight tour all the position in 64 moves from any position... i used recurvise backtracking... having prob...can some check my code and tell me the mistake...
MY CODE :
#include<stdio.h>
int moves[65][2]={0}; //the co-ordinate of each move
int position[9][9]={0}; //the status of each block of the board
int type_moves[8][2]={{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1},{-2,1}}; //8 types of move possible by knight
int k=64;
int checker(int,int); //function to check whether the move is valid or not
void backtrack(int,int,int);
void main()
{
int i,j;
scanf("%d %d",&i,&j);
printf("running...\n");
backtrack(1,i,j); //(i,j) the starting co-ordinate
printf("ended...\n");
for(i=1;i<65;i++)
if(i%2%2==0)
printf("%2d. - (%d,%d)\n",i,moves[0],moves[1]);
else
printf("%2d. - (%d,%d) ",i,moves[0],moves[1]);
printf("\n");
}
void backtrack(int n,int i,int j)
{
int check=checker(i,j);
if(n<k && check==1) // if the move is valid.
{
position[j]=1; // changing the status of block to 1 as occupied.
moves[n][0]=i; // storing the co-ordinate.
moves[n][1]=j;
n++;
for(int m=0;m<8;m++) // for 8 moves possible
{
i+=type_moves[m][0];
j+=type_moves[m][1];
backtrack(n,i,j);
i-=type_moves[m][0];
j-=type_moves[m][1];
}
// if road is blocked... changing again to status 0 and
// removing the stored co-ordinate.
position[j]=moves[n-1][0]=moves[n-1][1]=0;
}
if(n==k) // the last move.
{
moves[n][0]=i;
moves[n][1]=j;
return;
}
}
int checker(int i,int j)
{
if(i<1||i>8||j<1||j>8||position[j]==1)
return 0;
else
return 1;
}
can any one help me reagarding this problem
Let's talk about algorithms!
Moderator: Board moderators
Jump to
- Real Time Contests and Last Minute Information
- ↳ General
- ↳ Real Time Clarification
- ↳ Fixing Mistakes
- ↳ HOWTOs
- ↳ Bugs and suggestions
- New system
- ↳ FAQ
- ↳ Bugs and suggestions
- Let's make some programs!
- ↳ Other words
- ↳ Algorithms
- ↳ New features
- Help on the Problemset
- ↳ Volume 1 (100-199)
- ↳ Volume 2 (200-299)
- ↳ Volume 3 (300-399)
- ↳ Volume 4 (400-499)
- ↳ Volume 5 (500-599)
- ↳ Volume 6 (600-699)
- ↳ Volume 7 (700-799)
- ↳ Volume 8 (800-899)
- ↳ Volume 9 (900-999)
- ↳ Volume 10 (1000-1099)
- ↳ Volume 11 (1100-1199)
- ↳ Volume 12 (1200-1299)
- ↳ Volume 13 (1300-1399)
- ↳ Volume 14 (1400-1499)
- ↳ Volume 15 (1500-1599)
- ↳ Volume 16 (1600-1699)
- ↳ Volume 17 (1700-1799)
- ↳ Volume 100 (10000-10099)
- ↳ Volume 101 (10100-10199)
- ↳ Volume 102 (10200-10299)
- ↳ Volume 103 (10300-10399)
- ↳ Volume 104 (10400-10499)
- ↳ Volume 105 (10500-10599)
- ↳ Volume 106 (10600-10699)
- ↳ Volume 107 (10700-10799)
- ↳ Volume 108 (10800-10899)
- ↳ Volume 109 (10900-10999)
- ↳ Volume 110 (11000-11099)
- ↳ Volume 111 (11100-11199)
- ↳ Volume 112 (11200-11299)
- ↳ Volume 113 (11300-11399)
- ↳ Volume 114 (11400-11499)
- ↳ Volume 115 (11500-11599)
- ↳ Volume 116 (11600-11699)
- ↳ Volume 117 (11700-11799)
- ↳ Volume 118 (11800-11899)
- ↳ Volume 119 (11900-11999)
- ↳ Volume 120 (12000-12099)
- ↳ Volume 121 (12100-12199)
- ↳ Volume 122 (12200-12299)
- ↳ Volume 123 (12300-12399)
- ↳ Volume 124 (12400-12499)
- ↳ Volume 125 (12500-12599)
- ↳ Volume 126 (12600-12699)
- ↳ Volume 127 (12700-12799)
- ↳ Volume 128 (12800-12899)
- ↳ Volume 129 (12900-12999)
- ↳ Volume 130 (13000-13099)
- ↳ Volume 131 (13100-13199)
- Help on languages
- ↳ C
- ↳ C++
- ↳ Pascal
- ↳ Java
- Off Topic
- ↳ Off topic (General chit-chat)
- Category
- ↳ ACM ICPC Archive Board