Shortest path of the king

从寄存器配置到G值:一份给STM32开发者的SC7A20加速度数据换算保姆级指南 本文为STM32开发者提供SC7A20三轴加速度传感器的数据换算保姆级指南,详细解析从寄存器配置到G值转换的全过程。涵盖量程选择、输出值换算原理及优化代码实现,帮助开发者快速掌握加速度数据的物理意义转换技巧,提升运动感知系统开发效率。 阅读详情
Shortest path of the king
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output

The king is left alone on the chessboard. In spite of this loneliness, he doesn't lose heart, because he has business of national importance. For example, he has to pay an official visit to square t. As the king is not in habit of wasting his time, he wants to get from his current position s to square t in the least number of moves. Help him to do this.

In one move the king can get to the square that has a common side or a common vertex with the square the king is currently in (generally there are 8 different squares he can move to).

Input

The first line contains the chessboard coordinates of square s, the second line — of square t.

Chessboard coordinates consist of two characters, the first one is a lowercase Latin letter (from a to h), the second one is a digit from 1to 8.

Output

In the first line print n — minimum number of the king's moves. Then in n lines print the moves themselves. Each move is described with one of the 8: L, R, U, D, LU, LD, RU or RD.

L, R, U, D stand respectively for moves left, right, up and down (according to the picture), and 2-letter combinations stand for diagonal moves. If the answer is not unique, print any of them.

Sample test(s)
input
a8
h1
output
7
RD
RD
RD
RD
RD
RD
RD

一个棋盘,用字母和数字表示横轴和竖轴,可以向八个方向移动,求最短路并写出路径。

最简单的想法就是模拟,首先最短路大小可以判断,就是横向和纵向的最大值,然后根据大小先斜着走,然后在上下左右走。

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int max(int a, int b)
{
	return a > b ? a : b;
}
int  min(int a, int b)
{
	return a < b ? a : b;
}
int main()
{
	int i, j, m, n, ans, pre1, pre2, temp1, temp2, temp;
	char a[2], b[2];
	scanf("%s", a);
	scanf("%s", b);
	pre1 = b[0] - a[0];pre2 = b[1] - a[1];
	temp1 = max(pre1, -pre1); temp2 = max(pre2, -pre2);
	ans = max(temp1, temp2);
	if (pre1 > 0 && pre2 > 0)
	{
		cout << ans << endl;
		temp = min(temp1, temp2);
		for (i = 1; i <= temp; i++)
			cout << "RU" << endl;
		if (temp1>temp2)
		for (i = temp + 1; i <= temp1; i++)
			cout << "R" << endl;
		else
		for (i = temp + 1; i <= temp2; i++)
			cout << "U" << endl;
	}
	else if (pre1 > 0 && pre2 < 0)
	{
		cout << ans << endl;
		temp = min(temp1, temp2);
		for (i = 1; i <= temp; i++)
			cout << "RD" << endl;
		if (temp1>temp2)
		for (i = temp + 1; i <= temp1; i++)
			cout << "R" << endl;
		else
		for (i = temp + 1; i <= temp2; i++)
			cout << "D" << endl;
	}
	else if (pre1 < 0 && pre2 < 0)
	{
		cout << ans << endl;
		temp = min(temp1, temp2);
		for (i = 1; i <= temp; i++)
			cout << "LD" << endl;
		if (temp1>temp2)
		for (i = temp + 1; i <= temp1; i++)
			cout << "L" << endl;
		else
		for (i = temp + 1; i <= temp2; i++)
			cout << "D" << endl;
	}
	else if (pre1<0 && pre2>0)
	{
		cout << ans << endl;
		temp = min(temp1, temp2);
		for (i = 1; i <= temp; i++)
			cout << "LU" << endl;
		if (temp1>temp2)
		for (i = temp + 1; i <= temp1; i++)
			cout << "L" << endl;
		else
		for (i = temp + 1; i <= temp2; i++)
			cout << "U" << endl;
	}
	if (pre1 == 0)
	{
		cout << ans << endl;
		if (pre2>0)
		for (i = 1; i <= temp2; i++)
			cout << "U" << endl;
		else
		for (i = 1; i <= temp2; i++)
			cout << "D" << endl;
	}
	else if (pre2 == 0&&pre1!=0)
	{
		cout << ans << endl;
		if (pre1>0)
		for (i = 1; i <= temp1; i++)
			cout << "R" << endl;
		else
		for (i = 1; i <= temp1; i++)
			cout << "L" << endl;
	}
	return 0;
}

但是这个方法太麻烦了,写了很多行,并不是题目的最优解。看了一下源代码,发现可以模拟这个人走的坐标,即每次移动改变字符串。 代码简单易懂,值得思考。

