CodeForces 3A Shortest path of the king(贪心)——Codeforces Beta Round #3

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

CodeForces 3A Shortest path of the king(贪心)——Codeforces Beta Round #3_第1张图片

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: LRUDLULDRU or RD.

LRUD 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

/*********************************************************************/

题意:有一个棋盘,它的横坐标用a~h表示,纵坐标用1~8表示,现给你一个起点坐标,和一个终点坐标,求步数以及路径,每个格子可以往相邻的8个格子移动

解题思路:其实这题还是比较好做的,暂时假设起点坐标为(x1,y1),终点坐标为(x2,y2),那么步数就是max(|x2-x1|,|y2-y1|)

而对于路径,也好办,只要每步判断一下是否已经达到终点就可以了,代码还是比较好理解的

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
using namespace std;
const int N = 1001;
const int inf = 1000000000;
const int mod = 2009;
int main()
{
    int b,d;
    char a[5],c[5];
    scanf("%s",a);
    scanf("%s",c);
    b=a[1]-'0';
    d=c[1]-'0';
    printf("%d\n",max(abs(b-d),abs(a[0]-c[0])));
    while(b!=d||a[0]!=c[0])
    {
        if(c[0]>a[0])
        {
            printf("R");
            a[0]++;
        }
        if(c[0]<a[0])
        {
            printf("L");
            a[0]--;
        }
        if(b>d)
        {
            printf("D");
            b--;
        }
        if(b<d)
        {
            printf("U");
            b++;
        }
        puts("");
    }
    return 0;
}
菜鸟成长记


你可能感兴趣的:(ACM,greedy,Shortest,Paths)