码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2488 A Knight's Journey

时间:2018-07-11 22:54:25      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:visit   represent   first   struct   bfs   start   make   decide   possible   

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

标签:visit   represent   first   struct   bfs   start   make   decide   possible   

原文地址:https://www.cnblogs.com/kindleheart/p/9296931.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!