This problem did not find a circuit, so can not be used with 11,521 array storage after output. I used the method is DFS plus pruning, the direct DFS search will time out, the optimization method is in the search is a priority to get out of the small path, such as move1 and Move2 can walk, but as the move1 after the next step there are 7 ways to walk, and go after the move2 there are 2 ways to go, Then we'll take the priority move2, the specific implementation of the reference code:
1#include <bits/stdc++.h>2 using namespacestd;3 int_move[8][2] ={{1, -2}, {2, -1}, {2,1}, {1,2},{-1,2}, {-2,1}, {-2, -1}, {-1, -2}};4 5 6 structnode{7 intx, y;8vector<int>path;9 };Ten node node; One structmove{ A intx, y; - intdegree; - }; the - voidGetdegree (Move &move) { - intAns =0; - intcur_x = node.x+ move.x;//where to next + intCur_y = Node.y +move.y; - + for(inti =0; I <8; i++) {//take cur_x, cur_y as the starting point, check out the degree A at intx = cur_x + _move[i][0]; - inty = cur_y + _move[i][1]; - if(0<= x && x <8&&0<= y && y <8){//if the coordinates are not out of bounds and are not repeated, a - intFlag =1; - for(intj =0; J < Node.path.size (); J + +){ - if(Node.path[j] = = x*8+y) { inFlag =0; - Break; to } + } - the if(flag) ans++; * } $ Panax Notoginseng } -Move.degree =ans; the } + A BOOLCMP (move M1, move m2) { the returnM1.degree <M2.degree; + } - BOOLDfsintXinty) { $ $node.x =x; -Node.y =y; -Move move[8]; the for(inti =0; I <8; i++){ -move[i].x = _move[i][0];WuyiMOVE[I].Y = _move[i][1]; the if(0<= node.x + _move[i][0] && node.x + _move[i][0] <8&& - 0<= Node.y + _move[i][1] && Node.y + _move[i][1] <8) WuGetdegree (Move[i]);//If the next step is not out of bounds, get its degree - ElseMove[i].degree = -;//if the next step out of bounds, the degree is set to About } $Sort (move, move+8, CMP);//sort out from small to large - for(inti =0; I <8; i++){ - if(Move[i].degree = = -)Continue;//if the out degree is 100, skip - intx = node.x + move[i].x;//Next Position A inty = node.y +move[i].y; + if(0<= x && x <8&&0<= y && y <8){ the intFlag =1; - for(intj =0; J < Node.path.size (); J + +){ $ if(Node.path[j] = = x*8+y) { theFlag =0; the Break; the } the } - if(flag) { inNode.path.push_back (x*8+ y);//take the next step. the the if(Node.path.size () >= -){//when path has a size equal to 64, it means that it has traveled About for(inti =0; I < node.path.size (); i++){ thecout << Node.path[i] +1<<' '; the } thecout <<Endl; + return true; - } the if(Dfs (x, y))return true;BayiNode.path.pop_back ();//revert to original theNode.x-=move[i].x; theNODE.Y-=move[i].y; - } - } the the } the return false; the } - the intMain () { the intN; the while(CIN >> n && n! =-1){94 node.path.clear (); then = n1; the intA = N/8; the intb = n%8;98Node.path.push_back (A *8+b); About Dfs (A, b); - }101}
Sicily 1153: The horse's travel problem (dfs+ pruning)