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. Python变量状态保持四种方法

    Python状态保持 ​ 全局 global def tester(start): global state state = start def nested(label): global state ...

  2. Producer & Consumer

    需要与Eureka结合使用 Eureka环境搭建 Producer 一.pom文件 <?xml version="1.0" encoding="UTF-8" ...

  3. hdu-1757 A Simple Math Problem---矩阵快速幂模板题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1757 题目大意: 求递推式第k项模m If x < 10 f(x) = x.If x > ...

  4. java 字符串中是否有数字

    http://www.cnblogs.com/zhangj95/p/4198822.html http://www.cnblogs.com/sunzn/archive/2013/07/12/31865 ...

  5. SyntaxHighlighter使用方法

    原名:SyntaxHighlighter,是一款用于web页面的代码着色工具,可以用来着色多种语言,可以是HTML,CSS,Javascript,还可以是C,JAVA等编程语言.最早见于Yahoo的Y ...

  6. Dtree 添加 checkbox 复选框 可以默认选中

    一:目标 要实现用一个树形结构的展示数据,每个节点(除了根节点)前有一个checkbox,同时,点击父节点,则子节点全选或者全不选,当选中了全部子节点,父节点选中:如下图所示: 同时可以在创建的时候, ...

  7. 1412: [ZJOI2009]狼和羊的故事

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4017  Solved: 2037[Submit][Status][Discuss] Descript ...

  8. Mysql--数据操作语言(DML)

    定义:数据操作语言主要实现对数据库表中的数据进行操作,主要包括插入(insert).更新(update).删除(delete).查询(select),本节主要介绍增删改. 数据准备: 一.数据的插入( ...

  9. k8s的ingress资源简述

    ingress controller是独立与controller-manager的Ingress的主要作用是可以利用nginx,haproxy,envoy,traefik等负载均衡器来暴露集群内部服务 ...

  10. c#:无限极树形结构

    最近一直在研究树形结构菜单,无意中让我弄了出来.先上代码: 首先需要这个的一个类 public class Tree { public int id { get; set; } public stri ...