How to use C++ to realize horse pedal chessboard
This article mainly introduces how to use C++ to achieve horse chessboard, the article is very detailed, has a certain reference value, interested friends must finish!
The horse stepped on the chessboard and walked all over the chessboard with one horse. I use a two-dimensional array to record the entire path of the simulation, x as the column, y as the row, to find the next grid clockwise, the algorithm is relatively simple, through recursion and circular backtracking, that is, if it is an array of 8 to 8, the worst possible execution is 8 ^ (xroomy) times, which takes a long time to doubt life.
# include#define X 5#define Y 5 void ShowResult (); using namespace std; int chess [Y] [X] = {0}; int counter=0; int Next (int* x where intt * y case where) {switch (where) {case 0: if (* x+1=0&&chess [* ylashi 2] [* xlue 1] = 0) {* x quoti1; * yLaught 2; return 1 } break; case 1: if (* x+2=0&&chess [* yMurray 1] [* xanth2] = 0) {* xanthium 2; * yMurray 1; return 1;} break Case 2: if (* x+2=0&&chess [* yMurray 2] [* xmur1] = 0) {* xmuri 1; * yMuth2; return 1;} break;} return 0;} int Explore (int x quotation int y) {int x1fux; int y1ray; int flag; int where=0; counter++ Chess [y] [x] = counter; if (counter==X*Y) {return 1;} flag=Next