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");
}
|