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. IBM WebSphere MQ安装及配置详解

    打开MQ安装程序,选择下一步,默认安装WebSphere MQ, 完成MQ的安装工作,启动WebSphere MQ, 服务器配置,选择新建队列管理器,创建名为 "mq"的队列管理器 ...

  2. Spark运行命令示例

    local单机模式:结果xshell可见:./bin/spark-submit --class org.apache.spark.examples.SparkPi --master local[1] ...

  3. 设置和重置ssh key

    查看本地是否有已经生成好的ssh key $ cat ~/.ssh/id_rsa.pub 若有,先删除: $ cd ~ $ rm -rf .ssh 重新生成ssh key ssh-keygen -t ...

  4. Hive之数据模型

    (本文是基于多篇文章根据个人理解进行的整合,参考的文章见末尾的整理) 数据模型 hive的数据模型包括:database.table.partition和bucket. 1.Database:相当于关 ...

  5. softmax 函数

    总结为:   将一组数变换为  总和为1,各个数为0~1之间的软性归一化结果. ========================================================= 关于 ...

  6. Ubuntu安装mysql和简单使用

    一.安装mysql sudo apt-get install mysql-server sudo apt-get isntall mysql-client sudo apt-get install l ...

  7. jQuery支持链式编程,一句话实现左侧table页+常用筛选器总结

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  8. EasyUI获取正在编辑状态行的索引

    function getRowIndex(target){ var tr = $(target).closest("tr.datagrid-row"); return paseIn ...

  9. 配置管理-SpringCloudConfig

    1.搭建配置管理服务 添加依赖 <dependencies> <dependency> <groupId>org.springframework.cloud< ...

  10. JavaScript 循环

    for循环:  如果您希望一遍又一遍运行相同的代码,并且每次的值都不同,那么使用循环是很方便的. 我们可以这样输出数组的值: document.write(cars[0] + "<br ...