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 ...
随机推荐
- [转载] 你所不知道的TIME_WAIT和CLOSE_WAIT
前言 本文转载自 https://mp.weixin.qq.com/s/FrEfx_Yvv0fkLG97dMSTqw.很久前看到Vincent Bernat在博客中写了一遍关于TCP time-wai ...
- 【基础知识】Asp.Net基础三
服务器端控件一般用于访问量不高的网站,要做到物尽其用. 服务器端控件: FIleUpload控件:向服务器上传文件 if (this.FileUpload1.HasFile) { // Path.Ge ...
- 机器学习之路: python 实践 word2vec 词向量技术
git: https://github.com/linyi0604/MachineLearning 词向量技术 Word2Vec 每个连续词汇片段都会对后面有一定制约 称为上下文context 找到句 ...
- 学好js,这些js函数概念一定要知道
函数创建方式 1.声明方式 例如:function consoleTip (){ console.log("tip!"); } 2.表达式方式 例如:var consoleTip ...
- luoguP4466 [国际集训队]和与积 莫比乌斯反演
自然想到枚举\(gcd(a, b)\),不妨设其为\(d\),并且\(a = di, b = dj(a > b)\) 那么\(\frac{ab}{a + b} = \frac{dij}{i + ...
- Lunix/Mac下根据最后修改时间复制文件和文件夹,保持原有的目录结构
度娘知道:http://zhidao.baidu.com/link?url=DD47jm6qDgT7yxsnz9e-NC4Fqd33oRoiIwcGLkw5TL4cbf50VKY2IONbHKH0IE ...
- 预防Redis缓存穿透、缓存雪崩解决方案
最近面试中遇到redis缓存穿透.缓存雪崩等问题,特意了解下. redis缓存穿透: 缓存穿透是指用户查询数据,在数据库没有,自然在缓存中也不会有.这样就导致用户查询的时候,在缓存中找不到,每次都要去 ...
- better-scroll不生效原因
在vue项目中运用better-scroll插件进行上拉加载的功能时,页面拉不动. html结构: <div class="wrapper" ref="wrappe ...
- SVN服务器与客户端下载地址_搭建使用
下载地址: http://subversion.apache.org/packages.html Windows CollabNet (supported and certified by Colla ...
- 电感式DC/DC变换器工作原理
http://www.amobbs.com/thread-3293203-1-1.html 首先必须要了解电感的一些特性:电磁转换与磁储能.其它所有参数都是由这两个特性引出来的. 电感回路通电瞬间 断 ...