DES: Gives a chessboard of n rows M-column. The horse goes with the L-shaped. Ask if you can start every position on the board from a certain location. If you can continue to output a path with the smallest dictionary order.
Very typical of Dfs. Search by the dictionary order from small to large. Stop searching for the output path when you find the first one. Feel Dfs very witty. WA a few times because save the answer there is no backtracking .... At first I thought I didn't understand the Chu Xing and the column which is denoted by the letter .... T_t .... It's hard to read a question .....
A Knight ' s Journey
#include <stdio.h>#include<iostream>#include<string.h>using namespacestd;BOOLFlag;intvis[ -][ -];intx[Bayi], y[Bayi];intN, M;intDx[] = {-1,1, -2,2, -2,2, -1,1}, dy[]= { -2, -2, -1, -1,1,1,2,2};//Note OrderintCNT;voidDfsintXxxintyyy) { if(Flag = =false)return; if(cnt = = Nm) { for(intI=0; i<cnt; ++i) {cout<<Char(y[i]+'A') <<Char(x[i]+'1'); } cout<< Endl <<Endl; Flag=false; return; } for(intI=0; i<8; ++i) {intxx = xxx+Dx[i]; intyy = yyy+Dy[i]; if(xx>=0&& xx<n && yy>=0&& yy<m &&!Vis[xx][yy]) {Vis[xx][yy]=1; X[CNT]=xx; Y[cnt++] =yy; DFS (xx, yy); CNT--; VIS[XX][YY]=0; } }}/*void dfs2 (int xxx, int yyy, int num) {if (flag = = false) return; if (num = = n*m) {for (int i=0; i<cnt; ++i) {cout << char (y[i]+ ' A ') << char (x[ i]+ ' 1 '); } cout << Endl << Endl; Flag = false; Return } for (int i=0; i<8; ++i) {int xx = xxx+dx[i]; int yy = Yyy+dy[i]; if (xx>=0 && xx<n && yy>=0 && yy<m &&!vis[xx][yy]) {vis[xx ][YY] = 1; X[num] = XX; Y[num] = yy; DFS2 (xx, yy, num+1); VIS[XX][YY] = 0; } }}*/intMain () {intT; CIN>>T; intCasee =0; while(t--) {Casee++; CIN>> N >>m; Flag=true; cout<<"Scenario #"<< Casee <<':'<<Endl; for(intj=0; j<m; ++j) { for(intI=0; i<n; ++i) {memset (Vis,0,sizeof(VIS)); memset (x,0,sizeof(x)); memset (Y,0,sizeof(y)); VIS[I][J]=1; x[0] =i; y[0] =J; CNT=1; DFS (I, j); //DFS2 (I, J, 1); if(!flag) Break; } if(!flag) Break; } if(flag) cout<<"impossible\n\n"; } return 0;}
View Code
POJ 2488 DFS