HDU 1372 Knight Moves——————BFS , 优先队列

LC与RC滤波电路实战对比:从原理到选型指南 本文深入对比了LC与RC滤波电路的核心原理与实战应用。LC滤波利用电感与电容的无损能量搬运,适合高频、大电流电源滤波,效率高但需注意谐振风险;RC滤波通过电阻消耗能量实现滤波,结构简单、成本低,适用于低频信号处理或小电流供电场景。文章提供了从工作原理、性能维度到具体选型的完整指南,帮助工程师根据项目需求(如频率范围、负载电流、效率)做出精准选择。 阅读详情

Knight Moves

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13742    Accepted Submission(s): 8060


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.


#include<bits/stdc++.h>
using namespace std;
char s1[5];
char s2[5];
bool vis[10][10];
int d[8][2]={-2,1, -2,-1, -1,2, -1,-2, 1,2, 1,-2, 2,1, 2,-1};
int stx,sty,edx,edy,ans;

struct node{
    int x,y,step;
    node(){};
    node(int _x,int _y,int _step)
    {
        x=_x; y=_y; step=_step;
    }
    bool operator <(const node &b)const
    {
        return step>b.step;
    }
};

void bfs(int x,int y)
{
    vis[x][y]=1;
    node e1,e2;
    priority_queue<node> que;
    que.push(node(x,y,0));
    while(que.size())
    {
        e1=que.top();que.pop();
        if(e1.x==edx && e1.y==edy)
        {
            ans=e1.step;
            break;
        }
        for(int i=0;i<8;i++)
        {
            e2.x=e1.x+d[i][0];
            e2.y=e1.y+d[i][1];
            e2.step=e1.step+1;
            if(!vis[e2.x][e2.y] && 0 < e2.x && e2.x < 9 &&  0 < e2.y && e2.y < 9 )
                que.push(e2);
        }
    }
}



int main()
{
    while(~scanf("%s %s",s1,s2))
    {
//        printf("%s %s\n",s1,s2);
        stx=s1[0]-'a'+1;
        sty=s1[1]-'0';
        edx=s2[0]-'a'+1;
        edy=s2[1]-'0';
        memset(vis,0,sizeof(vis));
        ans=0;
        bfs(stx,sty);
        printf("To get from %s to %s takes %d knight moves.\n",s1,s2,ans);
    }
    return 0;
}


lwip-2.0.3 lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。 立即下载

相关推荐

深入瑞芯微SDK:手把手解析Uboot、Kernel到Rootfs的镜像生成与烧录

本文深入解析瑞芯微SDK中从Uboot、Kernel到Rootfs的镜像生成与烧录全流程,揭示编译背后的技术细节。通过详细分析SDK架构、镜像生成流水线、分区表定制、Rootfs构建及烧录科学,帮助开发者从会编译进阶到懂原理,掌握系统级开发的关键技术。

weixin_29193359的博客 271

Knight Moves(双向BFS)

题目大意 一个棋盘有八行八列,给出起点和终点求骑士的最小移动步骤。 解题思路 如果使用前面的bfs应该也是可以的,但是我们从双向bfs的思路出发。 由于起点和终点都很明确,因此我们可以从起点和终点分别bfs,当两个bfs在扩展时出现重复扩展的状态,表明从起点出发和从终点出发的两条路径连通,根据bfs的性质(在队首的一定是扩展次数少的)可以得到此时骑士移动的次数就是最少次数,搜索结束。 源代码 #include<iostream> #include<cstdio> #include&l

qq_45706306的博客 467

SECS/GEM EAP 设备自动化功能原理和设计方案

半导体SECS GEMEAP实现了对生产线上机台的实时监控,是工厂自动化不可缺少的控制系统。EAP系统与FAB中的机台紧密相关,系统的设计与开发必须与生产线的机台实际生产流程相一致,才能达到控制机台生产的目的。 EAP SECS/GEM是MES与设备的桥梁,EAP通过SECS国际标准协议与机台进行数据传输。SECS是半导体设备(半导体行业称设备为机台)必须遵循的一种国际通信协议。EAP就是通过SECS与设备通信、传输数据、发送指令控制设备按照预先定义........

semisecs 17万+

POJ1915Knight Moves(单向BFS + 双向BFS)

题目链接 单向bfs就是水题 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 #include <queue> 6 using namespace std; 7 c...

apk6909的博客 143

StarUML最新版汉化与破解二合一教程:5分钟搞定永久使用

本文提供StarUML最新版的高效使用指南,涵盖从安装到深度定制的全流程优化方案。包括软件获取最佳实践、界面个性化设置、核心建模技巧、团队协作策略及性能优化建议,帮助架构师和开发团队提升UML建模效率。特别推荐版本控制集成和文档自动化输出方案,适用于各类规模的项目开发。

weixin_42530732的博客 452

【计算机组成原理】实验4:单周期CPU(Verilog)中海大

【计算机组成原理】实验4:单周期CPU

m0_63023532的博客 3660