#include<cstring>
#include<string>
#include<iostream>
#include<cstdio>
using namespace std;
int max(int a, int b)
{
	return a > b ? a : b;
}
int main()
{
	string a, b;
	int i, j, m, n, ans, pre1, pre2;
	cin >> a >> b;
	pre1 = max(a[0] - b[0], b[0] - a[0]);
	pre2 = max(a[1] - b[1], b[1] - a[1]);
	ans = max(pre1, pre2);
	cout << ans << endl;
	while (a != b)
	{
		if (a[0] < b[0])
		{
			cout << "R";
			a[0]++;
		}
		if (a[0]>b[0])
		{
			cout << "L";
			a[0]--;
		}
		if (a[1] < b[1])
		{
			cout << "U";
			a[1]++;
		}
		if (a[1]>b[1])
		{
			cout << "D";
			a[1]--;
		}
		cout << endl;
	}
	return 0;
}




【学习系列】SAP RAP 20:Unmanaged场景开发示例-调用BAPI、BAPI包装类、BO接口创建标准采购订单 本文介绍了在SAP RAP(Restful ABAP Programming)框架下,通过三种方式实现采购订单创建的方案。首先分析了RAP框架对传统BAPI调用的限制,特别是禁止显式COMMIT语句的要求。随后详细阐述了三种实现方式:1)直接调用传统BAPI(仅支持OP环境);2)使用BAPI包装类(仅支持OP环境);3)调用标准业务对象接口(支持所有环境)。 文章重点说明了在unmanaged场景下的技术实现细节,包括字段控制、缓冲区管理、测试运行与实际调用的分离处理等。针对每种方式都提供了完整的实施步骤 阅读详情

相关推荐

初学者入门机器学习 (ML)的推荐教程

了解基本的机器学习概念并不难。有大量免费的在线博客文章、视频和编码教程可以引导您了解基础知识——从介绍性内容到常见应用程序,再到算法和应用技能。这篇文章总结了一些用于介绍 ML 的最佳免费选项。它按逻辑顺序排列,每个概念都建立在上一个概念的基础上。

u010542847的博客 4615

Shortest path of the king(贪心)

题目链接 : http://codeforces.com/problemset/problem/3/A题意:king有八个方向可以走,分别为L, R , U, D, LD, LU, RD ,RU 给出king的起点和终点, 问最少需要多少步数,并输出路径,路径可能不唯一。题解: 记起点和终点在横轴上的距离为tx,纵轴上的距离为ty;由tx,ty的正负值得到起点和终点的相对位置。若在横轴与

CatDsy的博客 437

电磁波成像技术全解析:从γ射线到无线电波的应用实践

本文全面解析电磁波成像技术,从γ射线到无线电波的应用实践。详细介绍了不同波长电磁波的特性与实用场景,包括医疗影像、工业检测、安防监控等领域。特别探讨了X射线、红外热成像、毫米波雷达等前沿技术的实战应用,为读者提供技术选型指南和操作技巧。

weixin_29305337的博客 292

1.23 Shortest path of the king

国王的最短路径 描述: The king is left alone on the chessboard. In spite of this loneliness, he doesn't lose heart, because he has business of national importance. For example, he has to pay an official visit to squaret. As the king is not in habit of wasting his.

Zhouwen_的博客 449

Cadence创建异形焊盘教程(详细操作)

背景:一些常见的矩形/圆形焊盘,设置参数就可。碰到非规则的一些焊盘,那只能用Cadence Allegro PCB来画咯。 一、新建图形 【File】——>【New…】 弹出如下窗口,选择绘制的类型Shape symbol、路径,输入名字 二、设置参数 【Setup】——>【Design Parameters…】 Enhanced display modes:增强型显示模型。全部勾选上。 在【Design】设置单位为mm 设置偏移量(-50,-50) 三、绘制图形 1、找到原点,即参考

qq_42057393的博客 2万+

ESP32-S3数字麦克风入门:手把手教你用Arduino IDE读取MSM261S4030H0R的I2S数据

本文详细介绍了如何使用Arduino IDE读取MSM261S4030H0R数字麦克风的I2S数据,涵盖硬件连接、代码编写和数据可视化全流程。重点解决I2S引脚配置、环境噪声过滤和声波观察等实际问题,适合ESP32-S3开发者快速上手数字麦克风应用。

weixin_26742753的博客 314

CF解题报告:3

CF3解题报告

PocketSam的博客 110

保姆级教程:用Zemax 18.9复现单模光纤耦合仿真(附康宁SMF-28e参数)

本文提供了一份详细的Zemax 18.9单模光纤耦合仿真教程,以康宁SMF-28e为例,涵盖从软件设置、材料参数输入到系统构建和优化的全流程。通过光束耦合分析和物理光学传播(POP)技术,帮助用户准确模拟光纤耦合效率,并解决常见问题,适用于光学设计初学者和专业人士。

weixin_27330481的博客 313

A. Shortest path of the king

A. Shortest path of the king time limit per test 1 second memory limit per test 64 megabytes input standard input output standard output The king is left alone on the chessboard.

wahaha1_的专栏 665

汽车电子RTOS--AUTOSAR OS

