POJ2488 A Knight's Journey(dfs)

A Knight‘s Journey(小白友好详解) A Knight's Journey 有AC代码 阅读详情

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


国际象棋中马的走法,比较棘手的是题目要求输出路径,并且是按照字典序,故需要从第一个格开始。


AC代码:


#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
struct node
{
	/* data */
	int row;
	char col;
};
int p, q, x, y;
bool map['Z' + 1][27];
void path(int i, int j, int num)
{
	switch(num) {
		case 1: {
			x = i - 1;
			y = j - 2;
			break;
		}
		case 2: {
			x = i + 1;
			y = j - 2;
			break;
		}
		case 3: {
			x = i - 2;
			y = j - 1;
			break;
		}
		case 4: {
			x = i + 2;
			y = j - 1;
			break;
		}
		case 5: {
			x = i - 2;
			y = j + 1;
			break;
		}
		case 6: {
			x = i + 2;
			y = j + 1;
			break;
		}
		case 7: {
			x = i - 1;
			y = j + 2;
			break;
		}
		case 8: {
			x = i + 1;
			y = j + 2;
		}
	}
}
bool dfs(node* a, int i, int j, int step) 
{
	map[i][j] = true;
	a[step].row = i;
	a[step].col = j;
	if(step == a[0].row) return true;
	for(int k = 1; k <= 8; ++k) {
		path(i, j, k);
		int ii = x, jj = y;
		if(!map[ii][jj] && ii >= 1 && ii <= p && jj >= 'A' && jj <= 'A' + q - 1)
			if(dfs(a, ii, jj, step + 1)) return true;
	}
	map[i][j] = false;
	return false;
}
int main(int argc, char const *argv[])
{
	int n;
	cin >> n;
	int t = 1;
	while(t <= n) {
		memset(map, false, sizeof(map));
		cin >> p >> q;
		if(p == 1 && q == 1) {
			cout << "Scenario #" << t++ << ":" << endl;
			cout << "A1" << endl << endl;
			continue;
		}
		node a[p * q + 1];
		a[0].row = p * q;
		bool flag = false;
		for(int j = 'A'; j <= 'A' + q - 1; ++j) {
			for(int i = 1; i <= p; ++i)
				if(dfs(a, i, j, 1)) {
					cout << "Scenario #" << t++ << ":" << endl;
					for(int k = 1; k <= a[0].row; ++k)
						cout << a[k].col << a[k].row;
					cout << endl << endl;
					flag = true;
					break;
				}
			if(flag) break;
		}
		if(!flag) {
			cout << "Scenario #" << t++ << ":" << endl;
			cout << "impossible" << endl << endl;
		}
	}
	return 0;
}


POJ2488-A Knight's Journey【骑士游历】 北大POJ2488-A Knight's Journey 解题报告+AC代码 立即下载

相关推荐

Simulink标定工作流优化:当A2L文件遇到ELF,如何用Matlab脚本打通‘最后一公里’?

本文探讨了Simulink标定工作流中A2L与ELF文件处理的自动化解决方案。通过Matlab脚本实现A2L文件智能合并与ELF文件自动匹配,解决了传统手动操作效率低下和易出错的问题,显著提升汽车ECU开发效率。重点介绍了脚本实现细节及持续集成系统集成方案。

weixin_30564785的博客 338

A Knight's Journey

A - A Knight's Journey Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2488 Description Background  The knight is getting bor

CSUST_ACM的专栏 862

软件工程大作业——图书管理系统/图书个性化推荐与实现系统

软件工程期末大作业,图书管理系统,包含ER图、程序流程图、属性图、结构图

2401_82857325的博客 3648

骑士的旅行

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...

u013345179的博客 580

Transformer模型在语义通信中的实战应用:从信源编码到端到端优化

本文深入探讨了Transformer模型在语义通信中的实战应用,从信源编码到端到端优化。文章分析了Transformer凭借其强大的全局依赖建模能力,如何成为实现高效语义提取与传输的理想架构,并提供了构建可端到端训练系统的具体实现思路与评估方法,旨在为下一代智能通信系统提供技术参考。

