POJ2488 A Knight's Journey

                                                                               A Knight's Journey

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11297   Accepted: 3773

Description

POJ2488 - 地平线 - 超越地平线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.

Input

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, . . .

Output

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.

Sample Input

3
1 1
2 3
4 3

Sample Output

Scenario #1:
A1

Scenario #2:
impossible

Scenario #3:
A1B3C1A2B4C2A3B1C3A4B2C4

Source

TUD Programming Contest 2005, Darmstadt, Germany

 

  1 #include <cstdlib>
  2 #include <iostream>
  3 #include <cstdio>
  4 #include <algorithm>
  5 #include <string.h>
  6 #include <cmath>
  7 using namespace std;
  8 int map[10][10];
  9 
 10 int move_q[8]={-2,-2,-1,-1,1,1,2,2};
 11 int move_p[8]={-1,1,-2,2,-2,2,-1,1};
 12 struct node{
 13        int x;
 14        int y;
 15        }grap[64];
 16        
 17        char s[200];
 18        int flag=0;
 19        int num;
 20        
 21 int dfs(int p,int q,int fx,int fy)
 22 {
 23     int i,j,k;
 24     int x,y;
 25     
 26     
 27     if(num==p*q)
 28     return 1;
 29     for(i=0;i<8;i++)
 30     {
 31                     x=fx+move_p[i];
 32                     y=fy+move_q[i];
 33                     if(x<1||y<1)
 34                     continue;
 35                     if(x>p||y>q)
 36                     continue;
 37                     if(map[x][y])
 38                     continue;
 39                     map[x][y]=1;
 40                     s[flag++]=(char)('A'+y-1);
 41                     s[flag++]=(char)('0'+x);
 42                     num++;
 43                     if(dfs(p,q,x,y))
 44                     return 1;
 45                     map[x][y]=0;
 46                     flag--;
 47                     flag--;
 48                     num--;
 49                     
 50     }
 51     return 0;
 52 }
 53                     
 54     
 55     
 56  
 57 
 58 
 59 
 60 int main(int argc, char *argv[])
 61 {//freopen("C:/Users/shp/Desktop/in.txt","r",stdin);
 62  //freopen("C:/Users/shp/Desktop/out.txt","w",stdout);
 63  int n;
 64  cin>>n;
 65  int i,j,k;
 66  int p,q;
 67  for(k=1;k<=n;k++)
 68  {cin>>p>>q;
 69  
 70  flag=0;
 71   for(j=1;j<=q;j++)
 72   for(i=1;i<=p;i++)
 73   {num=0;
 74    flag=0;
 75    memset(map,0,sizeof(map));
 76   s[flag++]=(char)('A'+j-1);
 77   s[flag++]=(char)('0'+i);
 78   num=1;
 79   map[i][j]=1;
 80    
 81   if(dfs(p,q,i,j))
 82   {s[flag]='\0';cout<<"Scenario #"<<k<<":"<<endl;cout<<s<<endl<<endl;goto L;}
 83   //else 
 84   //{cout<<"Scenario #"<<i<<":"<<endl;cout<<"impossible\n\n";  }   
 85   
 86   
 87   
 88    }   
 89    
 90    
 91   
 92   cout<<"Scenario #"<<k<<":"<<endl;cout<<"impossible\n\n"; 
 93   L:;
 94                   
 95                   
 96                   
 97                   
 98                   
 99                   
100  }
101  
102   
103                     
104 
105                 
106  
107    
108     system("PAUSE");
109     return EXIT_SUCCESS;
110 }
posted @ 2012-05-31 11:40  cseriscser  阅读(150)  评论(0编辑  收藏  举报