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.

背景:

这位骑士厌倦了一次又一次地看到同样的黑白方块,于是决定环游世界。每当一个骑士移动,它是一个方向上的两个正方形和一个垂直于这个方向的正方形。骑士的世界就是他赖以生存的棋盘。我们的骑士住在一个棋盘上,它的面积比普通的8 * 8棋盘小,但它仍然是长方形的。你能帮助这位爱冒险的骑士制定旅行计划吗?

问题:

找到一条骑士可以每个方格走一次的路。骑士可以在棋盘的任何方格上开始和结束。


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

输入以第一行的正整数 n 开始。下面的行包含 n 个测试用例。每个测试用例包含一个带有两个正整数 p 和 q 的单行,使得1 < = p * q < = 26。这表示一个 p * q 棋盘,其中 p 描述有多少不同的平方数1,... ,p 存在,q 描述有多少不同的平方字母存在。这些是拉丁字母表中的第一个 q 字母: 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.

每个场景的输出以包含“ Scenario # i:”的行开始,其中 i 是场景的数量,从1开始。然后打印一行,其中包含按字典顺序排列的第一条路径,该路径访问棋盘的所有方块,其中骑士移动后跟一条空行。路径应该在一个单一的线连接的名称所访问的方格。每个正方形名称由一个大写字母后跟一个数字组成。如果不存在这样的路径,您应该在一行中输出不可能的结果。


样例输入
3
1 1
2 3
4 3
样例输出
情景 #1:
A1

情景 #2:
impossible

情景 #3:
A1B3C1A2B4C2A3B1C3A4B2C4
来源
TUD Programming Contest 2005, Darmstadt, Germany


分析(这题我解析比较细,小白可以看看)

好长一道题,简单梳理一下,精简一下大概是:给一个棋盘,问能不能以马的跳法不重复、无遗漏地将棋盘遍历一遍。如果可以的话,按照字典序输出排列在先的一条路径。

要注意几点:

1.马的走法,如题目中给出的图片所示,和中国象棋中“马走日”的方式类似,走一个2*1矩形的对角线。

2、“按照字典序输出排列在先”是什么意思呢?
两种情况:
①针对字母:A1B2C3D4排列在A1C3B2D4之前
②针对数字:A1B2C3D4排列在A2B2C3D4之前
所以,在进行深搜的时候,八个位置的先后顺序应该如下图红色数字标注那样。否则会WA。

于是,就能给出移动的坐标变换数组了,分别对应上述八个位置的移动:

int dx[8]={-1,1,-2,2,-2,2,-1,1};///x为行数,纵坐标
int dy[8]={-2,-2,-1,-1,1,1,2,2};///y为列数,横坐标

3、 dfs分为四步:当前操作、结束条件、搜索条件、是否有回溯操作
这四步也是掌握dfs的关键。
其中,本题也需要注意“回溯”的部分。

代码如下:

for(int i=0;i<8;i++){
        int nx=x+dx[i];
        int ny=y+dy[i];//搜索条件
        if(nx>=1&&nx<=p&&ny>=1&&ny<=q&&!book[nx][ny]&&!success){
            book[nx][ny]=true;
            dfs(nx,ny,num+1);//极为重要,回溯的思想,要去想搜索失败的时候,当前位置的访问标记要变回false
            book[nx][ny]=false;
        }
    }

①按照0-7的顺序进行移动,判断是否达到搜索条件(请注意,此处除了需要满足不跳出棋盘外,还需要满足下一个点未被访问和还没有成功遍历整个棋盘两个条件)。
②如果是,进入if语句,标记(nx,ny)已被访问,继续dfs,将(nx,ny)的访问标记删除。

在不断dfs过程中,发现某一位置的下一步的八个位置要么在来的路上被访问过了,要么出界了,且此时整个棋盘还未遍历完,这说明,这条路径是不通的,那么在结束本次递归的时候,需要将原来标记的访问删除掉,以免在尝试另外一种走法时产生错误,因为这个位置的访问与否直接影响了下一条路径搜索的结果。

4、 还有一个需要注意的小地方,小技巧,就是序号与字母的对应转换。
输出路径代码:

for(int j=0;j<p*q;j++){
       printf("%c%d",path[j].y+'A'-1,path[j].x);
}

因为代码中x代表行数1-26,y代表列数A-Z。而输出要列在先,行在后,所以先y后x。
数字1+‘A’-1=‘A’,2+‘A’-1=B,以此类推。
同理,字母转化成序号就是 -‘A’ 或者 -‘a’ 。


代码

前面讲的够细了吧,现在把那些代码块组合,再加点开头、过渡和结尾就AC啦,突然感觉好简单

#include<stdio.h>
#define maxn 27
struct path{
    int x;
    int y;
}path[maxn];
int num,p,q;
bool book[maxn][maxn];
bool success=false;
///注意字典序
int dx[8]={-1,1,-2,2,-2,2,-1,1};///x为行数,纵坐标
int dy[8]={-2,-2,-1,-1,1,1,2,2};///y为列数,横坐标
void dfs(int x,int y,int num)
{
    ///当前节点加入路径
    path[num].x=x;
    path[num].y=y;
    ///结束条件
    if(num==p*q-1){
        success=true;
        return;
    }
    for(int i=0;i<8;i++){
        int nx=x+dx[i];
        int ny=y+dy[i];
        ///搜索条件
        if(nx>=1&&nx<=p&&ny>=1&&ny<=q&&!book[nx][ny]&&!success){
            book[nx][ny]=true;
            dfs(nx,ny,num+1);
            ///极为重要,回溯的思想,要去想搜索失败的时候,当前位置的访问标记要变回false
            book[nx][ny]=false;
        }
    }
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        num=0;
        success=false;
        scanf("%d%d",&p,&q);
        for(int t=1;t<=p;t++)
            for(int k=1;k<=q;k++)
                book[t][k]=false;
        ///以上为初始化
        book[1][1]=true;
        dfs(1,1,0);
        printf("Scenario #%d:\n",i);
        if(!success){
            printf("impossible\n");
        }
        else{
            for(int j=0;j<p*q;j++){
                printf("%c%d",path[j].y+'A'-1,path[j].x);
            }
            printf("\n");
        }
        printf("\n");
    }
    return 0;
}

给个赞和关注吧

  • 70
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值