CF3A Shortest path of the king

时间:2021-12-21 23:38:43

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

CF3A Shortest path of the king

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.

题目大意:从起点走到终点,求最短距离,而且写出最短距离的路径是什么样的:

样例输入:

a8 h1

样例输出:

7 RD RD RD RD RD RD RD

分析:贪心模拟,由题意知道最多有八个可以移动的方向:L,R,U,D,LU,LD,RU,RD,那么当前位置与终点不同就要优先走斜线,模拟一下即可知道最短路径的长度为起点与终点横坐标差的绝对值与纵坐标差的绝对值中较大的那一个。由于走斜线的格式均是L或R+U或D,输出移动动作时可以将走斜线分解为横坐标走一步加纵坐标走一步。

 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
string s1,s2;//记录始末位置坐标 int main(){
char cx,cy;
cin>>s1>>s2;
int x=s1[]-s2[];
int y=s1[]-s2[];
cx=x<?'R':'L';
cy=y<?'U':'D'; if(x<){
x=-x;
}
if(y<){
y=-y;
}
printf("%d\n",x>y?x:y); for( ;x||y; putchar('\n')){
if(x){
x--;
putchar(cx);
}
if(y){
y--;
putchar(cy);
}
} return ;
}

CF3A Shortest path of the king的更多相关文章

  1. Codeforces-A&period; Shortest path of the king&lpar;简单bfs记录路径&rpar;

    A. Shortest path of the king time limit per test 1 second memory limit per test 64 megabytes input s ...

  2. Codeforces Beta Round &num;3 A&period; Shortest path of the king 水题

    A. Shortest path of the king 题目连接: http://www.codeforces.com/contest/3/problem/A Description The kin ...

  3. CF3A 【Shortest path of the king】

    一句话题意:在8 * 8的棋盘上,输出用最少步数从起点走到终点的方案 数据很小,可以广搜无脑解决 定义数据结构体 struct pos{ int x,y,s; //x.y表示横纵坐标,s表示步数 ]; ...

  4. Shortest path of the king

    必须要抄袭一下这个代码 The king is left alone on the chessboard. In spite of this loneliness, he doesn't lose h ...

  5. A - Shortest path of the king &lpar;棋盘&rpar;

    The king is left alone on the chessboard. In spite of this loneliness, he doesn't lose heart, becaus ...

  6. node搜索codeforces 3A - Shortest path of the king

    发一下牢骚和主题无关: 搜索,最短路都可以     每日一道理 人生是洁白的画纸,我们每个人就是手握各色笔的画师:人生也是一条看不到尽头的长路,我们每个人则是人生道路的远足者:人生还像是一块神奇的土地 ...

  7. 3A&period; Shortest path of the king

    给你一个的棋盘, 问:从一个坐标到达另一个坐标需要多少步? 每次移动可以是八个方向.   #include <iostream> #include <cmath> #inclu ...

  8. Codeforces Beta Round &num;3 A&period; Shortest path of the king

    标题效果: 鉴于国际棋盘两点,寻求同意的操作,是什么操作的最小数量,在操作过程中输出. 解题思路: 水题一个,见代码. 以下是代码: #include <set> #include &lt ...

  9. Codeforces 3A-Shortest path of the king(BFS打印路径)

    A. Shortest path of the king time limit per test 1 second memory limit per test 64 megabytes input s ...

随机推荐

  1. 全面解析sizeof(下) 分类: C&sol;C&plus;&plus; StudyNotes 2015-06-15 10&colon;45 263人阅读 评论&lpar;0&rpar; 收藏

    以下代码使用平台是Windows7 64bits+VS2012. sizeof作用于基本数据类型,在特定的平台和特定的编译中,结果是确定的,如果使用sizeof计算构造类型:结构体.联合体和类的大小时 ...

  2. 实例之JavaScript

    使用JavaScript实现5秒倒计时 <html> <head> <meta charset="utf-8"> <title>&l ...

  3. css 之优先策略

    <html> <head> <title>testCSS</title> <style type="text/css"> ...

  4. poj1562--Oil Deposits

    Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...

  5. unique&lowbar;ptr简谈

    看到文章里的同学留言说到unique_ptr,这两天看了一下cplusplus提供的reference才知道这个东西是c++11的新特性,对c++11的新特性不是很了解,花时间了解了下unique_p ...

  6. 201521123024 《Java程序设计》第1周学习总结

    一.本周章学习总结 1.Java的版本迁移 2.运用eclipse和notepad++编写Java 3.对JDK,JRE,JVM有初步的了解 二.书面作业 1.为什么java程序可以跨平台运行?执行j ...

  7. How to bypass Win10 logon password&quest;

    Usually we will use LiveView or VFC to "boot up" the evidence files acquired from suspect' ...

  8. 使用async和wait进行异步编程

    本文来源于博客园-钱智慧,转载请注明出处 代码示例 // 要让一个方法成为异步方法: // - async修饰符. // - 返回类型是 Task 或者 Task<T>. 具体来说,如果函 ...

  9. 在Debian9服务器上安装最新版Python

    通过Xshell 6 远程连接linux服务器 安装前的准备工作 更新服务器: 命令:apt update && apt upgrade -y 安装python3的依赖库: 命令:ap ...

  10. http请求415错误Unsupported Media Type

    王子乔 每一个认真生活的人,都值得被认真对待 http请求415错误Unsupported Media Type 之前用了封装的ajax,因为请求出了点问题,我试了下jQuery的$.ajax,报出了 ...