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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章