o9p0q1的博客 690

基于RK3588与昇腾310B的异构K8s集群部署与NPU统一调度实战

本文介绍了基于RK3588与昇腾310B的异构K8s集群部署方案,通过HAMi+Volcano实现NPU统一调度。方案包含硬件配置、操作系统准备(银河麒麟/欧拉)、容器运行时部署及K8s集群搭建等关键步骤,支持多厂商NPU设备的统一抽象和精细化切分,为边缘AI计算提供高效资源管理能力。

huanghm88的专栏 280

【POJ 2488】A Knight‘s Journey 题解(深度优先搜索)

骑士厌倦了一次又一次地看到同样的黑白方块,决定去旅行全世界每当骑士移动时,它是一个方向上的两个正方形和一个垂直于这个方向的正方形。骑士的世界就是他生活的棋盘。我们的骑士生活在一个棋盘上,这个棋盘的面积比普通的8*8棋盘小,但它仍然是矩形的。你能帮助这位冒险的骑士制定旅行计划吗?问题找到一条道路,让骑士访问每个广场一次。骑士可以在棋盘的任何一方开始和结束。输入输入以第一行中的正整数n开始。以下行包含n个测试用例。每个测试用例都由一条包含两个正整数p和q的单行线组成,因此1

HEX9CF的技术博客 978

正确解决Authentication plugin ‘caching _sha2_password’ cannot be loaded:异常的有效解决方法

正确解决Authentication plugin ‘caching _sha2_password’ cannot be loaded:异常的有效解决方法

wbajsjhhhhh的博客 1万+

POJ2488 骑士游历 A Knight's Journey 解题报告

这个题有3点要注意的地方。 1、弄清题意,p指行号,从上到下依次是1,2,3...p;q指列号,从左到右依次是A,B,C,D...,字典序输出意思是先按列排序,左边的在前,同一列的按行排序,行号小的在前。 2、递归遇到某个方块的下一步无处可跳时,返回上层之前要把vis矩阵相应位置清零!还有起始位置就是A1,如果A1无解则不需要再遍历别的情况了,直接输出impossible即可。 3、输出的时

monkeyduck的专栏 1417

杰理接入第三方升级协议OTA教程

本教程介绍了使用dual_bank_updata_api.h接口接入第三方协议进行固件升级的方法。主要内容包括:INI配置要求(需启用双备份并强制4K对齐)、7个核心API接口功能说明及参数详解(初始化、空间检查、数据写入、校验等)、标准升级流程步骤(初始化→检查→写入→校验→引导→清理)。特别强调了4K对齐配置的重要性以避免数据丢失,并提供了被动升级流程图。该方案支持用户自定义协议接入,需严格按照流程调用接口完成升级。

qq_42014563的博客 1429

MAVLink协议详解

MAVLink是为微型飞行器MAV(Micro Air Vehicle)设计的(LGPL)开源的通讯协议。是无人飞行器和地面站(Ground Control Station ,GCS)之间,以及无人飞行器之间通讯常用的协议。APM、PIXHAWK飞控,Mission Planner、QGroundControl地面站均使用了MAVLink协议进行通讯。 MAVLink源码下载地址(现已更新至v2.0):https://github.com/mavlink/qgroundcontrol   用户手册:ht

程序猿老樊的博客 7704

POJ 2488 A Knight's Journey DFS(深度优先搜索)

A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 53688 Accepted: 18228 Description Background The knight is getting bored of seeing the same black an...

小渣渣的博客 279

【Windows Server 2019】企业虚拟专用网络服务的配置和管理(上)

博文介绍了部署企业虚拟专用网的定义,目的,以及好处。同时说明了实验拓扑图以及虚拟专用网络的工作原理。

VMware, Windows, Linux, Cisco, Container, Kubernetes, Kali 1万+

【DFS】POJ 2488 A Knight's Journey

