Shortest path of the king

Shortest path of the king

题面翻译

国王独自一人在国际象棋棋盘上。尽管他很孤独,但他并不会灰心,因为他有国家大事要做。例如,他必须对方格 t t t 进行访问。因为国王没有浪费时间的习惯,所以他想从目前的位置(方格 s s s)上出发,走最少的步数。请你帮他做这件事。

在一次移动中,国王可以到达与他目前所在方格有共同的边或共同的顶点的方格里(通常情况下,他可以移动到 8 8 8 个不同的方格里)。

【输入格式】

第一行包含方格 s s s 的棋盘坐标,第二行包含方格 t t t 的棋盘坐标。

棋盘坐标由两个字符组成,第一个是从 ah 的小写拉丁字母,第二个是从 18 的数字。具体情况如上图所示。

【输出格式】

在第一行输出 n n n——国王移动的最小步数。然后接下来 n n n 行输出移动的每一步。每次移动用 LRUDLULDRURD 中的一个来描述。

LRUD 分别指向左、向右、向上和向下移动,两个字母的组合代表沿着对角线移动(如 RU 代表向右上方移动)。如果答案不唯一,请输出任意一个答案。

题目描述

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.

样例 #1

样例输入 #1

a8
h1

样例输出 #1

7
RD
RD
RD
RD
RD
RD
RD

思路

这道题很像是bfs,但本道题比正常的bfs多了两个维度的变量:步数和方向。因此对于这样的题,我们就得用结构体来存储。

  • 特别注意:结构体中有string[N],建议直接再创一个结构体来存储,最后再放入。还有就是我们最好把方向和题目给定的什么U、L……对应起来。

代码

//这道题我的收获:如果题目有要求路径并且求最短路(权值为1),并且还要求步数,那么就得用结构体
#include<iostream>
#include<algorithm>
#include<queue>

using namespace std;

const int N = 10;

struct E{
    int x,y,s;
    string move[100];
};

int w[N][N],cnt;
char a[N],b[N];
bool st[N][N];
string path[N];
queue<E>q;
int dx[8]={-1,1,0,0,-1,-1,1,1};
int dy[8]={0,0,1,-1,1,-1,1,-1};
string op[8]={"L","R","U","D","LU","LD","RU","RD"};

int main(){
    cin>>a+1>>b+1;
    
    
    int stx=a[1]-'a'+1,sty=a[2]-'0';
    int edx=b[1]-'a'+1,edy=b[2]-'0';
    
    q.push((E){stx,sty,0," "});
    st[stx][sty]=true;
    //特判,可能重合
    if(st[edx][edy]){
        cout<<0<<endl;
        return 0;
    }
    int k=0;
    while(q.size()){
        auto t=q.front();
        q.pop();
        
        for(int i=0;i<8;i++){
            int x=t.x,y=t.y,step=t.s+1;
            
            int a=x+dx[i],b=dy[i]+y;
            // cout<<a<<" "<<b<<endl;
            if(a<1||b<1||a>8||b>8||st[a][b])continue;
            st[a][b]=true;
            
            
            //这样编译错误
            // string r[100];
            // for(int j=1;j<=step-1;j++){
            //     r[j]=t.move[j];
            // }
            // r[step]=op[i];
            
            E u;
            for(int j=1;j<=step-1;j++){
                u.move[j]=t.move[j];
            }
            u.move[step]=op[i];
            
            u.s=step;
            u.x=a;
            u.y=b;
            // cout<<step<<endl;
            if(a==edx&&b==edy){
                cout<<step<<endl;
                for(int i=1;i<=step;i++){
                    cout<<u.move[i]<<endl;
                }
                break;
            }
            q.push(u);
            
        }
        
    }
    return 0;
}
  • 26
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

green qwq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值