A Knight's Journey(dfs)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 25950 | Accepted: 8853 |
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
Output
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 题意:给出p和q,p代表行数(1,2,3....),q代表列数(A,B,C....),要求输出骑士从任意一点出发经过所有点的路径,必须按字典序输出;路径不存在输出impossible; 思路:与dfs模板不同的是路径按字典序输出,所以dfs的顺序就不是随意的了,必须按dir[8][2] = {{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}}的顺序;
而且起点必须是A1,这样得出的路径字典序才最小;
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std; struct node
{
int row;
int col;
}way[];//记录所走路径的行和列 int p,q;
bool vis['Z'+][];
int dir[][] = {{-,-},{,-},{-,-},{,-},{-,},{,},{-,},{,}}; bool DFS(struct node* way,int i,int j,int step)
{
vis[i][j]=true;
way[step].row=i;
way[step].col=j;
if(step==way[].row)
return true; for(int k=; k<; k++)//向八个方向走
{
int ii = i+dir[k][];
int jj = j+dir[k][];
if(!vis[ii][jj] && ii>= && ii<=p && jj>= && jj<=q)
if(DFS(way,ii,jj,step+))
return true;
} vis[i][j]=false;
return false;
} int main()
{
int test;
scanf("%d",&test);
for(int t = ; t <= test; t++)
{
memset(vis,false,sizeof(vis));
scanf("%d %d",&p,&q); way[].row =p*q; if(DFS(way,,,))
{
cout<<"Scenario #"<<t<<':'<<endl; for(int k=; k<=way[].row; k++)
cout<<(char)(way[k].col-+'A')<<way[k].row;
cout<<endl<<endl; } else
{
cout<<"Scenario #"<<t<<':'<<endl;
cout<<"impossible"<<endl<<endl;
}
}
return ;
}
A Knight's Journey(dfs)的更多相关文章
- POJ2488A Knight's Journey[DFS]
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 41936 Accepted: 14 ...
- 迷宫问题bfs, A Knight's Journey(dfs)
迷宫问题(bfs) POJ - 3984 #include <iostream> #include <queue> #include <stack> #incl ...
- POJ2488:A Knight's Journey(dfs)
http://poj.org/problem?id=2488 Description Background The knight is getting bored of seeing the same ...
- [poj]2488 A Knight's Journey dfs+路径打印
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45941 Accepted: 15637 Description Bac ...
- POJ2248 A Knight's Journey(DFS)
题目链接. 题目大意: 给定一个矩阵,马的初始位置在(0,0),要求给出一个方案,使马走遍所有的点. 列为数字,行为字母,搜索按字典序. 分析: 用 vis[x][y] 标记是否已经访问.因为要搜索所 ...
- POJ2488-A Knight's Journey(DFS+回溯)
题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Tot ...
- POJ 2488 A Knight's Journey(DFS)
A Knight's Journey Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 34633Accepted: 11815 De ...
- A Knight's Journey 分类: dfs 2015-05-03 14:51 23人阅读 评论(0) 收藏
A Knight’s Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34085 Accepted: 11621 ...
- poj2488 A Knight's Journey裸dfs
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35868 Accepted: 12 ...
随机推荐
- Java 国际化 语言切换
Java国际化 我们使用java.lang.Locale来构造Java国际化的情境. java.lang.Locale代表特定的地理.政治和文化.需要Locale来执行其任务的操作叫语言环境敏感的 ...
- Bash算术计算
1:$(( )) 2:$[ ] 3:`expr $x + $y` 4:bc命令 #!/bin/bash x= y= echo $(( x + y )) echo $[ $x + $y ] echo ` ...
- 使用WebSocket构建实时WEB
为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/3795075.html ...
- HTML5 文件域+FileReader 分段读取文件并上传(七)-WebSocket
一.单文件上传实例 HTML: <div class="container"> <div class="panel panel-default" ...
- java可变参数Varargs
http://www.cnblogs.com/shishm/archive/2012/01/31/2332656.html J2SE 1.5提供了“Varargs”机制.借助这一机制,可以定义能和多个 ...
- angularjs sortbale
参考地址:http://kamilkp.github.io/angular-sortable-view 案例:jsp: <div sv-root sv-part="vm.dataLis ...
- Difference Between XML and XAML.
XML, or Extensible Markup Language, is a subset of the more complex SGML (Standard Generalized Mark ...
- 获取C++类成员变量的地址偏移
今天有在校学生问怎么获取类中的成员变量的地址偏移量,这个应该是很多初学C++的人很好奇的问题.以前我在学校的时候,也有过这种需求.忘了当时是要写什么“奇怪的程序”了,反正需要获取一个类的成员变量的地址 ...
- 51nod建设国家
小C现在想建设一个国家.这个国家中有一个首都,然后有若干个中间站,还有若干个城市. 现在小C想把国家建造成这样的形状:选若干(可以是0个)的中间站把他们连成一条直线,然后把首都连在这一条直线的左端.然 ...
- SGU 188.Factory guard
模拟 code #include <iostream> #include <cstdio> #define LEN 1000 using namespace std; int ...