# Openjudge A Knight’s Journey

## A Knight’s Journey

1000ms

65536kB Background
The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey
around the world. Whenever a knight moves, it is two squares in one direction and one square perpendicular to this. The world of a knight is the chessboard he is living on. Our knight lives on a chessboard that has a smaller area than a regular 8 * 8 board, but it is still rectangular. Can you help this adventurous knight to make travel plans?Problem
Find a path such that the knight visits every square once. The knight can start and end on any square of the board.

The input begins with a positive integer n in the first line. The following lines contain n test cases. Each test case consists of a single line with two positive integers p and q, such that 1 <= p * q <= 26. This represents a p * q chessboard, where p describes how many different square numbers 1, . . . , p exist, q describes how many different square letters exist. These are the first q letters of the Latin alphabet: A, . . .

The output for every scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Then print a single line containing the lexicographically first path that visits all squares of the chessboard with knight moves followed by an empty line. The path should be given on a single line by concatenating the names of the visited squares. Each square name consists of a capital letter followed by a number.
If no such path exist, you should output impossible on a single line.

3
1 1
2 3
4 3

Scenario #1:
A1

Scenario #2:
impossible

Scenario #3:
A1B3C1A2B4C2A3B1C3A4B2C4


TUD Programming Contest 2005, Darmstadt, Germany

 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 #include #include using namespace std; int array; bool visited; int p,q,n; bool checkfull(){ //??     for(int i=1;i<=p;i++){         for(int j=1;j<=q;j++){             if(visited[i][j]==false)return false;         }     }     return true; } bool DFS(int i,int j,int cur){     if(i>0&&j>0&&i<=q&&j<=p&&(!visited[i][j])){         visited[i][j]=true;         array[i][j]=cur;         if(cur==p*q)return true;         if(DFS(i-2,j-1,cur+1))return true;         else if(DFS(i-2,j+1,cur+1))return true;         else if(DFS(i-1,j-2,cur+1))return true;         else if(DFS(i-1,j+2,cur+1))return true;         else if(DFS(i+1,j-2,cur+1))return true;         else if(DFS(i+1,j+2,cur+1))return true;         else if(DFS(i+2,j-1,cur+1))return true;         else if(DFS(i+2,j+1,cur+1))return true;         visited[i][j]=false;         array[i][j]=0;         return false;     }else return false; } bool dfs(){     for(int i=1;i<=q;i++){         for(int j=1;j<=p;j++){             if(DFS(i,j,1)){                 return true;             }         }     }     return false; } void output(int k){     struct R{         int i;         int j;     }Result;     for(int i=1;i<=q;i++){         for(int j=1;j<=p;j++){             Result[array[i][j]].i=i;             Result[array[i][j]].j=j;         }     }     printf("Scenario #%d:\n",k);     for(int i=1;i<=p*q;i++){         printf("%c%d",'A'+Result[i].i-1,Result[i].j);     }     printf("\n\n"); } int main(){     scanf("%d",&n);     for(int i=1;i<=n;i++){         scanf("%d %d",&p,&q);         memset(array,0,sizeof(array));         memset(visited,0,sizeof(visited));         if(dfs())output(i);         else printf("Scenario #%d:\nimpossible\n\n",i);     } }