Oct 23, 2013 at 7:12pm Oct 23, 2013 at 7:12pm UTC
Hi,
I'm working with a code that I wrote for knight tour. After I resolved all the errors I tried to run the program, but it doesn't stop to show the result like an infinite loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int const s=8;
int c;
int d;
int e;
int x1;
int y1;
int x2;
int y2;
int m=1;
int a;
int b=0;
int bwt[s+1][s+1]={0};
int cb[s][s]={0};
int bt[s][s]={0};
int mx[s] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int my[s] = { 1, 2, 2, 1, -1, -2, -2, -1 };
cout <<"enter the first knight location\n" ;
cout<<"enter x1: " ;
cin>>x1;
cout<<"\nenter y1: " ;
cin>>y1;
x2=x1;
y2=y1;
while (m<65)
{
while (b<=7)
{
for (a=b;a<b+1;a++)
if (x2+mx[b]>=0&&x2+mx[b]<8&&y2+my[b]>=0&&y2+my[b]<8&&cb[x2+mx[b]][y2+my[b]]==0)
{
cb[x2][y2]=m;
bt[x2][y2]=b+1;
bwt[x2+mx[b]][y2+my[b]]=b;
x2=x2+mx[b];
y2=y2+my[b];
b=bt[x2][y2];
m=m+1;
}
b=b+1;
}
if (b>7)
{
e=bwt[x2][y2];
cb[x2][y2]=0;
bt[x2][y2]=0;
bwt[x2][y2]=0;
x2=x2-mx[e];
y2=y2-my[e];
b=bt[x2][y2];
m=cb[x2][y2];
}
for (c=0;c<s;c++)
{
for (d=0;d<s;d++)
cout<<setw( 2 )<<cb[c][d]<<" " ;
cout<<endl;
}
cout<<endl;
}
system("pause" );
}
Does anyone know what the problem is?
Any help is welcome.
Cheers,
Domo
Last edited on Nov 2, 2013 at 4:14pm Nov 2, 2013 at 4:14pm UTC
Oct 23, 2013 at 7:12pm Oct 23, 2013 at 7:12pm UTC
Have you tried debugging it?
Nov 2, 2013 at 4:14pm Nov 2, 2013 at 4:14pm UTC
yes , but there is no bug in the software and and although i couldn't find any infinite loop , it keeps running with no result and no stops
Last edited on Nov 2, 2013 at 4:19pm Nov 2, 2013 at 4:19pm UTC
Nov 3, 2013 at 1:00am Nov 3, 2013 at 1:00am UTC
Well, it would help if the variable names even somewhat described what they are doing. Looking at this, it almost looks like a coded message.