POJ 2488 A Knight's Journey(DFS)
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 ;
}
POJ 2488 A Knight's Journey(DFS)的更多相关文章
- POJ 2488 A Knight's Journey (DFS)
poj-2488 题意:一个人要走遍一个不大于8*8的国际棋盘,他只能走日字,要输出一条字典序最小的路径 题解: (1)题目上说的"The knight can start and end ...
- poj 2488 A Knight's Journey( dfs )
题目:http://poj.org/problem?id=2488 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. #include <io ...
- 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 题意: 马走"日"字,让你为他设计一条道路,走遍所有格,并输出字典序最小的一条 ...
- A Knight's Journey (DFS)
题目: Background The knight is getting bored of seeing the same black and white squares again and agai ...
- POJ 2488 -- A Knight's Journey(骑士游历)
POJ 2488 -- A Knight's Journey(骑士游历) 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. 经典的“骑士游历”问题 ...
- 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: 1000MS Memory Limit: 65536K Total Submissions: 31702 Accepted: 10 ...
- 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 ...
随机推荐
- 开发中遇到的问题(一)——java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
1.错误描述: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) wit ...
- C语言---类型转换
itoa 功 能:把一整数转换为字符串 用 法:char *itoa(int value, char *string, int radix); 详细解释:itoa是英文integer to array ...
- 转:代码的坏味道之二十 :Data Class(纯稚的数据类)或POJO
所谓Data Class是指:它们拥有一些值域(fields),以及用于访问(读写]这些值域的函数,除此之外一无长物.这样的classes只是一种「不会说话的数据容器」,它们几乎一定被其他classe ...
- 10个工具让你的 shell 脚本更强大
10个工具让你的 shell 脚本更强大 很多人误以为shell脚本只能在命令行下使用.其实shell也可以调用一些GUI组件,例如菜单,警告框,进度条等等.你可以控制最终的输出,光标位 置还有各种输 ...
- 【剑指offer】面试题30:最小的 k 个数
题目: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 思路: 这个是O(nlogk)时间复杂度的思路:用一个容器来保存最先 ...
- 如何在程序中调用Caffe做图像分类
Caffe是目前深度学习比较优秀好用的一个开源库,采样c++和CUDA实现,具有速度快,模型定义方便等优点.学习了几天过后,发现也有一个不方便的地方,就是在我的程序中调用Caffe做图像分类没有直接的 ...
- hdu 5428 The Factor(数学)
Problem Description There is a sequence of n positive integers. Fancycoder is addicted to learn thei ...
- linux使用共享内存通信的进程同步退出问题
两个甚至多个进程使用共享内存(shm)通信,总遇到同步问题.这里的“同步问题”不是说进程读写同步问题,这个用信号量就好了.这里的同步问题说的是同步退出问题,到底谁先退出,怎么知道对方退出了.举个例子: ...
- 后台数据导出为Excel
数据导出的方法如下: 一.下载office的类库:microsoft.office.interop.excel.zip 根据电脑安装的office版本选择引入相应的类库,office2007选择12. ...
- java遍历泛型的方法
一.List遍历 Java中List遍历有三种方法来遍历泛型,主要为: 1.for循环遍历 2.iterator遍历 3.foreach遍历 package com.gmail.lsgjzhuwei; ...