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学习笔记:分支 与 循环
if 语句 if 条件: ...... # 条件为真的时候,执行缩进的代码 if 条件: ...... # 条件为真的时候执行 else: ...... # 条件为假的时候执行 if 条件1: ... ...
- 【快学SpringBoot】快速上手好用方便的Spring Cache缓存框架
前言 缓存,在开发中是非常常用的.在高并发系统中,如果没有缓存,纯靠数据库来扛,那么数据库压力会非常大,搞不好还会出现宕机的情况.本篇文章,将会带大家学习Spring Cache缓存框架. 原创声明 ...
- 三分钟让你秒懂.Net生态系统
提到.Net的时候,大多数人的第一反应可能就是.Net Framework和Visual Studio..Net Framework的第一个版本发布与2002年2月13日,这对于科技发展日新月异的时代 ...
- kafka单机搭建
1.安装jdk1.8和zookeeper 2.下载kafka上传服务器 下载地址:http://archive.apache.org/dist/kafka/0.10.0.0/kafka_2.11-0. ...
- python上传文件接口
1.由于公司做接口测试,遇到了上传文件,一直搞了好久,原来是加了头部的原因def test_79(self): '''导入配置文件''' request = e['mysqlshujuku'] url ...
- pair node stack vector string priority_queue
multiset 元素重复 自动排序 map #include <bits/stdc++.h> using namespace std; map<int,int> s;//自当 ...
- 树莓派Raspberry实践笔记—显示分辨率配置
转载:http://www.cnblogs.com/atsats/p/6607886.html 如果未接显示设备,使用VNC登录后,显示分辨率很小,应该是480p,导致使用很不方便. 这里通过修改/b ...
- 利用ProxySQL实现MySQL的读写分离
本文简单介绍ProxySQL的安装及如果实现后端MySQL主从结构的读写分离. 一.ProxySQL安装 Proxy官方地址:https://proxysql.com/ proxysql-2.0.8- ...
- float元素上-margin的用法
css标准,float元素上的负margin表示把下面的元素向对应方向移动,并且覆盖上一个元素(这里是指html中元素的声明顺序). 标准情况下我们用float 时候是这样的. -margin通俗点说 ...
- WKWebView单个界面添加请求头
https://www.jianshu.com/p/14b9ea4bf1d4 https://github.com/Yeatse/NSURLProtocol-WebKitSupport/blob/ma ...