More typical deep search, note 1, the final output format, I imposiable behind forget Endl, the results of PE two times, a little pity; 2. Final output in dictionary order
1#include <iostream>2#include <string.h>3 using namespacestd;4 5 #defineMAX 86 7 intp,q;8 intBoard[max][max];9 intsteps[max*MAX];Ten One intdir[8][2] = {{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}}; A - - BOOLDFS (intXintYintStep) { the - if(Step = = P *q) - return true; - + BOOLFlag =false; - intTempx,tempy; + A for(intI=0;i<8; i++) { at - -TEMPX = x + dir[i][0]; -Tempy = y + dir[i][1]; - - if(!board[tempx][tempy] && tempx < p && Tempy < q && tempx >=0&& Tempy >=0) { inBoard[tempx][tempy] =1; - if(DFS (tempx,tempy,step+1)) to { +steps[step-1] =i; -Flag =true; the Break; * } $ Else {Panax NotoginsengBoard[tempx][tempy] =0; - } the + } A the } + - returnFlag; $ $ } - - the - Wuyi intMain () { the - Wu intcount; - AboutCIN >>count; $ intnum =count; - while(num--) { - -Cin>> P >>Q; A +memset (board,0,sizeof(int) *max*MAX); thememset (Steps,0,sizeof(int) *max*MAX); -board[0][0] =1; $ thecout<<"Scenario #"<<count-num<<":"<<Endl; the the if(DFS (0,0,1)) { the stringS"A1"); - intx_offset=0, y_offset=0; in the for(intI=0; i<p*q-1; i++) { the AboutX_offset + = dir[steps[i]][0]; theY_offset + = dir[steps[i]][1]; the theS.push_back ((Char)('A'+y_offset)); +S.push_back ((Char)('1'+x_offset)); - } the Bayicout<<s<<endl<<Endl; the } the Else -cout<<"Impossible"<<endl<<Endl; - the } the the the return 0; -}
poj_2488 A Knight ' s Journey