A Knight's Journey

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 66   Accepted Submission(s) : 27
Problem 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
PKU
题意:

给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径。经典的“骑士游历”问题。

思路:

1、  题目要求以"lexicographically"方式输出,也就是字典序...要以字典序输出路径,那么搜索的方向(我的程序是path()函数)就要以特殊的顺序排列了...这样只要每次从dfs(A,1)开始搜索,第一个成功遍历的路径一定是以字典序排列...

下图是搜索的次序,马的位置为当前位置,序号格为测试下一步的位置的测试先后顺序

按这个顺序测试,那么第一次成功周游的顺序就是字典序

 

2、国际象棋的棋盘,行为数字a;列为字母b

这一题一定程度上考验了做题者的模拟思想,利用了DFS+回溯;

AC代码:

 #include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath> using namespace std; int s[][]={};
int number=;
int a;
int b; void path(int &x,int &y,int i,int j,int num)
{
switch(num)
{
case :{x=i-;y=j-;break;}
case :{x=i+;y=j-;break;}
case :{x=i-;y=j-;break;}
case :{x=i+;y=j-;break;}
case :{x=i-;y=j+;break;}
case :{x=i+;y=j+;break;}
case :{x=i-;y=j+;break;}
case :{x=i+;y=j+;break;}
}
return;
} void dfsz(int row,int cow)
{
char c=(char)(cow-+'A');
cout<<c<<row;
if(number==a*b){//当number=a*b的时候则证明输出完毕
return;
}
number++;//自己后的number代表着该输出第number个点了
int x,y;
for(int i=;i<=;i++)//寻找第number个点
{
path(x,y,row,cow,i);
if(s[x][y]==number)
break;
}
dfsz(x,y);//输出(x,y)这个点;
return;
} bool dfs(int row,int cow)//表示行进到了(row,cow)这个点
{
if(row<=||row>a)//横坐标超界
return false;//竖坐标超界
if(cow<=||cow>b)
return false;
if(s[row][cow])//该点已经被访问过
return false;
number++;
s[row][cow]=number;//该点是第number个点
if(number==a*b){//当number=a*b的时候则证明输出完毕
return true;
}
int i;
for(i=;i<=;i++){
int x,y;
path(x,y,row,cow,i);//计算下一步的坐标(x,y)
bool a=dfs(x,y);//判断点(x,y)
if(a){
return true;
}
}
s[row][cow]=;//这一步访问点(row,cow)不行
number--;
return false;
} int main()
{
// freopen("1.txt","r",stdin);
int test;
cin>>test;
int k=;
while(k<=test){
cin>>a>>b;
number=;
memset(s,,sizeof(s));//每一个样例都要初始化,我就在这WA好几次
bool sgin=false;
for(int i=;i<=a;i++){
for(int j=;j<=b;j++){
sgin=dfs(i,j);
if(sgin){
number=;
cout<<"Scenario #"<<k<<":"<<endl;
dfsz(i,j);
cout<<endl<<endl;//输出结束后有一个空行
break;
}
s[i][j]=;
}
if(sgin)
break;
}
if(!sgin){
cout<<"Scenario #"<<k<<":"<<endl<<"impossible"<<endl<<endl;//输出结束后有一个空行,切记!
}
k++;
}
return ;
}

POJ 2488 A Knight's Journey(深搜+回溯)的更多相关文章

  1. POJ 2488 -- A Knight's Journey(骑士游历)

    POJ 2488 -- A Knight's Journey(骑士游历) 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. 经典的“骑士游历”问题 ...

  2. POJ 2488:A Knight's Journey 深搜入门之走马观花

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

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

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

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

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

  5. POJ 2488 A Knight's Journey (回溯法 | DFS)

    题目链接:http://poj.org/problem?id=2488 题意: 在国际象棋的题盘上有一个骑士,骑士只能走“日”,即站在某一个位置,它可以往周围八个满足条件的格子上跳跃,现在给你一个p ...

  6. poj 2488 A Knight's Journey 【骑士周游 dfs + 记忆路径】

    题目地址:http://poj.org/problem?id=2488 Sample Input 3 1 1 2 3 4 3 Sample Output Scenario #1: A1 Scenari ...

  7. Poj 2488 A Knight's Journey(搜索)

    Background The knight is getting bored of seeing the same black and white squares again and again an ...

  8. [poj]2488 A Knight's Journey dfs+路径打印

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45941   Accepted: 15637 Description Bac ...

  9. POJ 2488 A Knight's Journey【DFS】

    补个很久之前的题解.... 题目链接: http://poj.org/problem?id=2488 题意: 马走"日"字,让你为他设计一条道路,走遍所有格,并输出字典序最小的一条 ...

随机推荐

  1. AC日记——【模板】字符串哈希 洛谷 3370

    题目描述 如题,给定N个字符串(第i个字符串长度为Mi,字符串内包含数字.大小写字母,大小写敏感),请求出N个字符串中共有多少个不同的字符串. 友情提醒:如果真的想好好练习哈希的话,请自觉,否则请右转 ...

  2. Facebook FB.init() status参数的作用

    意思是 status 设为 ture 之后调用 FB.getLoginStatus() 不再产生网络请求,数据已经在 FB.init() 调用的时候被请求回来,缓存住了.

  3. C/C++-style输入输出函数

    C风格的输入输出 (1) int getchar() 与 int putchar(int c) getchar从stdin输入流中读取字符,每次只能读取一个字符.若想一次性读取多个字符,则可将其放入循 ...

  4. 【XML】document.createEvent的使用方法

    <aclass="comment-mod"onclick="alert('ss')"href="#">评论</a> ...

  5. kindeditor编辑器上传图片失败 错误 405.0解决办法(亲测)

    HTTP 错误 405.0 - Method Not Allowed(省略)editor/php/upload_json.php?dir=image物理路径 http://www.gdgoga.com ...

  6. xampp版本和具体的php,mysql版本的对应

    在国外网上查找到具体的xampp版本与php,mysql版本的对应关系,特此记录.以便需要的人使用.原文链接如下: http://code.stephenmorley.org/articles/xam ...

  7. NIO和IO(转)

    java NIO由以下几个核心部分组成: Channels(通道) Buffers(缓冲区) Selectors(选择器) 其他 Channel和Buffer: 所有的IO再NIO中都从一个Chann ...

  8. python demo整理

    1 变量作用域 #!/usr/bin/python # coding=utf-8 name = "whole global name" class Person: name = & ...

  9. 寒假学干货之------初步布局Layout

    在开发的最初,需要设计好我们的Activity,在res/layout下,找到**activitymian(名字都差不多的)的.xml文件,打开他就可以开始编辑. http://www.tuicool ...

  10. 初始化一个本地GIT仓储

    简单总结下 // 定位到仓储文件夹目录 $ cd /dir // 初始化本地仓储 $ git init ``` 添加本地GIT忽略清单文件.gitignore```// 添加OS X中系统文件.DS_ ...