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

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

搞清楚一个字典序就行了,其余的很简单。

Posted by xijunlee93 at 2013-03-19 16:37:46 on Problem 2488


这一题的字典序:就是先按列排序,较小的在前。然后按行排序,也是较小的在前。
我的排序是这样的:
int diri[8]={-1,1,-2,2,-2,2,-1,1};
int dirj[8]={-2,-2,-1,-1,1,1,2,2};

大意很明了,就是找到一个路径让马走完所有的点,不重复不遗漏;思路很容易找到,直接用DFS搜索标记并回溯,一个点一个点作为起点去试;找到后停止;

#include<stdio.h>
int dir[][]={-,-,-,,-,-,-,,,-,,,,-,,}; //记录方向
int g,a,b;//g用来记录是否找到解,找到后不再搜索
int vist[][],path[][];
void find(int i,int j,int k)//i,j是要走的格子,k记录已经走过的步数
{
if(k==a*b)//走完了
{
for(int i=;i<k;i++)
printf("%c%d",path[i][]+'A',path[i][]+);
printf("\n");
g=;
}
else
for(int x=;x<;x++)//8个方向依次搜索
{
int n=i+dir[x][];
int m=j+dir[x][];
if(n>=&&n<b&&m>=&&m<a&&!vist[n][m]&&!g)
{
vist[n][m]=;//标记已走
path[k][]=n,path[k][]=m;
find(n,m,k+);
vist[n][m]=;//清除标记
}
}
}
int main()
{
int n;
scanf("%d",&n);
for(int m=;m<n;m++)
{
g=;
scanf("%d %d",&a,&b);
for(int i=;i<a;i++)//一个点一个点的尝试
for(int j=;j<b;j++)
vist[i][j]=;
vist[][]=;
path[][]=,path[][]=;
printf("Scenario #%d:\n",m+);
find(,,);
if(!g) printf("impossible\n");
printf("\n");
}
return ;
}
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
bool vis[maxn][maxn];
int path[][];
int n,m;
int next[][]={-,-,-,,-,-,-,,,-,,,,-,,};
bool flag;
void dfs(int x,int y,int step){
if(step==m*n){
flag=true;
for(int i=;i<step;i++){
printf("%c%d",path[i][]+'A'-,path[i][]);
}
printf("\n");
}
else
for(int k=;k<;k++){
int tx=x+next[k][];
int ty=y+next[k][];
if(tx>=&&tx<=n&&ty>=&&ty<=m&&!vis[tx][ty]&&!flag){
vis[tx][ty]=true;
path[step][]=tx;
path[step][]=ty;
dfs(tx,ty,step+);
vis[tx][ty]=false;
}
} } int main(){
int t;
scanf("%d",&t);
int Case=;
while(t--){
Case++;
memset(vis,false,sizeof(vis));
memset(path,,sizeof(path));
scanf("%d%d",&n,&m);
flag=false;
vis[][]=true;
path[][]=;
path[][]=;
printf("Scenario #%d:\n",Case);
dfs(,,);
if(!flag)
printf("impossible\n"); printf("\n");
}
return ;
}

大意很明了,就是找到一个路径让马走完所有的点,不重复不遗漏;思路很容易找到,直接用DFS搜索标记并回溯,一个点一个点作为起点去试;找到后停止;

poj2488 A Knight's Journey裸dfs的更多相关文章

  1. POJ2488-A Knight's Journey(DFS+回溯)

    题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Tot ...

  2. POJ 2488 A Knight's Journey(DFS)

    A Knight's Journey Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 34633Accepted: 11815 De ...

  3. 快速切题 poj2488 A Knight's Journey

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31195   Accepted: 10 ...

  4. POJ2488:A Knight's Journey(dfs)

    http://poj.org/problem?id=2488 Description Background The knight is getting bored of seeing the same ...

  5. poj-2488 a knight's journey(搜索题)

    Time limit1000 ms Memory limit65536 kB Background The knight is getting bored of seeing the same bla ...

  6. POJ 2488 A Knight's Journey (DFS)

    poj-2488 题意:一个人要走遍一个不大于8*8的国际棋盘,他只能走日字,要输出一条字典序最小的路径 题解: (1)题目上说的"The knight can start and end ...

  7. A Knight's Journey (DFS)

    题目: Background The knight is getting bored of seeing the same black and white squares again and agai ...

  8. poj 2488 A Knight's Journey( dfs )

    题目:http://poj.org/problem?id=2488 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. #include <io ...

  9. POJ2488 A Knight's Journey

    题目:http://poj.org/problem?id=2488 题目大意:可以从任意点开始,只要能走完棋盘所有点,并要求字典序最小,不可能的话就impossible: 思路:dfs+回溯,因为字典 ...

随机推荐

  1. linux 命令——34 du(转)

    Linux du命令也是查看使用空间的,但是与df命令不同的是Linux du命令是对文件和目录磁盘使用的空间的查看,还是和df命令有一些区别的. 1.命令格式: du [选项][文件] 2.命令功能 ...

  2. spa 小程序的研发随笔 (2) --- 预编译

    因为是连续写的2篇随笔,废话不多说.直接进入正题. 选择预编译的工具时,笔者采用了gulp.虽然,如今市面上大多采用的多为webpack,使用gulp也是有自己的缘由的. webpack的最主要特点是 ...

  3. python_61_装饰器4

    import time def timer(func):#timer(test1) func=test1 def deco(): start_time=time.time() func()#run t ...

  4. 小w的糖果

    题目连接 : https://ac.nowcoder.com/acm/contest/923/C 算是一道找规律的题了,因为后一个人会比前一个人多,可以理解成后一个人要继承前一个人,sum为当前糖果数 ...

  5. 螺旋矩阵,两步进阶,从暴力到o(1)

    题目描述 一个 n 行 n 列的螺旋矩阵可由如下方法生成: 从矩阵的左上角(第 1 行第 1 列)出发,初始时向右移动:如果前方是未曾经过的格子,则继续前进,否则右转:重复上述操作直至经过矩阵中所有格 ...

  6. A1020 Tree Traversals (25 分)

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  7. c++运算符重载和虚函数

    运算符重载与虚函数 单目运算符 接下来都以AClass作为一个类例子介绍 AClass{ int var } 区分后置++与前置++ AClass operator ++ () ++前置 一般设计为返 ...

  8. 三 python并发编程之多线程-重点

    一 threading模块介绍 multiprocess模块的完全模仿了threading模块的接口,二者在使用层面,有很大的相似性,因而不再详细介绍 二 开启线程的两种方式 #方式一 from th ...

  9. PHP判断时关于null,0,true,flase的值

    <?php// $wh = 0; //等于整数0,等于字符串'0',等于空字符串,等于空格字符串, 等于空(null),等于字符串('null')// $wh = '0'; //=>等于整 ...

  10. centos 关闭AliYunDun

    执行命令: service aegis stop #停止服务 chkconfig --del aegis # 删除服务