POJ 2488 A Knight's Journey
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 29226 | Accepted: 10023 |
Description

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
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
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
深索水题。,,。主要是方向被规定了。
AC代码例如以下:
#include<iostream>
#include<cstring>
using namespace std; struct H
{
int x;
char y;
}b[30],c[30]; int dx[8]={-1,1,-2,2,-2,2,-1,1};
int dy[8]={-2,-2,-1,-1,1,1,2,2}; int a[30][30],vis[30][30];
int n,m,bj; void dfs(int h,int z,int cur)
{
int i;
if(cur==n*m)
{
if(bj==0)
{
for(i=0;i<cur;i++)
cout<<b[i].y<<b[i].x;
cout<<endl<<endl;
bj=1;
} }
else
{
for(i=0;i<8;i++)
{
int xx,yy;
xx=h+dx[i];
yy=z+dy[i];
if(!vis[xx][yy]&&xx>=1&&xx<=n&&yy>=1&&yy<=m)
{
vis[xx][yy]=1;
b[cur].x=xx;b[cur].y=(char)(yy+'A'-1);
dfs(xx,yy,cur+1);
vis[xx][yy]=0;
}
} }
} int main()
{
int t,cas=0;
cin>>t;
while(t--)
{
cas++;
memset(a,0,sizeof a);
memset(vis,0,sizeof vis);
cin>>n>>m;
bj=0;
b[0].x=1;b[0].y=1+'A'-1;
vis[1][1]=1;
cout<<"Scenario #"<<cas<<":"<<endl;
dfs(1,1,1);
if(bj==0)
{
cout<<"impossible"<<endl<<endl;
}
}
return 0;
}
POJ 2488 A Knight's Journey的更多相关文章
- poj 2488 A Knight's Journey(dfs+字典序路径输出)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem? id=2488 ----- ...
- pku 2488 A Knight's Journey (搜索 DFS)
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28697 Accepted: 98 ...
- POJ 2488 -- A Knight's Journey(骑士游历)
POJ 2488 -- A Knight's Journey(骑士游历) 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. 经典的“骑士游历”问题 ...
- poj 2488 A Knight's Journey( dfs )
题目:http://poj.org/problem?id=2488 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. #include <io ...
- POJ 2488-A Knight's Journey(DFS)
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31702 Accepted: 10 ...
- POJ 2488 A Knight's Journey(DFS)
A Knight's Journey Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 34633Accepted: 11815 De ...
- POJ 2488 A Knight's Journey(深搜+回溯)
A Knight's Journey Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) ...
- POJ 2488 A Knight's Journey (回溯法 | DFS)
题目链接:http://poj.org/problem?id=2488 题意: 在国际象棋的题盘上有一个骑士,骑士只能走“日”,即站在某一个位置,它可以往周围八个满足条件的格子上跳跃,现在给你一个p ...
- 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 ...
随机推荐
- eclipse文本编码格式修改为UTF-8
1.windows->Preferences...打开"首选项"对话框,左侧导航树,导航到general->Workspace,右 侧Text file encodin ...
- ServletConfig对象和ServletContext对象
(1)ServletConfig:用来保存一个Servlet的配置信息的(比如 : name, class, url ... ) 这些配置信息没什么大用处,我们还可以在ServletConfig中保存 ...
- 线性表之顺序表C++实现
线性表之顺序表 一.头文件:SeqList.h //顺序线性表的头文件 #include<iostream> ; //定义顺序表SeqList的模板类 template<class ...
- [WC2014]时空穿梭(莫比乌斯反演)
https://www.cnblogs.com/CQzhangyu/p/7891363.html 不难推到$\sum\limits_{D=1}^{m_1}\sum\limits_{d|D}C_{d-1 ...
- SQL SERVER中获取表间主外键关系
sql server 2008中的主外键关系获取方式: 转自:http://www.cnblogs.com/ke10/archive/2012/06/11/2544655.html SELECT OB ...
- 【洛谷】NOIP提高组模拟赛Day1【组合数学】【贪心+背包】【网络流判断是否满流以及流量方案】
U41568 Agent1 题目背景 2018年11月17日,中国香港将会迎来一场XM大战,是世界各地的ENLIGHTENED与RESISTANCE开战的地点,某地 的ENLIGHTENED总部也想派 ...
- BZOJ 3751: [NOIP2014]解方程 数学
3751: [NOIP2014]解方程 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3751 Description 已知多项式方程: ...
- SCC缩点
int V; //顶点数量 vector<int> G[max_v]; //图的邻接表表示方法 vector<int> rG[max_v]; //把边反向建的图 vector& ...
- cocos2dx 字符串拼接
;i<;i++){ ]; sprintf(str,"%d",i); ]; strcpy(totalFilename, "game_loading") ; ...
- c++ 常见网络协议头
//NTP协议 typedef struct _NTP_HEADER { uint8_t _flags;//Flags 0xdb uint8_t _pcs;//Peer Clock Stratum u ...