POJ 2488-A Knight's Journey(DFS-象棋中的马)

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

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

Source

TUD Programming Contest 2005, Darmstadt, Germany

题目意思:

给出p*q大小的棋盘,棋子“马”可以从任何位置开始或者结束(马走日字),求一条路径满足“马”可以以字典序遍历各个方格且仅一次。

解题思路:

DFS,坑就是字典序,解决这个问题要从搜索方向入手。
我的程序中字母作为列,数字作为行。
先标记各个点是第几步走到的,然后根据标号依次输出路径。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define MAXN 30
#define MOD 1000000000
int vis[MAXN][MAXN],map[MAXN][MAXN];
int dir[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{2,-1},{2,1},{1,-2},{1,2}};//字典序方向搜索
int res=0,st;
int m,n;
bool flag=false;

char shift(int a)
{
    return char(int(a+'A'));
}

void dfs(int x,int y)
{
    if(flag) return;
    if(x<0||y<0||x>=m||y>=n||vis[x][y])
        return;
    st++;//总步数
    int i,j,tx,ty;
    if(st==m*n)//能经过每个点且仅一次
    {
        map[x][y]=m*n;
        int step=1;
        while(step<=m*n)//依次输出1~m*n
        {
            for(i=0; i<m; ++i)
                for(j=0; j<n; ++j)
                    if(map[i][j]==step)
                    {
                        ++step;
                        cout<<shift(j)<<i+1;
                        break;
                    }
        }
        cout<<endl;
        flag=true;
        return;
    }
    vis[x][y]=1;//已访问
    map[x][y]=st;//记录步数
    for(i=0; i<8; ++i)//八个方向依次搜索
    {
        tx=x+dir[i][0];
        ty=y+dir[i][1];
        dfs(tx,ty);
    }
    vis[x][y]=0;//回退
    map[x][y]=0;
    --st;
}
int main()
{

#ifdef ONLINE_JUDGE
#else
    freopen("G:/cbx/read.txt","r",stdin);
    //freopen("F:/cb/out.txt","w",stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t,ca=0;
    cin>>t;
    while(t--)
    {
        cin>>m>>n;
        cout<<"Scenario #"<<++ca<<":"<<endl;
        int i,j;
        flag=false;
        for(i=0; i<m; ++i)//m*n的棋盘
            for(j=0; j<n; ++j)
            {
                if(flag) break;
                memset(map,0,sizeof(map));
                memset(vis,0,sizeof(vis));
                st=0;
                //cout<<"("<<i<<","<<j<<")"<<endl;//起点
                dfs(i,j);//点(i,j)作为起始位置
            }
        if(!flag) cout<<"impossible"<<endl;
        cout<<endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值