codeforces- 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).
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.
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
题目大意:在棋盘上要从a(a8)到(h1)。输出最小的步数和路线(其中L:向左,R:向右,U:向上,D:向下)
解题思路:这题其实很简单.....横向长度和纵向长度都是确定的,主要刚开始看这道题的时候,看到国王可以斜着走于是想复杂,其实不管
国王是左上方斜走,右上斜走,左下斜走,右下斜走,有两点是唯一不变的,
(1)斜着走必定会在x方向改变1,y方向改变1。
(2)四个斜着走的方向在每次测试用例中只会出现一种。
(就是比如终点在右下方,国王在斜着走的步骤就只会选择右下了,如果不是唯一,则肯定不是最短路径!)
然后下面的代码就很容易理解了.
AC代码1:比较简单易懂
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
inline int read() {int x=,f=;char c=getchar();while(c!='-'&&(c<''||c>''))c=getchar();if(c=='-')f=-,c=getchar();while(c>=''&&c<='')x=x*+c-'',c=getchar();return f*x;}
typedef long long ll;
const int maxn = 1e5+;
char a[],b[];
int main()
{
scanf("%s%s",a,b);
char c,d;
int x=a[]-b[],y=a[]-b[];
if(x>){
c='L';
}
else{
x=-x;
c='R';
}
if(y>){
d='D';
}
else{
y=-y;
d='U';
}
printf("%d",x>y?x:y);
while(x||y){
printf("\n");
if(x){
x--;
printf("%c",c);
}
if(y){
y--;
printf("%c",d);
}
}
return ;
}
AC代码2:
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
inline int read() {int x=,f=;char c=getchar();while(c!='-'&&(c<''||c>''))c=getchar();if(c=='-')f=-,c=getchar();while(c>=''&&c<='')x=x*+c-'',c=getchar();return f*x;}
typedef long long ll;
const int maxn = 1e5+;
char a[],b[];
int main()
{
char c,d;
scanf("%s%s",a,b);
int x=a[]-b[],y=a[]-b[];
c=((x<)?x=-x,'R':'L');
d=((y<)?y=-y,'U':'D');
printf("%d",x>y?x:y);
while(x||y){
printf("\n");
if(x){
x--;
printf("%c",c);
}
if(y){
y--;
printf("%c",d);
}
}
return ;
}
codeforces- 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 ...
- 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 ...
- node搜索codeforces 3A - Shortest path of the king
发一下牢骚和主题无关: 搜索,最短路都可以 每日一道理 人生是洁白的画纸,我们每个人就是手握各色笔的画师:人生也是一条看不到尽头的长路,我们每个人则是人生道路的远足者:人生还像是一块神奇的土地 ...
- Codeforces Beta Round #3 A. Shortest path of the king
标题效果: 鉴于国际棋盘两点,寻求同意的操作,是什么操作的最小数量,在操作过程中输出. 解题思路: 水题一个,见代码. 以下是代码: #include <set> #include < ...
- A - Shortest path of the king (棋盘)
The king is left alone on the chessboard. In spite of this loneliness, he doesn't lose heart, becaus ...
- 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 ...
- 3A. Shortest path of the king
给你一个的棋盘, 问:从一个坐标到达另一个坐标需要多少步? 每次移动可以是八个方向. #include <iostream> #include <cmath> #inclu ...
- CF3A 【Shortest path of the king】
一句话题意:在8 * 8的棋盘上,输出用最少步数从起点走到终点的方案 数据很小,可以广搜无脑解决 定义数据结构体 struct pos{ int x,y,s; //x.y表示横纵坐标,s表示步数 ]; ...
随机推荐
- java &&和&与逻辑运算区别
二者都表示与运算,同真为真,遇假即假 && 具有短路功能,前面为false后面不在预算直接表达式为false; &还可以用作位运算符,当&操作符两边的表达式不是 boo ...
- linux nmon安装
系统版本红帽7.7: [root@hostuser1 nmon_permon]# cat /etc/redhat-release CentOS Linux release 7.7.1908 (Core ...
- python接口自动化测试 - requests库的post请求进行文件上传
前言 如果需要发送文件到服务器,比如上传图片.视频等,就需要发送二进制数据. 一般上传文件使用的都是 Content-Type: multipart/form-data; 数据类型,可以发送文件,也可 ...
- appium---如何判断原生页面和H5页面
目前app中存在越来越多的H5页面了,对于一些做app自动化的测试来说,要求也越来越高,自动化不仅仅要支持原生页面,也要可以H5中进行操作自动化,这一篇介绍如何查看页面上是否存在H5页面,这里首先要了 ...
- win7安装composer(PHPStudy环境)
好句没写博客园了,因为现在的公司就是写代码啥的,没有什么新的东西,但是光写代码也学到不少东西,因为本身太菜了,最近让做一个pdf表单的功能,首先得安装php-pdftk,看GitHub里面用compo ...
- 概率DP (大概是最入门的题了) lightoj 1248
有一个骰子,n个面,问所有面都被摇出的期望. 转自**的博客, 因为概率是(n-k)/n 所以期望次数是1/(前面这个数) #include<cstdio> #include<a ...
- jquery 复制
Jq将字符串复制粘贴到剪贴板 第一种: 自己测试时 只适合于input 和textarea 但是针对于其他标签的复制就不能用了.代码如下: <!DOCTYPE html> < ...
- [2020BUAA软工助教]第1次个人作业
热身作业(阅读) 一.前言 我认为人生就是一次次地从<存在>到<光明>. 二.软件工程师的成长 博客索引 同学们在上这门课的时候基本都是大三,觉得在大学里,到教室来听课有意思么 ...
- scrapy下载 大文件处理
# 一个校花网图片下载的案例,也适合大文件处理,多个文件视频,音频处理 工程流程 -- scrapy startproject xx cd xx scrapy genspider hh www.xx. ...
- 给footer标签设置padding:7px auto;失效
margin:auto可以做到水平居中,前提条件就是,这个标签是块状元素,并且有个确定的宽度,百分比的宽度也行: padding的话, 设置成auto它会自动继承浏览器的padding值, 当设置pa ...