Home > Back-end >  Knight's tour problem accessibility heuristics have a solution
Knight's tour problem accessibility heuristics have a solution

Time:09-27

Teaching material "c + + university teaching materials" (9)
Problem sets 7.22
According to the train of thought in the book code, knight steps walked 64 63 no solution, 62 steps with five solutions
Don't know whether I thought the problem of
The code is as follows:
//KnightProblem. CPP: defines the entry point of the console application, 
//

# include "stdafx. H"
#include
using namespace std;

Int KnightTour (int a, int b) {
Int const size=8;
Int board [size] [size], horizontal [8]={2, 1, 1, 2, 2, 1,1,2}, vertical [8]={1, 2, 2, 1,1,2,2,1};
Int currentRow, currentColumn;
Int count=0;
CurrentRow=a, currentColumn=b;
Int the org.eclipse.swt.accessibility [size] [size];
For (int I=0; i For (int j=0; J & lt; The size; J++) {
If (I==0 | | I==7) {
If (j==0 | | j==7)
The org.eclipse.swt.accessibility [I] [j]=2;
Else if (j==1 | | j==6)
The org.eclipse.swt.accessibility [I] [j]=3;
The else
The org.eclipse.swt.accessibility [I] [j]=4;
}
Else if (I==1 | | I==6) {
If (j==0 | | j==7)
The org.eclipse.swt.accessibility [I] [j]=3;
Else if (j==1 | | j==6)
The org.eclipse.swt.accessibility [I] [j]=4;
The else
The org.eclipse.swt.accessibility [I] [j]=6;
}
The else {
If (j==0 | | j==7)
The org.eclipse.swt.accessibility [I] [j]=4;
Else if (j==1 | | j==6)
The org.eclipse.swt.accessibility [I] [j]=6;
The else
The org.eclipse.swt.accessibility [I] [j]=8;
}
}
}
For (int I=0; i For (int j=0; J & lt; The size; J++) {
Board [I] [j]=0;
}
}
Int willRow=0, willColumn=0, mark [8]=,0,0,0,0,0,0,0 {0};
For (int I=0; i <8; I++) {
WillRow=currentRow + horizontal [I];
WillColumn=currentColumn + vertical [I];
If (willRow & gt;=0 & amp; & WillRow & lt; 8 & amp; & WillColumn & gt;=0 & amp; & WillColumn & lt; 8 & amp; & Board [willColumn] [willRow]==0) {
Mark [I]=the org.eclipse.swt.accessibility [willColumn] [willRow];
}
If (I==7) {
Int compare=10, num=1;
For (int j=0; J & lt; 8; J++) {
If (mark [j] Compare=mark [j];
Num=j;
}
}
/* if (count==60)
Cout & lt; <"Pause"; */
The board [currentColumn] [currentRow]=+ + count;

CurrentRow +=horizontal (num);
CurrentColumn +=vertical (num);
I=1;
If (currentColumn & lt; 0 | | currentRow & lt; 0)
I=8;
For (int k=0; K & lt; 8; K++) {
Mark [k]=0;
If (currentRow + horizontal [k] & gt;=0 & amp; & CurrentRow + horizontal [k] <8 & amp; & The currentColumn + vertical [k] & gt;=0 & amp; & The currentColumn + vertical [k] <8)
The org.eclipse.swt.accessibility [currentRow + horizontal [k]] [currentColumn + vertical [k]] -=1;
}
}
}

If (count==60) {
For (int I=0; i For (int j=0; J & lt; The size; J++) {
Cout & lt; }
Cout & lt; }
Cout & lt; return 1;
}
return 0;
}

Int main ()
{
int sum=0;
For (int I=0; i <8; I++)
For (int j=0; J & lt; 8; J++) {
The sum +=KnightTour (I, j);
}
Cout & lt; return 0;
}

  • Related