博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2488 A Knight's Journey(DFS)
阅读量:5345 次
发布时间:2019-06-15

本文共 3254 字,大约阅读时间需要 10 分钟。

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

31 12 34 3

Sample Output

Scenario #1:A1Scenario #2:impossibleScenario #3:A1B3C1A2B4C2A3B1C3A4B2C4

题目大意:在棋盘上骑士要走遍棋盘上所有的位置,如果可以走完输出按字典序!!!排序的路径,否则输出impossible。

分析:dfs,这里特别注意国际象棋中骑士是按日字跳跃的,所以有8个方向可以行走,这里要输出按字典序排序的路径,那么可以直接找出所有可以到达的路径再进行排序,但是这样会超时,因为当棋盘很大的时候,走法很多。那么其实有个更巧妙的办法,那就是开始的时候把跳跃的8个方向的顺序按字典序排序,那么第一次成功时的路径就是字典序最小的路径。

int dx[] = {-1, 1, -2, 2, -2, 2, -1, 1};//按字典顺序定义好行走方向

int dy[] = {-2, -2, -1, -1, 1, 1, 2, 2};

代码:

#include
#include
#include
using namespace std;const int N = 30;typedef struct { int x; int y;} P;P que[1000];int cur = 0; int n, m;int vis[N][N];int dx[] = {-1, 1, -2, 2, -2, 2, -1, 1};//按字典顺序定义好行走方向 int dy[] = {-2, -2, -1, -1, 1, 1, 2, 2};int dfs(int x, int y, int num) { int flag = 0; if(num == n*m) { printf("A1"); for(int i = 0; i < cur; i++) { printf("%c%d", 'A'+que[i].y, que[i].x + 1); } printf("\n\n"); return 1; } for(int i = 0; i < 8; i++) { int nx = x + dx[i], ny = y + dy[i]; if(nx >= 0 && nx < n && ny >= 0 && ny < m && !vis[nx][ny]) { vis[nx][ny] = 1; que[cur].x = nx; que[cur].y = ny; cur++; flag = dfs(nx, ny, num+1); vis[nx][ny] = 0; cur--; if(flag) return flag; } } return flag;}int main() { int t; scanf("%d", &t); for(int i = 1; i <= t; i++) { scanf("%d%d", &n, &m); vis[0][0] = 1; printf("Scenario #%d:\n", i); if(!dfs(0, 0, 1)) { printf("impossible\n\n"); } } return 0;}

转载于:https://www.cnblogs.com/kindleheart/p/9296931.html

你可能感兴趣的文章
BZOJ 1925: [Sdoi2010]地精部落( dp )
查看>>
c++中的string常用函数用法总结!
查看>>
[DLX精确覆盖+打表] hdu 2518 Dominoes
查看>>
SuperMap iServerJava 6R扩展领域开发及压力测试---判断点在那个面内(1)
查看>>
Week03-面向对象入门
查看>>
一个控制台程序,模拟机器人对话
查看>>
web.xml 中加载顺序
查看>>
pycharm激活地址
查看>>
hdu 1207 四柱汉诺塔
查看>>
Vue 2.x + Webpack 3.x + Nodejs 多页面项目框架(上篇——纯前端多页面)
查看>>
我的PHP学习之路
查看>>
【题解】luogu p2340 奶牛会展
查看>>
对PostgreSQL的 SPI_prepare 的理解。
查看>>
解决响应式布局下兼容性的问题
查看>>
使用DBCP连接池对连接进行管理
查看>>
【洛谷】【堆+模拟】P2278 操作系统
查看>>
hdu3307 欧拉函数
查看>>
Spring Bean InitializingBean和DisposableBean实例
查看>>
Solr4.8.0源码分析(5)之查询流程分析总述
查看>>
[Windows Server]安装系统显示“缺少计算机所需的介质驱动程序”解决方案
查看>>