POJ2488/openjudge 1490 A Knight's Journey  解题报告(dfs)

这篇博客介绍了一个使用DFS解决的骑士在棋盘上寻找最优路径的问题。题目要求在p*q的棋盘上,骑士能按照日字型移动,访问每个格子一次。通过分析,博主提出了一种按字典序选择方向的方法,并提供了C++实现的代码片段。对于无法找到符合条件的路径的情况,输出'Impossible'。博客中包含3个样例输入和对应的输出解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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, …
给你两个数p,q代表行与列(或列与行,可自己决定)。p*q代表格子数,这里的q(列的坐标)用A,B,C…..代替1,2,3……,p(行的坐标)还是用1,2,3,4….表示
输出
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

底部还有91组测试数据。

题目分析:

简单的dfs寻找最优路径问题,但这里的最优路径有两个条件。①必须可以走完所有格子且不重复 ②需率先走字典序小的那个路径。

这里问题①很好解决。主要问题是②如何解决。
里面的白点就代表着骑士可以走的格子
我们将图上的点抽象一下。
这里有一个很巧妙的方法:

这里将@看做是骑士,0看做是能走的位置。

1234
#######
A##0#0##
B#0###0#
###@###
C#0###0#
D##0#0##
#######

因为我们的行列分别是用数字和字母编号的,那么我们也可以将骑士所能走的八个方向按字典序从小到大排序进行排序
从图可知 A2 < A3 < B1 < B4 < C1 < C4 < D2 < D3
从而我们可以这样定义:
c代表列,r代表行
(dc[0],dr[0])就代表图中的A2点
const int dc[]={-1,1,-2,2,-2,2,-1,1};
const int dr[]={-2,-2,-1,-1,1,1,2,2};

  • 注意1 <= p*q <= 26 说明p,q的最大值可以达到26。

具体代码

采用了 vector数组 ,判定成立的条件就是当数组大小等于p*q时,说明以全部走完。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <string>
using namespace std;
vector<string> v;//存储路径
//八个方向,按字典序排序
const int dc[]={-1,1,-2,2,-2,2,-1,1};
const int dr[]={-2,-2,-1,-1,1,1,2,2};
int R,C;//边界
int vis[27][27];//标记数组
bool ok = true;
void dfs(int r,int c);
string int_s(int r,int c)//将(x,y)坐标转变为字母加数字
{
    string a;
    char ch1,ch2;
    ch1 = r-1  + 'A';
    ch2 = c+'0';
    a=a+ch1;
    a=a+ch2;
    return a;//返回一个string
}
int main()
{
    int n;
    cin>>n;
    for(int i = 1; i <= n; i++)
    {
        //初始化
        ok = true;
        memset(vis, 0, sizeof(vis));
        cin>>C>>R;
        v.clear();//记得清空
        v.push_back(int_s(1, 1));
        vis[1][1]=1;
        dfs(1,1);//从起点1,1开始
        //初始化
        cout<<"Scenario #"<<i<<":"<<endl;;
        if(v.size()!=R*C)//如果最后的路径等于所有的格子说明有解,否则无解
            ok=false;
        if(ok)
        {
            for(vector<string>::iterator it=v.begin();it!=v.end();it++)//输出vector的所有内容
                cout<<*it;
            cout<<endl;
        }
        else
            cout<<"impossible"<<endl;
        cout<<endl;
    }
    return 0;

}
void dfs(int r,int c)
{
    for(int i=0;i<8;i++)//八个方向
    {
        int tmp_r=r,tmp_c=c;
        r+=dr[i];c+=dc[i];
        if(!vis[r][c]&&r>=1&&r<=R&&c>=1&&c<=C)
        {
            vis[r][c]=1;//标记
            v.push_back(int_s(r, c));//该点压入数组
            dfs(r,c);
            if(v.size()==R*C)//如果已经到了这一步说明全部的格子都已走完,就可以直接结束递归,将所有栈帧释放
                break;//这里的break可以省掉一大笔时间
            v.pop_back();//走到这说明刚刚上一条路径不同,则需把刚刚压入数组的点释放出来
            vis[r][c]=0;//还原变量
            r=tmp_r;c=tmp_c;
        }
        r=tmp_r;c=tmp_c;//记得还原
    }
}
91
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 21
1 22
1 23
1 24
1 25
1 26
2 1
2 2
2 3
2 4
2 5
2 6
2 7
2 8
2 9
2 10
2 11
2 12
2 13
3 1
3 2
3 3
3 4
3 5
3 6
3 7
3 8
4 1
4 2
4 3
4 4
4 5
4 6
5 1
5 2
5 3
5 4
5 5
6 1
6 2
6 3
6 4
7 1
7 2
7 3
8 1
8 2
8 3
9 1
9 2
10 1
10 2
11 1
11 2
12 1
12 2
13 1
13 2
14 1
15 1
16 1
17 1
18 1
19 1
20 1
21 1
22 1
23 1
24 1
25 1
26 1
Scenario #1:
A1

