码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2488 A Knight's Journey

时间:2014-07-09 12:03:01      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   strong   width   

A Knight‘s Journey
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 29280   Accepted: 10039

Description

bubuko.com,布布扣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

Source

TUD Programming Contest 2005, Darmstadt, Germany

八个方向的深搜回溯 把移动方向打好 (网上好多人说要按字典序走才能A   测试了一下  不按字典序也A了)

#include<iostream>
#include<cstring>
#define M 30
int dx[8] = {-1, 1, -2, 2, -2, 2, -1, 1};
int dy[8] = {-2, -2, -1, -1, 1, 1, 2, 2};
using namespace std;
int cas,n,m,tag;
int map[M][M],t;
char ans[900][2];
void dfs(int x,int y,int k)
{
    int xx,yy,i,j;
    if(k==n*m)
    {
        tag=1;
    }
    for(i=0;i<8;i++)
    {
        xx=x+dx[i];
        yy=y+dy[i];
        if(map[xx][yy]==0&&xx>0&&xx<=n&&yy>0&&yy<=m)
        {
            ans[k][0]=ans[k-1][0]+dy[i];//注意这里x轴移动的位移并不是字母轴的位移而是数字轴的位移。。坑我好久
            ans[k][1]=ans[k-1][1]+dx[i];
            map[xx][yy]=1;
for(i=1;i<=n;i++)
{
    for(j=1;j<=m;j++)
    cout<<map[i][j];cout<<endl;
}cout<<endl;
            dfs(xx,yy,k+1);
            if(tag)
                return ;
            map[xx][yy]=0;
        }
    }
    //return ;



}
int main()
{
    int i,j,l=1;
    cin>>cas;
    while(l<=cas)
    {

        memset(map,0,sizeof(map));
        memset(ans,'0',sizeof(ans));
        map[1][1]=1;
        ans[0][0]='A';
        ans[0][1]='1';
        cin>>n>>m;
        t=1;
        tag=0;
        cout<<"Scenario #"<<l<<":"<<endl;
        dfs(1,1,1);
            if(tag)
               {
                   //cout<<ans[0][0]<<ans[0][1];
                   for(i=0;i<n*m;i++)
                    cout<<ans[i][0]<<ans[i][1];
               }
            else
                cout<<"impossible";
            cout<<endl<<endl;
            l++;
    }
}


poj 2488 A Knight's Journey,布布扣,bubuko.com

poj 2488 A Knight's Journey

标签:des   style   http   color   strong   width   

原文地址:http://blog.csdn.net/fanerxiaoqinnian/article/details/37566487

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!