A - 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 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 1 to 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.
Examples
a8
h1
7
RD
RD
RD
RD
RD
RD
RD cf此题地址http://codeforces.com/problemset/problem/3/A 这里学到了string这个东西。比如,string a; a[0]="adasdasd",第一次知道还有这种操作,一位存一个字符串,那么就简单多了。
那么使用while模拟就好了,一直接近终点。肯定尽量先斜着走。while根据两者的位置分成好几种情况。根据ASCLL码,直接++,--就好了,不用再变成数字了。string s。s[0],s[1]直接++--就好了,直到等于e[0],e[1]。
使用一个tot来计数,此为总步数。
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
string map[];
string s,e;
int main()
{
int tot=;
cin>>s;
cin>>e;
while(s[]<e[]&&s[]<e[])
{
s[]++;s[]++;
map[tot++]="RU";
}
while(s[]<e[]&&s[]>e[])
{
s[]++;s[]--;
map[tot++]="RD";
}
while(s[]>e[]&&s[]<e[])
{
s[]--;s[]++;
map[tot++]="LU";
}
while(s[]>e[]&&s[]>e[])
{
s[]--;s[]--;
map[tot++]="LD";
}
while(s[]==e[]&&s[]>e[])
{
s[]--;
map[tot++]="D";
}
while(s[]==e[]&&s[]<e[])
{
s[]++;
map[tot++]="U";
}
while(s[]==e[]&&s[]<e[])
{
s[]++;
map[tot++]="R";
}
while(s[]==e[]&&s[]>e[])
{
s[]--;
map[tot++]="L";
}
cout<<tot<<endl;
for(int i=;i<tot;i++)
cout<<map[i]<<endl;
}
A - Shortest path of the king (棋盘)的更多相关文章
- Codeforces Beta Round #3 A. Shortest path of the king 水题
A. Shortest path of the king 题目连接: http://www.codeforces.com/contest/3/problem/A Description The kin ...
- Codeforces-A. 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 ...
- 3A. Shortest path of the king
给你一个的棋盘, 问:从一个坐标到达另一个坐标需要多少步? 每次移动可以是八个方向. #include <iostream> #include <cmath> #inclu ...
- Codeforces Beta Round #3 A. Shortest path of the king
标题效果: 鉴于国际棋盘两点,寻求同意的操作,是什么操作的最小数量,在操作过程中输出. 解题思路: 水题一个,见代码. 以下是代码: #include <set> #include < ...
- Shortest path of the king
必须要抄袭一下这个代码 The king is left alone on the chessboard. In spite of this loneliness, he doesn't lose h ...
- CF3A Shortest path of the king
The king is left alone on the chessboard. In spite of this loneliness, he doesn't lose heart, becaus ...
- node搜索codeforces 3A - Shortest path of the king
发一下牢骚和主题无关: 搜索,最短路都可以 每日一道理 人生是洁白的画纸,我们每个人就是手握各色笔的画师:人生也是一条看不到尽头的长路,我们每个人则是人生道路的远足者:人生还像是一块神奇的土地 ...
- CF3A 【Shortest path of the king】
一句话题意:在8 * 8的棋盘上,输出用最少步数从起点走到终点的方案 数据很小,可以广搜无脑解决 定义数据结构体 struct pos{ int x,y,s; //x.y表示横纵坐标,s表示步数 ]; ...
- 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 ...
随机推荐
- JAVA学习笔记-数组的三种初始化方式
package Study; public class TestArray02 { public static void main(String[] args){//声明 int[] a; int ...
- java 如何爬取百度百科词条内容(java如何使用webmagic爬取百度词条)
这是老师所布置的作业 说一下我这里的爬去并非能把百度词条上的内容一字不漏的取下来(而是它分享链接的一个主要内容概括...)(他的主要内容我爬不到 也不想去研究大家有好办法可以call me) 例如 互 ...
- WC2020 联训 #19 矩阵
好不容易自己切一道题 链接 Description 在一个 \(n×(n+1)\) 的棋盘上放棋子, \(n\) 行中每行都恰好有两枚棋子,并且 \(n+1\) 列中每列都至多有两枚棋子,设 \(n= ...
- Django(五)1 - 4章实战:从数据库读取图书列表并渲染出来、通过url传参urls.py path,re_path通过url传参设置、模板语法
一.从数据库读取图书数据并渲染出来 1)app1/views.py函数books编写 [1]从模型下导入bookinfo信息 [2]从数据库获取图书对象列表 [3]把获取到的图书对象赋值给books键 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-search
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- J - Worker
Avin meets a rich customer today. He will earn 1 million dollars if he can solve a hard problem. The ...
- MongoDB 3.2 升级至 3.4.6
MongoDB 升级测试步骤:1.MongoDB版本升级顺序3.2->3.4->3.6->4.0 不能跨版本升级2.升级到3.4后,您不能降级到3.2.7或更早版本.您只能降级到3. ...
- 五十九、SAP中常用预定义系统变量
一.SAP中常用预定义系统变量 内容如下: 二.系统变量定义在结构SYST里,我们打开SE38 三.在代码编辑器输入SYST变量 四.双击SYST,来到这个系统结构,里面有很多系统变量 五.我们随便写 ...
- 145-PHP 使用<<<和HTML混编(一)
<?php $html=<<<TEMP1 <title>PHP输出HTML代码</title> <body> <a href=#> ...
- hdu 1671 Phone List 统计前缀次数
Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...