POJ 2488 A Knight's Journey【dfs过】

本文探讨如何帮助骑士在特定大小的棋盘上完成一次访问所有方格的旅程,并提供了一个通过深度优先搜索(DFS)算法实现路径规划的解决方案。通过确保路径按字典序输出,本文为迷宫问题提供了实用的解决方法。

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

A Knight's Journey
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 37494 Accepted: 12748

Description

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

题目大意:起点在左上角A1处,问是否能够走完所有的格子,如果能走完所有的格子,要求字典序输出,否则输出impossible;

题目不难,因为图比较小,所以直接dfs就行了,不需要任何剪枝,另外我们要注意,因为走法不一,所以要求输出按字典序输出,我们这里在走法优先上做点文章就行了,我们不能无脑游走棋子,需要有序的游走,按最优的走完之后,其他方案都一律抛弃就行了:

/*
    poj 2488
*/
#include<stdio.h>
#include<string.h>
using namespace std;
int ans[100][2];//这里我记录的是坐标,换成字母的时候要对应加上大写字母A
int vis[100][100];
int fx[] = { -1, 1, -2, 2, -2, 2, -1, 1 },
fy[] = { -2, -2, -1, -1, 1, 1, 2, 2 };//注意顺序
//左侧优先
int n,m;
int ok;
int kase;
void dfs(int x,int y,int cur)
{
    if(ok==1)
    return ;
    if(cur==n*m)
    {
        ok=1;
        printf("Scenario #%d:\n",++kase);
        for(int i=1;i<cur;i++)
        {
            printf("%c%d",ans[i][1]+'A'-1,ans[i][0]);
        }
        printf("%c%d\n",y+'A'-1,x);
        return ;
    }
    ans[cur][0]=x;
    ans[cur][1]=y;
    vis[x][y]=1;
    for(int i=0;i<8;i++)
    {
        int xx=x+fx[i];
        int yy=y+fy[i];
        if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&vis[xx][yy]==0)
        {
            vis[xx][yy]=1;
            dfs(xx,yy,cur+1);
            vis[xx][yy]=0;
        }
    }
    return ;
}
int main()
{
    kase=0;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ok=0;
        memset(vis,0,sizeof(vis));
        scanf("%d%d",&n,&m);
        dfs(1,1,1);
        if(ok==0)
        {
            printf("Scenario #%d:\nimpossible\n",++kase);
        }
        printf("\n");
    }
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值