Scenario #2:
impossible

Scenario #3:
impossible

Scenario #4:
impossible

Scenario #5:
impossible

Scenario #6:
impossible

Scenario #7:
impossible

Scenario #8:
impossible

Scenario #9:
impossible

Scenario #10:
impossible

Scenario #11:
impossible

Scenario #12:
impossible

Scenario #13:
impossible

Scenario #14:
impossible

Scenario #15:
impossible

Scenario #16:
impossible

Scenario #17:
impossible

Scenario #18:
impossible

Scenario #19:
impossible

Scenario #20:
impossible

Scenario #21:
impossible

Scenario #22:
impossible

Scenario #23:
impossible

Scenario #24:
impossible

Scenario #25:
impossible

Scenario #26:
impossible

Scenario #27:
impossible

Scenario #28:
impossible

Scenario #29:
impossible

Scenario #30:
impossible

Scenario #31:
impossible

Scenario #32:
impossible

Scenario #33:
impossible

Scenario #34:
impossible

Scenario #35:
impossible

Scenario #36:
impossible

Scenario #37:
impossible

Scenario #38:
impossible

Scenario #39:
impossible

Scenario #40:
impossible

Scenario #41:
impossible

Scenario #42:
impossible

Scenario #43:
A1C2A3B1D2B3C1A2C3D1B2D3

Scenario #44:
impossible

Scenario #45:
impossible

Scenario #46:
A1B3D2F1G3E2G1F3E1G2E3C2A3B1C3A2C1D3B2D1F2

Scenario #47:
A1B3C1A2C3D1B2D3E1G2E3C2A3B1D2F1H2F3G1E2G3H1F2H3

Scenario #48:
impossible

Scenario #49:
impossible

Scenario #50:
A1B3C1A2B4C2A3B1C3A4B2C4

Scenario #51:
impossible

Scenario #52:
A1B3C1A2B4D3E1C2D4E2C3A4B2D1E3C4A3B1D2E4

Scenario #53:
A1B3C1A2B4C2D4E2F4D3E1F3D2B1A3C4B2A4C3E4F2D1E3F1

Scenario #54:
impossible

Scenario #55:
impossible

Scenario #56:
impossible

Scenario #57:
A1B3A5C4D2B1A3B5D4C2B4A2C1D3C5A4B2D1C3D5

Scenario #58:
A1B3A5C4A3B1D2E4C5A4B2D1C3B5D4E2C1A2B4D5E3C2E1D3E5

Scenario #59:
impossible

Scenario #60:
impossible

Scenario #61:
impossible

Scenario #62:
A1B3A5C6D4B5D6C4D2B1A3C2B4A2C1D3B2D1C3D5B6A4C5A6

Scenario #63:
impossible

Scenario #64:
impossible

Scenario #65:
A1B3C1A2C3B1A3C2B4A6C7B5A7C6A5B7C5A4B2C4B6

Scenario #66:
impossible

Scenario #67:
impossible

Scenario #68:
A1B3C1A2B4C2A3B1C3A4B2C4A5B7C5A6B8C6A7B5C7A8B6C8

Scenario #69:
impossible

Scenario #70:
impossible

Scenario #71:
impossible

Scenario #72:
impossible

Scenario #73:
impossible

Scenario #74:
impossible

Scenario #75:
impossible

Scenario #76:
impossible

Scenario #77:
impossible

Scenario #78:
impossible

Scenario #79:
impossible

Scenario #80:
impossible

Scenario #81:
impossible

Scenario #82:
impossible

Scenario #83:
impossible

Scenario #84:
impossible

Scenario #85:
impossible

Scenario #86:
impossible

Scenario #87:
impossible

Scenario #88:
impossible

Scenario #89:
impossible

Scenario #90:
impossible

Scenario #91:
impossible
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值