codeforces- Shortest path of the king
Posted lipu123
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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=0,f=1;char c=getchar();while(c!=‘-‘&&(c<‘0‘||c>‘9‘))c=getchar();if(c==‘-‘)f=-1,c=getchar();while(c>=‘0‘&&c<=‘9‘)x=x*10+c-‘0‘,c=getchar();return f*x;} typedef long long ll; const int maxn = 1e5+10; char a[3],b[3]; int main() { scanf("%s%s",a,b); char c,d; int x=a[0]-b[0],y=a[1]-b[1]; if(x>0){ c=‘L‘; } else{ x=-x; c=‘R‘; } if(y>0){ d=‘D‘; } else{ y=-y; d=‘U‘; } printf("%d",x>y?x:y); while(x||y){ printf(" "); if(x){ x--; printf("%c",c); } if(y){ y--; printf("%c",d); } } return 0; }
AC代码2:
#pragma GCC optimize(2) #include<bits/stdc++.h> using namespace std; inline int read() {int x=0,f=1;char c=getchar();while(c!=‘-‘&&(c<‘0‘||c>‘9‘))c=getchar();if(c==‘-‘)f=-1,c=getchar();while(c>=‘0‘&&c<=‘9‘)x=x*10+c-‘0‘,c=getchar();return f*x;} typedef long long ll; const int maxn = 1e5+10; char a[3],b[3]; int main() { char c,d; scanf("%s%s",a,b); int x=a[0]-b[0],y=a[1]-b[1]; c=((x<0)?x=-x,‘R‘:‘L‘); d=((y<0)?y=-y,‘U‘:‘D‘); printf("%d",x>y?x:y); while(x||y){ printf(" "); if(x){ x--; printf("%c",c); } if(y){ y--; printf("%c",d); } } return 0; }
以上是关于codeforces- Shortest path of the king的主要内容,如果未能解决你的问题,请参考以下文章
Berland and the Shortest Paths CodeForces - 1005F(最短路树)
Codeforces 1005F Berland and the Shortest Paths 最短路树性质
Codeforces Round #731 (Div. 3) A. Shortest Path with Obstaclea
The Shortest Statement CodeForces - 1051F(待测试)