poj2488--A Knight's Journey(dfs,骑士问题)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 31147 | Accepted: 10655 |
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
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
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
Source
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int t , n , m , sum ;
int mm[30][30] ;
int pre[1000] ;
int a[8][2] = { {-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1} };
int dfs(int x,int y,int temp)
{
if( temp == sum )
return 1 ;
int flag = 0 , xx , yy , i ;
for(i = 0 ; i < 8 ; i++)
{
xx = x + a[i][0] ;
yy = y + a[i][1] ;
if( xx >= 0 && xx < n && yy >= 0 && yy < m && !mm[xx][yy] )
{
mm[xx][yy] = 1 ;
pre[x*m+y] = xx*m+yy ;
flag = dfs(xx,yy,temp+1);
if( flag ) return flag ;
mm[xx][yy] = 0 ;
}
}
return flag ;
}
int main()
{
int i , j , k , tt ;
scanf("%d", &t);
for(tt = 1 ; tt <= t ; tt++)
{
memset(mm,0,sizeof(mm));
memset(pre,-1,sizeof(pre));
scanf("%d %d", &m, &n);
sum = n*m ;
mm[0][0] = 1 ;
k = dfs(0,0,1);
printf("Scenario #%d:\n", tt);
if(k == 0)
printf("impossible");
else
{
for(i = 0 , k = 0 ; i < sum ; i++)
{
printf("%c%c", k/m+'A', k%m+'1');
k = pre[k] ;
}
}
printf("\n\n");
}
return 0;
}
poj2488--A Knight's Journey(dfs,骑士问题)的更多相关文章
- POJ 2488-A Knight's Journey(DFS)
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31702 Accepted: 10 ...
- DFS深搜——Red and Black——A Knight's Journey
深搜,从一点向各处搜找到全部能走的地方. Problem Description There is a rectangular room, covered with square tiles. Eac ...
- 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(dfs+字典序路径输出)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem? id=2488 ----- ...
- 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)
http://poj.org/problem?id=2488 Description Background The knight is getting bored of seeing the same ...
- poj2488 A Knight's Journey裸dfs
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35868 Accepted: 12 ...
- 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 ...
- POJ2488A Knight's Journey[DFS]
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 41936 Accepted: 14 ...
随机推荐
- android滚动图片
关于广告轮播,大家肯定不会陌生,它在现手机市场各大APP出现的频率极高,它的优点在于"不占屏",可以仅用小小的固定空位来展示几个甚至几十个广告条,而且动态效果很好,具有很好的用户& ...
- IOS学习笔记1—Iphone程序运行流程
Iphone程序运行流程 main.m文件,iOS应用程序的主入口 main函数的两个参数为命令行参数,在ios开发中不会用到这些元素,包括这两个参数是为了与标准ANSI C保持一致 UIApplic ...
- encodeURI()与decodeURI()等转码方法
只针对文本编码 encodeURI() 只针对文本解码 decodeURI()针对文本和特殊字符的编码 encodeURIComponent()针对文本和特殊字符的解码 decodeURIComp ...
- 关于Servlet一些东西
---- 概念 Servlet是一种服务器端的Java应用程序,具有独立于平台和协议的特性,可以生成动态的Web页面. 它担当客户请求(Web浏览器或其他HTTP客户程序)与服务器响应(HTTP服务器 ...
- mac rar文件解压缩
在下载文件时经常遇到RAR格式的压缩文件, 之前从APP Store下载了免费的解压软件, 但是总觉着不好用, 广告信息很多. 好用的软件都要花钱, 所以找到了命令行解决的办法. 步骤如下: 首先需要 ...
- maven打包oracle jdbc驱动
背景 由于版权问题,maven中央仓库缺少oracle jdbc的驱动,这个给开发带来了很多不便利性.也出现各种各样的解决方案,基本就两种思路: 将oracle驱动安装到本地仓库,这个需要大家统一好名 ...
- Python-接口自动化(十一)
配置文件的作用(十一) (十二)配置文件 1.python当中有一个模块可以读取配置文件里面的信息:configparser,对这个模块进行导入之后就可以使用了,import configparser ...
- 图论trainning-part-1 D. Going in Cycle!!
D. Going in Cycle!! Time Limit: 3000ms Memory Limit: 131072KB 64-bit integer IO format: %lld Ja ...
- Oracle中有关数学表达式的语法
Oracle中有关数学表达式的语法 三角函数 SIN ASIN SINHCOS ACOS COSHTA ...
- 用SQLLDR来装载date类型的控制文件
以前给山东某单位做oracle数据库恢复得时候,恢复出来得数据中包含date类型,当时给客户提供得是sqlldr得方式,因为数据量比较大,用sqlldr装载起来速度比较快,所以采用了这种方式,结果在装 ...