1.POJ2488 A Knight's Journey
search
1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 #include<string> 5 using namespace std; 6 7 #define N 27 8 #define INF 0x7FFFFFFF 9 int p,q,_count=0; 10 int _dec[N][N]; 11 int routine[2*N]; 12 int t1,t2; 13 int flag,flag2; 14 15 void path(int x,int y,int k) 16 { 17 switch (k) 18 { 19 case 0:{t1 = x-1;t2 = y-2;break;} 20 case 1:{t1 = x+1;t2 = y-2;break;} 21 case 2:{t1 = x-2;t2 = y-1;break;} 22 case 3:{t1 = x+2;t2 = y-1;break;} 23 case 4:{t1 = x-2;t2 = y+1;break;} 24 case 5:{t1 = x+2;t2 = y+1;break;} 25 case 6:{t1 = x-1;t2 = y+2;break;} 26 case 7:{t1 = x+1;t2 = y+2;break;} 27 } 28 } 29 void OUTPUT() 30 { 31 for (int i=0;i<_count;i++) 32 { 33 cout<<(char)('A'+routine[2*i+1])<<routine[2*i]+1; 34 if (i==_count-1) cout<<endl; 35 } 36 } 37 bool DFS(int x,int y) 38 { 39 if (flag2 == 1) return 0; 40 _dec[x][y]=1; 41 routine[2*_count]=x; 42 routine[2*_count+1]=y; 43 _count++; 44 if (_count>p*q) 45 { 46 flag2 = 1; 47 return 0; 48 } 49 if ((_count == p*q)&&(flag == 0)) 50 { 51 OUTPUT(); 52 flag = 1; 53 return 1; 54 } 55 for (int k=0;k<8;k++) 56 { 57 path(x,y,k); 58 int xx=t1,yy = t2; 59 if ((xx>=0)&&(xx<p)&&(yy>=0)&&(yy<q)&&(_dec[xx][yy]==0)) 60 if (DFS(xx,yy)) return 1; 61 } 62 _dec[x][y]=0; 63 _count--; 64 return 0; 65 } 66 int main() 67 { 68 int n; 69 cin>>n; 70 for (int m=1;m<=n;m++) 71 { 72 for (int i=0;i<N;i++) 73 for (int j=0;j<N;j++) 74 _dec[i][j]=0; 75 memset(routine,0,sizeof(routine)); 76 cin>>p>>q; 77 cout<<"Scenario #"<<m<<":"<<endl; 78 if ((p==1)&&(q==1)) 79 { 80 cout<<"A1"<<endl<<endl; 81 continue; 82 } 83 if (p*q>26 || p>=9 || q>=9 || p<=2 || q<=2) 84 { 85 cout<<"impossible"<<endl<<endl; 86 continue; 87 } 88 _count = 0; 89 flag = 0; 90 flag2 = 0; 91 DFS(0,0); 92 if ((flag2 == 1)||(flag==0)) cout<<"impossible"<<endl; 93 cout<<endl; 94 } 95 return 0; 96 }
2.POJ 1077 Eight