AUTOSAR OS(AUTomotive Open System ARchitecture Operating System)是AUTOSAR(汽车开放系统架构)标准中的一个重要组件,它是一个专门为汽车电子控制单元(ECU)设计的实时操作系统(RTOS)。AUTOSAR OS旨在提供一个标准化的软件平台,以促进不同供应商和汽车制造商之间的组件复用和协同开发,提高软件质量和降低成本。

MHD0815的博客 1796

11、系统稳定性分析与李雅普诺夫函数的应用

本文深入探讨了李雅普诺夫函数在系统稳定性分析中的核心作用,重点介绍了时变与时不变系统的稳定性判据差异,以及巴尔巴拉特引理在处理非自治系统中的应用。文章还阐述了正线性系统与严格正实传递函数的性质及其在非线性控制系统中的重要性,并展示了李雅普诺夫函数在评估主导时间常数和设计快速干扰抑制控制策略方面的实际应用。通过具体示例和流程图,系统地总结了李雅普诺夫方法在稳定性分析、性能评估与控制器设计中的多维度价值,同时指出了当前挑战与未来发展方向。

Jerry的专栏 93

codeforces 3A Shortest path of the king

A. Shortest path of the king time limit per test 1 second memory limit per test 64 megabytes input standard input output standard output The king is left alone on the ch

ahoLic的专栏 1760

【图像识别】基于计算机视觉实现红绿灯识别matlab代码

​ 1 简介 现如今,盲人出行依旧是一大问题.面对无处不在的红绿灯,盲人无法通过眼睛辨识红绿灯的状态,出行受阻.为了解决盲人识别红绿灯的难题,本文提出了一种基于MATLAB进行红绿灯识别的系统.对交通灯的识别主要基于对交通灯的色彩及形状特征.基于信号灯的亮度对其进行分割,提取,并设置了一种自动最优选择阈值的方法.再由HIS彩色空间的H信息识别颜色.最后根据二值化的交通灯的横坐标和纵坐标方向的投影进行形状匹配识别. 2 部分代码 clc clear all close all a=imread('m

qq_59747472的博客 2690

3A. Shortest path of the king

一、Problem The king is left alone on the chessboard. In spite of this loneliness, he doesn’t lose heart, because he has business of national importance. For example, he has to pay an official visit to ...

riemann_的博客 599

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

【codeforces#3A】Shortest path of the king

【codeforces#3A】Shortest path of the king

nininicrystal的专栏 425

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

[codeforces] A. Shortest path of the king

题目:A. Shortest path of the kingtime limit per test1 secondmemory limit per test64 megabytesinputstandard inputoutputstandard outputThe king is left alone on the chessboard. In spite of this loneliness...

jing16337305的博客 310

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

CF 3A Shortest path of the king

这题我傻逼还用BFS。看了别人的代码我彻底无语~ #include #include #include using namespace std; int main() { string a,b; while(cin>>a>>b) { int m = abs(a[0]-b[0]); int n = abs(a[1]-b[1]);

Johnbug 727

CF3A Shortest path of the king

题意翻译 国王独自一人在棋盘上。尽管他很孤独,但他并不会灰心,因为他有国家大事要做。例如,他必须对广场T进行正式访问。因为国王没有浪费时间的习惯,所以他想从目前的位置上尽量走最少的步数。请你帮他做这件事。 在一次移动中,国王可以到达有一个共同的边或一个共同的顶点的正方形上,(他通常可以移动到8个不同的方格上)。 输入格式 第一行包含方形s的棋盘坐标,第二行包含平方t。 棋盘坐...

wcxyky的博客 438

CodeForces - 3A Shortest path of the king

题目链接 BFS 注意起点和终点相同的情况 import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Main{ static String s0,s1; static String[] str = {"LU","U","RU","R","RD","D","LD","L"}; static int[][] next = {{-1,1},{0,1},{1,..

付某人的博客 279

[Codeforces]3A. Shortest path of the king

time limit :per test 1 second memory limit: per test64 megabytes input:standard input output:standard outputThe king is left alone on the chessboard. In spite of this loneliness, he doesn’t lose hea

U__F__O 319

A - Shortest path of the king

A - Shortest path of the king Crawling in process...Crawling failedTime Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u SubmitStatusPracticeCodeForces 3A Description

sdjzujxc的专栏 597

题解:CF3A Shortest path of the king

国王在国际象棋棋盘上可以朝8个方向移动(上、下、左、右、以及四个对角线方向)。需要找到从起点到终点的最短路径,并输出具体移动方向。尽管他很孤独,但他并不会灰心,因为他有国家大事要做。在一次移动中,国王可以到达与他目前所在方格有共同的边或共同的顶点的方格里(通常情况下,他可以移动到。分别指向左、向右、向上和向下移动,两个字母的组合代表沿着对角线移动(如。因为国王没有浪费时间的习惯,所以他想从目前的位置(方格。)上出发,走最少的步数。——国王移动的最小步数。行输出移动的每一步。的棋盘坐标,第二行包含方格。

as_asdf的博客 1399
上一篇: Face Detection
下一篇: Ice Sculptures
Techay
博客等级 码龄11年 11粉丝 174原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值