ZOJ-1091 Knight Moves(bfs)

A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy.
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.

Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.

Input Specification

The input file will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.

Output Specification

For each test case, print one line saying "To get from  xx  to  yy  takes  n  knight moves.".

Sample Input

e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6

Sample Output

To get from e2 to e4 takes 2 knight moves.
To get from a1 to b2 takes 4 knight moves.
To get from b2 to c3 takes 2 knight moves.
To get from a1 to h8 takes 6 knight moves.
To get from a1 to h7 takes 5 knight moves.
To get from h8 to a1 takes 6 knight moves.
To get from b1 to c3 takes 1 knight moves.
To get from f6 to f6 takes 0 knight moves.

题意:给定象棋(对这个题来说国际中国一样)棋盘上的两个坐标,问你从第一个点走到第二个点最少需要多少步。

刚刚学了一个bfs模板题,恰巧来了一个适合我去用的地方,这个题真心不错。

模板题我找的代码是求路径的,我就想,那就直接用回溯求一个方法数不就得了?

按照脑子里还不熟的代码搞了半天终于整出来了……

注意:多组数据的时候,一定清空队列!否则你会对于第二组输入的结果一脸懵逼……

还要sum是每次都要从0求和的!所以别忘了等于0!

底层实现的队列,看着代码挺整齐的就学来了

还要注意不能把两个点看成小矩形的两个顶点,因为肯定会走到外面去!

给出代码供参考:

#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
int vis[10][10];

char a,b;
int m,n;
int sum=0;

struct que
{
    int x;
    int y;
    int pre;
}q[300];

int next[16][2]={-2,-1,-2,1,-1,-2,-1,2,1,-2,1,2,2,-1,2,1};

void cnt(int i)
{
    if(q[i].pre!=-1)
    {
        cnt(q[i].pre);
        sum++;
    }
}
void bfs(int x,int y)
{
    if(a==b&&m==n)
    {
        sum=-1;
        return ;
    }
    int front = 0,rear = 1;
    q[front].x=x;
    q[front].y=y;
    q[front].pre=-1;
    while(front < rear)
    {
        for(int i=0;i<8;i++)
        {
            int nx=next[i][0]+q[front].x;
            int ny=next[i][1]+q[front].y;
            if(nx<1||ny<1||nx>8||ny>8||vis[nx][ny])
                continue;
            else
            {
                vis[nx][ny]=1;
                q[rear].x=nx;
                q[rear].y=ny;
                q[rear].pre=front;
                rear++;
            }
            if(nx==b-'a'+1&&ny==n)cnt(front);
        }
        front++;
    }
}
int main()
{

    while(~scanf("%c%d %c%d",&a,&m,&b,&n))
    {
        getchar();
        memset(vis,0,sizeof(vis));
        bfs(a-'a'+1,m);
        printf("To get from %c%d to %c%d takes %d knight moves.\n",a,m,b,n,sum+1);
        sum=0;
    }
    return 0;
}



全部评论

相关推荐

魔法恐龙:这真得给个机会,面试的时候问问不吃饭78.5h怎么做到的
点赞 评论 收藏
分享
10-19 10:28
已编辑
成都理工大学 后端工程师
团孝子已上线feeling:面了很多家公司,能感受到目前只有小公司+外包喜欢问八股。大厂虽然也问八股,但是是从实习、项目中进行提问,并且大厂会问很深,面试官也会对你的回答进行思考➕追问,所以准备大厂面试前一定要备好相关资料。对于算法,我做的是codetop前100+力扣hot100+力扣高频150,面试中实感hot100就足够,基本上只要是hot100就秒答。对于项目和八股,我做的也是烂大街的星球项目,八股则是看小林和问ai,自己也写了很多技术博客和画了很多思维导图,并且自己也尝试用嘴巴说出来,不只停留于纸面。运气也很重要,必须要让面试官/HR看到简历才行,所以建议投递时间是下午两点。tl:第一岗位9.9&nbsp;投递9.10&nbsp;一面(一面评价:最近见过最强的大三,结束五分钟后约二面,都晚上九点了不下班吗)9.11&nbsp;二面(三道算法a出两道,反问评价:经验不够等横向,我实习生要啥经验)9.21挂(实习时间过短+其他原因,想要一年实习的,为什么不招个正职)第二岗位10.10投递10.11约面(主管打电话,说看到我之前投递记录了想要我挂qa职进去干后端,同意)10.14&nbsp;一面(无八股,主动说确实很强,意愿很强)10.16&nbsp;oc其余,友邦,东软,东华,惠择,用友oc已拒京东测开一面挂(投后端被测开捞)腾讯测试已拒(投后端被测开捞)ps:表扬惠择的主管面,没怎么问技术(可能是一面面试官沟通过了),全程一起讲大道理,解答了心中很多疑惑,也告诉我以面试官角度来看怎么选候选人,如果可以下次一定选惠择
越今朝0:慧择一共几面啊,我二面后就没声音了。。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务