这段时间要沉迷刷题一段时间了,就让优快云陪我一起吧! 一、题目大意 这道题的大致意思就是,给你一个p×q的矩阵,矩阵的行标号为1-p,矩阵的列标号为’A’ - ‘A’ + q,一个骑士要经过这个矩阵的所有格子,他可以从任意一个点开始,并且可以终止于任意点,要求所有可能路径中字典序最小的那条路径。 骑士的行走方式如下: 看着有点类似于象棋中的马。 二、题目思路以及AC代码 这道题也比较简单,...

Johnson的博客 244

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

POJ2488 A Knight's Journey【DFS】

A Knight’s Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 58714 Accepted: 19998 Description Background The knight is getting bored of seeing the same black and white squares agai...

海岛Blog 525

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

POJ2488-A Knight's Journey(DFS)

POJ2488 原题链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 46885   Accepted: 15967 Description Backg

Yorozuya 381

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

POJ 2488 A Knight's Journey【dfs】

题目链接http://poj.org/problem?id=2488思路题意就是给你p*q的棋盘,行号1~p,列号A~A+q,问你一个马走遍整个棋盘的字典序最小的路径。那就遍历起点,列优先,行其次,然后深搜,深搜的分支也要列小的优先,列一样的行小的优先。然后把路径保存下来,能行的话直接返回就行。但是这么玩的话时间复杂度咋一想是26×26×826×2626\times 26\times 8^{26\t

初涉_代码世界 520

POJ 2488 A Knight's Journey【DFS】

补个很久之前的题解。。。。 题目链接: http://poj.org/problem?id=2488 题意: 马走“日”字,让你为他设计一条道路,走遍所有格,并输出字典序最小的一条。 分析: dfs~~~ 代码: #include<iostream> #include<cstring> #include<algorit...

An55511的博客 117

POJ2488 A Knight's Journey[dfs]

D - A Knight's Journey Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description Background The knight is getting bored of seeing the sa

ControlBear的博客 539

poj 2488 A Knight's Journey (DFS)

A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34660   Accepted: 11827 Description Background  The knight is getting bored of seeing t

ysCover 851

POJ2488 A Knight's Journey 骑士巡游 DFS

DescriptionBackground 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 s

Bill_Utada的成长之路 1696

POJ 2488 A Knight's Journey【DFS + 回溯应用】

原题链接:http://poj.org/problem?id=2488 我的链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=19651#problem/A A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total

free斩 2484

POJ 2488 - A Knight‘s Journey + Python

这是一个深度优先搜索DFS题目。 原题连接:2488 -- A Knight's Journey 参考资料:POJ 2488 - A Knight's Journey | 眈眈探求 参考资料:POJ2488-A Knight's Journey【骑士游历】_ζёСяêτ - 小優YoU-优快云博客 参考资料:POJ-2488-A Knight's Journey_fuerbosi-优快云博客 一问题描述 骑士按照象棋中“马走日”的原则在棋盘行走,直到不重复地走完整个棋盘,并且输出“字典顺序.

xxdragon126的博客 690

POJ2488 A Knight's Journey DFS

题目大意:一个p×q的棋盘,问你一个骑士(走法相当于中国象棋中的马)能否遍历棋盘上的每一点?如果可以,输出字典序最小的一组路径,否则输出“impossible”。 分析:典型的骑士周游问题,直接DFS就行,注意一下方向数组的定义就行,因为要按字典序输出路径,我们可以按字典序遍历,这样,第一次成功的遍历就是答案了。 实现代码如下: #include #include #include u

AC_Gibson的专栏 625

DFS poj2488 A Knight's Journey

很经典的搜索+最小字典序路径打印 巧妙的利用LAST结构体,来保存上一个节点的位置,最后再递归输出 #include #include #include #include #include #include #include using namespace std; typedef long long LL; typedef pair PII; const int MX = 30; con

qwb的博客 763

POJ2488——DFS——A Knight's Journey

Description BackgroundThe knight is getting bored of seeing the same black and white squares again and again and has decided to make a journeyaround the world. Whenever a knight moves, it is t...

dgjm4087的博客 182
上一篇: POJ1011 Sticks(dfs)
下一篇: POJ1321 棋盘问题(dfs)
GKHack
博客等级 码龄11年 48粉丝 291原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值