This problem, directly using DFS Search, and then somehow AC. Later read the puzzle, said that the order of the move different words may lead to timeouts, then need pruning, really interesting. Originally I was a mistake AC, HHH. The other solution is to first put a complete road in the array, input I, from the I position from the array loop output again, is really 666 of the solution Ah, sure enough can not be limited by the traditional way of thinking!
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 A BOOLDfsintXinty) { - -node.x =x; theNode.y =y; - for(inti =0; I <8; i++){ - intx = node.x + _move[i][0]; - inty = node.y + _move[i][1]; + if(0<= x && x <5&&0<= y && y <6){ - intFlag =1; + for(intj =0; J < Node.path.size (); J + +){ A if(Node.path[j] = = x*6+y) { atFlag =0; - Break; - } - } - if(flag) { -Node.path.push_back (x*6+ y);//take the next step. in - if(Node.path.size () >= -){//when path has a size equal to 30, it means that it has traveled to for(inti =0; I < node.path.size (); i++){ +cout << Node.path[i] +1<<' '; - } thecout <<Endl; * return true; $ }Panax Notoginseng if(Dfs (x, y))return true; -Node.path.pop_back ();//revert to original thenode.x-= _move[i][0]; +NODE.Y-= _move[i][1]; A } the } + - } $ return false; $ } - - intMain () { the intN; - while(CIN >> n && n! =-1){Wuyi node.path.clear (); then = n1; - intA = N/6; Wu intb = n%6; -Node.path.push_back (A *6+b); About Dfs (A, b); $ } - } - - /* A #include <iostream> + using namespace std; the int a[]={1,14,25,21,29,18,5,16,12,4,8,19,27,23,10,6,17,30,22,26,13,2,15,7,3,11,24,28,20,9}; - int main () $ { the int n; the While (cin>>n) the { the int tmp; - for (int i=0;i<30;i++) in if (a[i]==n) tmp=i; the for (int i=0;i<30;i++) the { About cout<<a[(tmp+i)%30]; the if (i==29) cout<<endl; the else cout<< ""; the } + } - } the */
Sicily 1151: Simple horse Travel problem (DFS)