1. 程式人生 > >POJ 2488 A Knight's Journey

POJ 2488 A Knight's Journey

lag 圖片 字典 api possible b2c scribe plan png

POJ 2488 A Knight‘s Journey

技術分享圖片

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

題目大意:bfs求最短路問題,在棋盤上騎士要走遍棋盤上所有的位置,如果可以走完輸出按字典序!!!排序的路徑,否則輸出impossible,這裏特別註意國際象棋中騎士是按日字跳躍的,所以有8個方向可以行走,這裏要輸出按字典序排序的路徑,那麽可以直接找出所有可以到達的路徑再進行排序,但是這樣會超時,因為當棋盤很大的時候,走法很多。那麽其實有個更巧妙的辦法,那就是開始的時候把跳躍的8個方向的順序按字典序排序,那麽第一次成功時的路徑就是字典序最小的路徑。

int dx[] = {-1, 1, -2, 2, -2, 2, -1, 1};//按字典順序定義好行走方向
int dy[] = {-2, -2, -1, -1, 1, 1, 2, 2};

代碼:

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
const int N = 30;
typedef struct {
    int x;
    int y;
} P;
P que[1000];
int cur = 0; 
int n, m;
int vis[N][N];
int dx[] = {-1, 1, -2, 2, -2, 2, -1, 1};//按字典順序定義好行走方向 
int dy[] = {-2, -2, -1, -1, 1, 1, 2, 2};
int dfs(int  x, int y, int num) {
    int flag = 0;
    if(num == n*m) {
        printf("A1");
        for(int i = 0; i < cur; i++) {
            printf("%c%d", A+que[i].y, que[i].x + 1);
        }
        printf("\n\n");
        return 1;
    }
    for(int i = 0; i < 8; i++) {
        int nx = x + dx[i], ny = y + dy[i];
        if(nx >= 0 && nx < n && ny >= 0 && ny < m && !vis[nx][ny]) {
            vis[nx][ny] = 1;
            que[cur].x = nx;
            que[cur].y = ny;
            cur++; 
            flag = dfs(nx, ny, num+1);
            vis[nx][ny] = 0;
            cur--;
            if(flag) return flag;
        }
    }
    return flag;
}
int main() {
    int t;
    scanf("%d", &t);
    for(int i = 1; i <= t; i++) {
        
        scanf("%d%d", &n, &m);
        vis[0][0] = 1; 
        printf("Scenario #%d:\n", i);
        if(!dfs(0, 0, 1)) {
            printf("impossible\n\n");
        }
    }
    return 0;
}

POJ 2488 A Knight's Journey