HDOJ/HDU 1372 Knight Moves(经典BFS)

简介: HDOJ/HDU 1372 Knight Moves(经典BFS)

Problem Description

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

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

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.


题意:国际象棋的骑士~可以理解成象棋中的马,走日字。

行号从:1-8

列号从:a-h

问:从起点到终点的最短路径是几步。


遇到最短路径的题。最好用广搜,虽然深搜也可以AC。


结构体变量名不要取next,否则会出现CE!!!

这个next搞了我个把小时,后来改成nextb,就AC了。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
int xa,ya,xb,yb;
char a[3],b[3];
struct node{
    int x;
    int y;
    int t;
}first,nextb;
int map[10][10];
int dir[8][2]={-2,1,-2,-1,-1,2,-1,-2,1,2,1,-2,2,-1,2,1};
void bfs(){
    int i;
    queue<node> q;
    first.x=xa;
    first.y=ya;
    first.t=0;
    q.push(first);
    map[first.x][first.y]=1;
    while(!q.empty()){
        first = q.front();
        //printf("---%d---%d\n",first.x,first.y);
        q.pop();
        if(first.x==xb&&first.y==yb){
            printf("To get from %s to %s takes %d knight moves.\n",a,b,first.t);
            return;
        }
        for(i=0;i<8;i++){
            nextb.x=first.x+dir[i][0];
            nextb.y=first.y+dir[i][1];
            if(nextb.x<0||nextb.x>=8||nextb.y<0||nextb.y>=8){
                //printf("1\n");
                continue;
            }
            if(map[nextb.x][nextb.y]==1){
                //printf("2\n");
                continue;
            }
            map[nextb.x][nextb.y]=1;
            nextb.t=first.t+1;
            q.push(nextb);
        }
    }
}
int main()
{
    while(~scanf("%s%s",&a,&b)){
        xa=a[0]-'a';
        ya=a[1]-'1';
        xb=b[0]-'a';
        yb=b[1]-'1';
        memset(map,0,sizeof(map));
        for(int i=0;i<10;i++){
            for(int j=0;j<10;j++){
                map[i][j]=0;
            }
        }
        //printf("a=%s\n",a);
        //printf("b=%s\n",b);
        bfs();
    }
    return 0;
}
目录
相关文章
|
前端开发 测试技术 持续交付
云效平台介绍
云效,创立于2012年,是由阿里巴巴出品,是业内领先的面向企业的一站式研发效能平台,以提升研发效能为目标,通过线上化,透明化和自动化打通产品质量闭环,真正实现了持续集成持续交付。
10260 14
|
人工智能 供应链 监控
数字孪生与农业:精准农业的发展趋势
数字孪生技术正逐步渗透到农业生产的各个环节,通过创建物理实体的数字副本,实现对实体状态的精确模拟和预测。在农业领域,这一技术的应用正引领着精准农业的发展趋势,包括智慧栽培、环境智能控制、精准农业管理和农业供应链优化等方面,为农业生产的智能化、高效化和可持续发展提供了强大的技术支持。
|
10月前
|
存储 Ubuntu 前端开发
Linux软件包管理工具概览
在Linux系统中,dpkg、apt、rpm、yum和dnf是几种常见的包管理工具,它们分别属于不同的Linux发行版或家族,并有着各自的诞生顺序和特点。下面将按照这些工具的诞生顺序,并结合Debian、Red Hat、CentOS、Ubuntu和Kali等系统,进行详细的介绍。
278 4
|
11月前
|
人工智能 自然语言处理 语音技术
《AI赋能鸿蒙Next:为特殊人群打造无障碍交互新体验》
在科技飞速发展的今天,鸿蒙Next设备借助人工智能技术,显著提升了特殊人群的无障碍交互体验。针对视障人群,提供精准屏幕朗读、视觉辅助智能问答和导航避障辅助;面向听障人群,实现AI声音修复、实时字幕与语音转文字;助力语言障碍者和老年人群体,通过AI优化交流与操作体验。开发者可利用鸿蒙Next的AI能力,深入了解用户需求,进行测试与优化,共同创造友好、便捷的无障碍环境,让特殊人群更好地融入数字社会,享受科技带来的美好生活。
590 8
|
9月前
|
城市大脑 安全 计算机视觉
课时13:城市数据大脑介绍
阿里云与杭州市合作打造的城市数据大脑,通过智能调控红绿灯、实时视频分析交通事件,提升了道路通行效率。如今,城市大脑不仅能主动发现并处理交通事故,还能为救护车规划最优路线,从被动接警转变为积极应对,使城市交通更加顺畅和安全。交警们希望通过这一系统,让杭州变得更加美好,实现更愉快的出行体验。
465 0
|
11月前
|
安全 Linux 测试技术
Intel Linux 内核测试套件-LKVS介绍 | 龙蜥大讲堂104期
《Intel Linux内核测试套件-LKVS介绍》(龙蜥大讲堂104期)主要介绍了LKVS的定义、使用方法、测试范围、典型案例及其优势。LKVS是轻量级、低耦合且高代码覆盖率的测试工具,涵盖20多个硬件和内核属性,已开源并集成到多个社区CICD系统中。课程详细讲解了如何使用LKVS进行CPU、电源管理和安全特性(如TDX、CET)的测试,并展示了其在实际应用中的价值。
323 4
|
机器学习/深度学习 API 数据库
淘宝拍立淘按图搜索商品API接口详解
拍立淘按图搜索商品API接口提供了一种通过上传商品图片来搜索相似或相同商品的功能。用户只需上传一张商品图片,系统通过图像识别技术对该图片进行分析和处理,提取出商品的特征信息,并在商品数据库中进行匹配搜索,最终返回与上传图片相似或相同的商品列表。这一功能广泛应用于电商平台、购物应用以及图像搜索等领域,极大地提升了用户的购物体验。
|
Linux Python
在Linux中,如何查找系统中占用CPU最高的进程?
在Linux中,如何查找系统中占用CPU最高的进程?
|
自然语言处理 数据可视化 数据挖掘
闭源与开源嵌入模型比较以及提升语义搜索效果的技术探讨
本文探讨了自然语言处理中嵌入技术的应用,重点在于语义搜索及聚类方法。通过对比不同规模的开源与闭源模型,文章展示了如何利用聚类技术过滤无关结果,提高搜索精度。实验结果显示,较小模型如mxbai在某些任务上表现优异,提示我们在追求高性能的同时不应忽视计算效率与成本效益。最后,文章还介绍了重新排序技术,进一步优化检索结果的相关性。
362 7
闭源与开源嵌入模型比较以及提升语义搜索效果的技术探讨
|
负载均衡 Java Nacos
python flask服务如何注册到nacos
一文讲清楚python flask服务如何注册到nacos
583 2
python flask服务如何注册到nacos