Day2-F-A Knight's Journey POJ-2488
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的棋盘,求出每个点恰好只走一次的路径,若有多个答案输出字典序最小的。
分析:求最深路径,只经过一次,DFS+回溯,注意到如果其能满足题意,那么必然会经过A1,从A1开始字典序最小,所以从A1开始DFS搜索即可,注意保证dx与dy也是字典序排序,代码如下:
const int maxm = ;
//注意字典序大小排序
const int dx[] = {-, , -, , -, , -, };
const int dy[] = {-, -, -, -, , , , }; int vis[maxm][maxm], Next[maxm][maxm], r, c, n, kase = ; bool inside(int x,int y) {
return x > && x <= r && y > && y <= c;
} void print(int x,int y,int t) {
if(t) {
printf("%c%d", y - + 'A', x);
print(Next[x][y] / , Next[x][y] % , t - );
}
} bool dfs(int x,int y,int t) {
vis[x][y] = ;
if(t == r * c) {
return true;
}
for (int i = ; i < ; ++i) {
int nx = x + dx[i], ny = y + dy[i];
if(inside(nx,ny) && !vis[nx][ny]) {
Next[x][y] = nx * + ny;
if(dfs(nx, ny,t+)) {
return true;
}
}
}
vis[x][y] = ;
return false;
} int main() {
scanf("%d", &n);
while(n--) {
scanf("%d%d", &r, &c);
memset(vis, , sizeof(vis)), memset(Next, , sizeof(Next));
printf("Scenario #%d:\n", ++kase);
if(dfs(, , )) {
print(,,r*c);
} else {
printf("impossible");
}
printf("\n\n");
}
return ;
}
Day2-F-A Knight's Journey POJ-2488的更多相关文章
- 迷宫问题bfs, A Knight's Journey(dfs)
迷宫问题(bfs) POJ - 3984 #include <iostream> #include <queue> #include <stack> #incl ...
- 广大暑假训练1(poj 2488) A Knight's Journey 解题报告
题目链接:http://vjudge.net/contest/view.action?cid=51369#problem/A (A - Children of the Candy Corn) ht ...
- POJ 2488 -- A Knight's Journey(骑士游历)
POJ 2488 -- A Knight's Journey(骑士游历) 题意: 给出一个国际棋盘的大小,判断马能否不重复的走过所有格,并记录下其中按字典序排列的第一种路径. 经典的“骑士游历”问题 ...
- 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(深搜+回溯)
A Knight's Journey Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) ...
- POJ 2488 A Knight's Journey
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29226 Accepted: 10 ...
- POJ 2488:A Knight's Journey 深搜入门之走马观花
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35342 Accepted: 12 ...
- A Knight's Journey 分类: POJ 搜索 2015-08-08 07:32 2人阅读 评论(0) 收藏
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35564 Accepted: 12119 ...
- POJ2488-A Knight's Journey(DFS+回溯)
题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Tot ...
- POJ2488A Knight's Journey[DFS]
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 41936 Accepted: 14 ...
随机推荐
- python opencv:摄像头捕获图像
- C++ STL之集合set的使⽤
写在最前面,本文摘录于柳生笔记: set是集合,一个set里面个元素各不相同的,而且set会按照元素从小到大的进行排序,一下是set的常用方法:
- Laravel 6.X 数据库迁移 创建表 与 修改表
数据库迁移创建表 本篇文章中使用的是mysql数据库,其他数据库需要修改env文件和app配置,请其他地方搜索一下就会找到. 创建示例 1.创建users表: 命令行键入 php artisan ma ...
- i.MX RT600之DSP调试环境搭建篇
恩智浦的i.MX RT600是跨界处理器产品,同样也是i.MX RTxxx系列的开山之作.不同于i.MX RT1xxx系列单片机,i.MX RT600 采用了双核架构,将新一代Cortex-M33内核 ...
- python--脚本传参与shell脚本传参(位置参数)
写一个最简单的shell脚本,了解shell脚本是如何传参 1. vim test1.sh name=$1 age=$2 echo ${name} echo ${age} 2.调用脚本并传参 sh t ...
- RestTemplate的异常 Not enough variables available to expand
当使用 RestTemplate 可能会遇到异常: Not enough variables available to expand 典型如下: @Autowired private RestTemp ...
- ajax相同url和参数,将不会重复发起请求
IE浏览器下使用GET发送请求时,如果两次请求的地址和参数相同,在不刷新页面的情况下,浏览器会缓存第一次请求的内容,服务端更新后浏览器仍然显示第一次的内容. 解决办法: 一. GET请求URL后加随机 ...
- Elasticsearch 6.8.4 启动报错解决方法
运行环境:centos 7,jdk 1.8 问题一: ERROR: bootstrap checks failed max ] ] 原因:无法创建本地文件问题,用户最大可创建文件数太小 解决方案:切换 ...
- EOS主网搭建教程--&&--搭建节点--&&--搭建mongodb数据库
EOS主网搭建教程: 1.git clone https://github.com/EOS-Mainnet/eos.git --recursive 2.cd eos 3.git tag (查看有哪些分 ...
- Spring boot 2.x 中使用redis
一.添加Maven 依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifac ...