1#include <cstdio>2 using namespacestd;3 Const intb[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};4 inta[ -][ -],p,q;5 struct6 {7 intx, y;8}step[910];9 Ten BOOLDfsintXintYintNow ) One { A if(NOW==P*Q)return true; - for(intm=0;m<8; m++) - { the intdx=x+b[m][0]; - intdy=y+b[m][1]; - if(dx>=1&& dy>=1&& dx<=q && dy<=p && a[dx][dy]==0) - { +a[dx][dy]=1; -step[now].x=DX; +step[now].y=dy; A if(Dfs (dx,dy,now+1))return true; ata[dx][dy]=0; - } - } - return false; - } - in intMain () - { to intN; +scanf"%d",&n); - for(intt=1; t<=n;t++) the { *printf"Scenario #%d:\n", T); $scanf"%d%d",&p,&q);Panax Notoginseng for(intI=0; i<= -; i++) - for(intj=0; j<= -; j + +) a[i][j]=0; thea[1][1]=1; +step[0].x=1; Astep[0].y=1; the if(!dfs (1,1,1)) printf ("impossible\n\n"); + Else - { $ for(intI=0; i<p*q;i++) printf ("%c%d",(Char) step[i].x+'A'-1, step[i].y); $printf"\ n"); -printf"\ n"); - } the } - return 0;Wuyi}
Dfs/poj 2488 A Knight ' s Journey