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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<cstdio>
#include<cstring>
using namespace std;
int array[30][30];
bool visited[30][30];
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[30];
    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);
    }
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注