poj 2488  A Knight's Journey

来源:互联网 发布:维珍澳航 知乎 编辑:程序博客网 时间:2024/06/10 13:49

题目链接:点击打开链接

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

31 12 34 3

Sample Output

Scenario #1:A1Scenario #2:impossibleScenario #3:A1B3C1A2B4C2A3B1C3A4B2C4


题目大意:给出棋盘的大小,骑士在(1,1)原点开始走(走日),问能不能走完整个棋盘,如果能输出字典需最小的序列(行用数字表示,列用字母表示),else 输出示例2

ps:dfs深搜,当搜到符合的时候就标记不再进行其他的路径搜索,字典序列从骑士的左上方到右下方遍历即可.注意换行的输出

<span style="font-size:18px;">#include <iostream>#include<cstdio>#include<cstring>using namespace std;int mp[100][100];int dx[]={-1,1,-2,2,-2,2,-1,1};int dy[]={-2,-2,-1,-1,1,1,2,2};int s;int n,m;int a[1000];int b[1000];int ans;int f;void dfs(int x,int y,int step){    a[step]=x;    b[step]=y;    if(step==s)    {        f=1;        return ;    }    for(int i=0;i<8;i++)    {        int nx=x+dx[i];        int ny=y+dy[i];        if(nx>=1&&ny>=1&&nx<=n&&ny<=m&&mp[nx][ny]==0&&f==0)        {            mp[nx][ny]=1;            dfs(nx,ny,step+1);            mp[nx][ny]=0;        }    }}int main(){    int t;    cin>>t;    int k=0;    while(t--)    {        cin>>n>>m;        memset(mp,0,sizeof(mp));        s=n*m;        mp[1][1]=1;        f=0;        dfs(1,1,1);        if(f==0)        {            cout<<"Scenario #"<<++k<<":\nimpossible\n";        }        else        {            cout<<"Scenario #"<<++k<<":"<<endl;            for(int i=1;i<=s;i++)            {                printf("%c%d",b[i]+'A'-1,a[i]);            }            cout<<endl;        }        if(t!=0)cout<<endl;    }    return 0;}</span>


0 0
原创粉丝点击