CF3A. 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 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
这个世界大牛真太多了......
 
   
题目大意:从起点走到终点,求最短距离,而且写出最短距离的路径是什么样的:
只不过要把y轴翻转一下,就可以了,第一思路是bfs,但是老是不对,看了一下大神的bfs代码:
#include <iostream>
#include<cstdio>
#include<string.h>
#include<queue>
using namespace std;
int move[8][2]= {{-1,0},{1,0},{0,1},{0,-1},{1,1},{-1,1},{1,-1},{-1,-1}};
//             左 L    右  R 下  D  上 U    右下 RD  左下  LD  右上RU    左上LU
char mo[8][3]= {'L','R','D','U','RD','LD','RU','LU'};
int ex,ey,sx,sy;
struct node
{
    int x,y;
    int vis;
    int depth;
    int shx,shy;
    int m;
} pan[10][10];
int bfs()
{
    int i,j,k;
    queue<node> q;//队列先进先出
    q.push(pan[sx][sy]);//在队尾插入一个元素
    if(sx==ex&&sy==ey)
        return 0;
    pan[sx][sy].vis=1;//标记起点为1
    while(!q.empty())
    {
        node p;
        int tx,ty,yx,yy;
        p=q.front();//起点
        q.pop();//删除起点
        for(i=0; i<8; i++)
        {
            node t;
            t=p;
            tx=p.x+move[i][0];
            ty=p.y+move[i][1];//遍历
            //cout<<tx<<"df"<<ty<<endl;
            if(tx<0||tx>=8||ty<0||ty>=8)
                continue;
            if(pan[tx][ty].vis==0)
            {
                pan[tx][ty].shx=p.x;
                pan[tx][ty].shy=p.y;
                pan[tx][ty].depth=p.depth+1;
                q.push(pan[tx][ty]);//在队尾插入一个元素
                pan[tx][ty].vis=1;
                pan[tx][ty].m=i;//标记走的路线,是L,R.....
                //cout<<pan[tx][ty].shx<<" "<<pan[tx][ty].shy<<" "<<pan[tx][ty].depth<<" "<<pan[tx][ty].m<<endl;
                if(tx==ex&&ty==ey)
                    return pan[tx][ty].depth;
            }
        }
    }
    return 0;
}
int main()
{
    int i,j,k,l,m,n;
    memset(pan,0,sizeof(pan));
    for(int i=0; i<8; i++)
    {
        for(int j=0; j<8; j++)
        {
            pan[i][j].x=i;
            pan[i][j].y=j;
        }
    }
    char z;
    int v;
    scanf("%c%d",&z,&v);//先y(字母)后x
    sx=z-'a';
    sy=8-v;//起点
    //cout<<sx<<" "<<sy<<endl;
    getchar();
    scanf("%c%d",&z,&v);
    ex=z-'a';
    ey=8-v;//终点
    //cout<<ey<<" "<<ex<<endl;
    m=bfs();
    printf("%d\n",m);
    if(m==0)
        return 0;
    int path[100];
    memset(path,0,sizeof(path));
    j=0;
    path[j++]=pan[ex][ey].m;
    int qx,qy;
    qx=pan[ex][ey].shx;
    qy=pan[ex][ey].shy;
    while(qx!=sx||qy!=sy)
    {
        int tx,ty;
        tx=qx;
        ty=qy;
        qx=pan[tx][ty].shx;
        qy=pan[tx][ty].shy;
        path[j++]=pan[tx][ty].m;
        //printf("%d\n",p->m);
    }
    for(i=j-1; i>=0; i--)
    {
        int k=path[i];
        if(k==0)
            printf("L\n");
        else if(k==1)
            printf("R\n");
        else if(k==2)
            printf("D\n");
        else if(k==3)
            printf("U\n");
        else if(k==4)
            printf("RD\n");
        else if(k==5)
            printf("LD\n");
        else if(k==6)
            printf("RU\n");
        else if(k==7)
            printf("LU\n");
    }
    return 0;
}

后来在一个牛人的博客里看到了特吐血的代码:
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
using namespace std;

int main()
{
    string a,b;
    while(cin>>a>>b)
    {
        int m = abs(a[0]-b[0]);
        int n = abs(a[1]-b[1]);
        cout<<max(m,n)<<endl;
        while(a!=b)
        {
            if(a[0] > b[0]) {cout<<"L";a[0] --;}
            if(a[0] < b[0]) {cout<<"R";a[0] ++;}
            if(a[1] > b[1]) {cout<<"D";a[1] --;}
            if(a[1] < b[1]) {cout<<"U";a[1] ++;}
            cout<<endl;
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值