A Knight's Journey

Time Limit: 1000MS
Memory Limit: 65536K

Total Submissions: 34633
Accepted: 11815

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

题目简单翻译:

给你一个象棋中的马,一个n*m的棋盘,求是否能从一点出发,走遍整个棋盘,不重复走。如果能,按字典序输出第一个序列。如果不能,则输出“impossible”。

解题思路:

dfs,从一点出发,然而因为要字典序较小的,我们就选择(1,1)为起始点吧。注意延伸的方向,优先向字典序小的方向延伸。

代码:

#include<cstdio>
#include<cstring>
#include<queue> using namespace std;
int n,m;
int vis[][];
int dx[]={-,-,-,-,,,,};
int dy[]={-,,-,,-,,-,};
int a1[],a2[];
bool check(int x,int y)
{
return x>=&&x<n&&y>=&&y<m;
}
bool dfs(int x,int y,int depth)
{
if(depth==m*n)
{
for(int i=;i<depth;i++)
printf("%c%d",a1[i]+'A',a2[i]+);
puts("");
return true;
}
for(int i=;i<;i++)
{
int curx=x+dx[i];
int cury=y+dy[i];
if(check(curx,cury)&&vis[curx][cury]==)
{
a1[depth]=curx;
a2[depth]=cury;
vis[curx][cury]=;
if(dfs(curx,cury,depth+)) return true;
vis[curx][cury]=;
}
}
return false;
}
int main()
{
int T;
scanf("%d",&T);
int flag=;
while(T--)
{
if(flag) puts("");
scanf("%d%d",&m,&n);
memset(vis,,sizeof vis);
vis[][]=;
a1[]=,a2[]=;
printf("Scenario #%d:\n",++flag);
if(!dfs(,,)) puts("impossible");
}
return ;
}

最新文章

  1. TCP十一种状态
  2. MFCC特征提取(C语言版本)
  3. 在Ubuntu下使用 csapp.h 和 csapp.c
  4. ai seek
  5. Lua中的常用函数库汇总
  6. VIM如何将全部内容复制并粘贴到外部
  7. 第十一章 管理类型(In .net4.5) 之 管理对象的生命周期
  8. iOS tableview 优化总结
  9. pattern目录
  10. can&#39;t add foreign key in mysql?
  11. 【原】Spark Rpc通信源码分析
  12. idea svn 更新覆盖了本地代码
  13. 优化mysql数据库的几个步骤
  14. web攻击
  15. HttpClient MultipartEntityBuilder 上传文件
  16. SQL Server重置INDETITY的开始值
  17. Spring Security 使用数据库用户进行认证
  18. MySQL时间戳时间
  19. svn+apache+ssl快速部署
  20. perl(ExtUtils::Embed)依赖包

热门文章

  1. CSU 1335 高桥和低桥
  2. DataTables在回调方法中使用api
  3. 为什么1Byte=8bit
  4. spring的数据源基本配置
  5. CSS3----background:-webkit-gradient()渐变效果
  6. java.lang.OutOfMemoryError: unable to create new native thread(转)
  7. linux下C和shell调用的popen函数
  8. 编译不通过:提示XXXX不是类或命名空间名 的解决办法
  9. 精确覆盖DLX算法模板另一种写法
  10. JAX-WS 学习一:基于java的最简单的WebService服务