poj 2488 A Knight's Journey(dfs+字典序路径输出)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents
id=2488
----------------------------------------------------------------------------------------------------------------------------------------------------------
欢迎光临天资小屋:http://user.qzone.qq.com/593830943/main
----------------------------------------------------------------------------------------------------------------------------------------------------------
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
大致题意:
给出一个国际棋盘的大小,推断马是否能不反复的走过全部格,并记录下当中按字典序排列的第一种路径。
代码例如以下:
#include <cstdio>
#include <cstring>
#define M 26
struct node
{
int x, y;
}w[M*M];
bool vis[M][M];
int p, q;
int flag = 0;
int dir[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};
//按此顺序搜索出来的结果就是字典序
bool judge(int x, int y)
{
if(x>=0&&x<q&&y>=0&&y<p&&!vis[x][y])
return true;
return false;
}
void dfs(int x, int y, int step)
{
w[step].x = x,w[step].y = y;
vis[x][y] = true;
if(step == p*q-1)
{
flag = 1;
return ;
}
for(int i = 0; i < 8; i++)
{
int dx = w[step].x+dir[i][0];
int dy = w[step].y+dir[i][1];
if(judge(dx,dy))
{
vis[dx][dy] = true;
dfs(dx,dy,step+1);
if(flag)//一但找到就退出搜索
return;
vis[dx][dy] = false;
}
}
return ;
}
void print()
{
for(int i = 0; i < p*q; i++)//列为字母,行为数字
{
printf("%c%d",w[i].x+'A',w[i].y+1);
}
printf("\n\n");
}
int main()
{
int t, i, j, cas = 0;
scanf("%d",&t);
while(t--)
{
memset(vis,false,sizeof(vis));
flag = 0;
scanf("%d%d",&p,&q);
for(i = 0; i < q; i++)//列
{
for(j = 0; j < p; j++)//行
{
dfs(i,j,0);
if(flag)
break;
}
if(flag)
break;
}
printf("Scenario #%d:\n",++cas);
if(flag)
print();
else
printf("impossible\n\n");
}
return 0;
}
poj 2488 A Knight's Journey(dfs+字典序路径输出)的更多相关文章
- POJ 2488 A Knight's Journey
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29226 Accepted: 10 ...
- poj2488--A Knight's Journey(dfs,骑士问题)
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31147 Accepted: 10 ...
- 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 【骑士周游 dfs + 记忆路径】
题目地址:http://poj.org/problem?id=2488 Sample Input 3 1 1 2 3 4 3 Sample Output Scenario #1: A1 Scenari ...
- [poj]2488 A Knight's Journey dfs+路径打印
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45941 Accepted: 15637 Description Bac ...
随机推荐
- String与StringBuffer的差别
String 是一个常量,即一旦创建不可更改 输出结果为:helloworldjeok 看似.string变量name的值改变了,事实上此name非彼name. 输出结果为: sex:hello wo ...
- android:layout_gravity 和android:gravit的区别?
Android:layout_gravity 和android:gravit的区别? android:gravity是调整元素本身的内容或元素包含的子元素显示的位置,默认是显示在左侧 android: ...
- webview同步cookies
目前很多android app都内置了可以显示web页面的界面,会发现这个界面一般都是由一个叫做WebView的组件渲染出来的,学习该组件可以为你的app开发提升扩展性. 先说下WebView的一些优 ...
- 【转】iOS 设置APP的名称(浅述APP版本国际化与本地化)
原文网址:http://www.jianshu.com/p/a3a70f0398c4 前言 App的名字设置方式有很多种,如果在App打包上线时不做修改,最终App的名字就是Xcode在建立工程时的名 ...
- webstorm配置Monokai-Sublime.jar主题
https://github.com/OtaK/jetbrains-monokai-sublime 导入下载的Monokai-Sublime.jar jar包即可使用.
- 如何在ubuntu中安装mysql与mysql workbench
安装过程如下 sudo apt-get install mysql-server 安装过程中随后设置mysql的密码 之后sudo apt-get install mysql-client 安装好之后 ...
- Maven 学习笔记(一)
定义 Maven 是基于项目对象模型(POM)的软件项目管理工具,它采用纯 java 编写,用于管理项目的构建,最早在 Jakata Turbine 项目中开始被使用.它包含了一个项目对象模型(Pro ...
- QT-自定义信号和槽
前言:信号和槽是一种松耦合机制,或者说是一种分布式机制,信号广播出去,槽会自定义订阅接收. 一.新建工程 二.新建部件 拖入button按钮.修改内容为“发送自定义信号” 三.自定义发送信号 3.1 ...
- C#将内容导出到Word到指定模板
昨天做了下导入导出Excel文件,今天研究了下导出Word文件. 从网上找了半天才找到了一个能导出到指定模板的,在这里总结下. 导出模板原理就是利用的替换占位符. 我这里先建立好了一个模板, 接下来写 ...
- line-height与间距总总
一点说明(个人吐槽,可以略过) 之所以想写这篇文章,是因为自己工作的经验总结.以前的页面编写极度不注重间距大小,特别是行级元素间距.认为只要差不多好就行了,没必要花那么大的精力去抠几px的小细节.事实 ...