POJ2488 A Knight's Journey

Description

Background
The knight is getting bored of seeing the same black and white squares againand again and has decided to make a journey
around the world. Whenever a knight moves, it is two squares in one directionand one square perpendicular to this. The world of a knight is the chessboardhe is living on. Our knight lives on a chessboard that has a smaller area thana regular 8 * 8 board, but it is still rectangular. Can you help thisadventurous knight to make travel plans?

Problem
Find a path such that the knight visits every square once. The knight can startand end on any square of the board.

Input

The input begins with a positive integer n in the firstline. The following lines contain n test cases. Each test case consists of asingle 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 differentsquare numbers 1, . . . , p exist, q describes how many different square lettersexist. These are the first q letters of the Latin alphabet: A, . . .

Output

The output for every scenario begins with a linecontaining "Scenario #i:", where i is the number of the scenariostarting at 1. Then print a single line containing the lexicographically firstpath that visits all squares of the chessboard with knight moves followed by anempty line. The path should be given on a single line by concatenating thenames of the visited squares. Each square name consists of a capital letterfollowed 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

 

       给定一个p*q的棋盘,问骑士能不能走遍棋盘上的所有格子,每个格只访问一次,可以从任意位置开始。

       一道简单的DFS,骑士必须按日”字行走。从棋盘上每个格作为起点开始深搜,直到找到答案,或者所有情况都搜索到而没有答案。

       是模板题吧(大概)。不过自己当时做的时候,记录每一步的方法太蠢,后来借鉴其他大牛的方法,效果好很多。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>

using namespace std;

int p,q,path[30][2];
bool vis[30][30];
int dq[]={-2,-2,-1,-1,1,1,2,2};
int dp[]={-1,1,-2,2,-2,2,-1,1};
bool flag;

bool judge(int a,int b)
{
    if (a>0&&a<=b)
        return 1;
    else
        return 0;
}


void DFS(int x,int y,int cur)
{

    if (cur==p*q)
    {
        flag=1;
        return ;
    }
    else
    {
        for (int i=0;i<8;i++)
        {
            int tp,tq;
            tp=x+dp[i]; tq=y+dq[i];
            if (!vis[tp][tq]&&judge(tq,q)&&judge(tp,p))
            {
                path[cur+1][0]=tp;    path[cur+1][1]=tq;
                vis[tp][tq]=1;
                DFS(tp,tq,cur+1);
                if (flag)
                    return ;
                vis[tp][tq]=0;
            }
        }
    }
}

int main()
{
    int T,n=0;
    cin>>T;
    while (T--)
    {
        if (n++)
            printf("\n");
        cin>>p>>q;
        memset(vis,0,sizeof vis);
        memset(path,-1,sizeof path);
        flag=0;
        for (int i=1;i<=p;i++)
        {
            for (int j=1;j<=q;j++)
            {
                memset(path,-1,sizeof path);
                path[1][0]=1;   path[1][1]=1;
                vis[i][j]=1;
                DFS(i,j,1);
                if (flag)
                    break;
                vis[i][j]=0;
            }
            if (flag)
                break;
        }
        printf("Scenario #%d:\n",n);
        if (flag)
        {
            for (int i=1;i<=p*q;i++)
            {
                printf("%c%d",'A'+path[i][1]-1,path[i][0]);
            }
            printf("\n");
        }
        else
        {
            printf("impossible\n");
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值