POJ1915 Knight Moves【BFS】

  Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27199   Accepted: 12856   Description Background Mr Somurolov, fabulous chess-gamer indeed, asserts that...

海岛Blog 1274

告别手动群发!用IDA Pro和CE搞定企业微信4.1.38.6011的HOOK开发(附完整代码)

本文详细介绍了如何利用IDA Pro和Cheat Engine进行企业微信4.1.38.6011的HOOK开发,实现自动化群发功能。从逆向工程环境配置、关键函数定位到HOOK框架设计,提供完整代码示例,帮助企业提升运营效率。

weixin_42533120的博客 479

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

feng_zhiyu的博客 619

RK3588,V4l2 读取Gmsl相机, Rga yuv422转换rgb (dma), 实现零拷贝

RK3588, 使用V4l2 读取 gmsl 相机,获得yuv422格式图像, 使用 rga 转换 rgb 图像。减少cpu占用率. 内存管理方式采用 dma,实现零拷贝.

J。 1262

晶闸管选型参数,结构原理,工艺与注意问题总结

闸管(Thyristor)是一种半导体开关器件,也被称为可控硅整流器(SCR, Silicon Controlled Rectifier)。它可以在高电压和大电流条件下工作,常被用于交流电的控制和整流。本文对晶闸管的工作原理,结构特点,工艺流程,选型参数及设计注意事项进行总结。

fydar的博客 1284

HDU-1915-Knight Moves(双向BFS)

Knight Moves Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 25071 Accepted: 11863 DescriptionBackground Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else

Prim 495

openmediavault(OMV)(30)插件介绍(3)filebrowser文件管理器

filebrowser可以对omv的文件进行图形化管理,而不需要每次都通过命令进行修改。filebrowser安装。

Monster✺◟(∗❛ัᴗ❛ั∗)◞✺的博客 2320

hdu 1372 || poj 2243 Knight Moves(搜索:BFS+优先队列水题)

给出两个坐标,问你按象棋马走日的方式最少需要跳几次

小华在努力~ 544

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

HDU-1372 Knight Moves

HDU 1372 Knight Moves 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...

你呢的博客 935

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Knight Moves

Knight Moves  HDU - 1372  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 se...

qq_42759455的博客 223

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

HDU 1372 Knight Moves 【BFS+特殊例子】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 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 ...

甜晨流年 325

HDU - 1372 ——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 chessboa...

xiang_hehe的博客 206

hdu1372 Knight Moves

Background Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him? The Problem Your task is to write

王小二的博客 415

HDU-1372-Knight Moves

E - Knight Moves Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u SubmitStatusPracticeHDU 1372 Description A friend of you is doing research on the Traveling Knight Prob

Prim 389

第1部分 基础算法(提高篇)--第4章 广搜的优化技巧-1450:【例 3】Knight Moves

1450:【例 3】Knight Moves 时间限制: 1000 ms 内存限制: 65536 KB 提交数: 1170 通过数: 406 【题目描述】 原题来自:POJ 1915 编写一个程序,计算一个骑士从棋盘上的一个格子到另一个格子所需的最小步数。骑士一步可以移动到的位置由下图给出。 【输入】 第一行给出骑士的数量 n。 在接下来的 3n 行中,每 3 行描述了一个骑士。其中, 第一行一个整数 L 表示棋盘的大小,整个棋盘大小为 L×L; 第二行和第三行分别包含一对整数 (

zqhf123的博客 1017

pku 1915 Knight Moves(双向BFS)

一直听到大家在说双向BFS,自己从来没用,于是今天也找了个题目写着玩。直接写的,没有参照别人代码。候选结点,用了两个队列保存。至于已经到达的状态,一个数组足矣。从起点出发到达的用正数表示,从终点出发到达的用负数表示。然后当整数碰到负数,说明找到了,结束。#include #include using namespace std;int n;int move[8][2

low coder 987

Knight Moves (双向bfs)

# 10028. 「一本通 1.4 例 3」Knight Moves 【题目描述】 编写一个程序,计算一个骑士从棋盘上的一个格子到另一个格子所需的最小步数。骑士一步可以移动到的位置由下图给出。 【算法】 双向bfs优先扩展节点数少的队列。什么破东西速度没快多少啊。。。。 【代码】 #include <stdio.h> #include <queue> #defi...

321

BFS - Knight Moves

Background  Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him?  The Problem  Your task is to write

TPH530539250的博客 338

Knight Moves(BFS)

http://acm.hdu.edu.cn/showproblem.php?pid=1372题目描述: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 ea...

someng的博客 227

Knight Moves(BFS,模拟队列)

#include<iostream> #include<cstring> #include<string> #include<cstdio> #define maxn 310 using namespace std; int used[maxn][maxn]; int dis[8][2]={{-2,1},{-1,2},{1,2},{2,1},{...

qq_44722533的博客 261
上一篇: HDU 1241 Rescue ——————BFS , 优先队列
下一篇: HDU 2612 Find a way ——————BFS + 打表
陶鸿杰
博客等级 码龄9年 93粉丝 298原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值