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

小米路由器安装Misstar tools 和MIXBOX 1.该教程适用于如下路由器: 小米路由器1代硬盘版(R1D) 小米路由器mini(R1CM) 全新小米路由器硬盘版(R2D) 小米路由器3(R3) 小米路由器3G(R3G) 小米路由器Pro(R3P) 小米路由器HD(R3D) 2.Misstar2.0在线安装 方法如下: 1.根据路由器对应文件解压文件,把解压目录(比如小米3路由器为R3)和offline_install.sh到U盘根目录,并插入路由器 2.ssh登录后,cd到U盘的目录,比如小米3路... 阅读详情

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;
}


拼多多全站商品SKU数据爬虫实战:Python自动化采集与清洗 在电商数据分析领域,SKU(Stock Keeping Unit,库存量单位)数据是洞察商品策略、价格体系和市场竞争格局的核心抓手。本文以拼多多平台为研究对象,系统讲解如何利用Python构建一套完整的全站商品SKU自动化采集与清洗流程。文章涵盖目标分析与反爬策略、网络请求模拟、数据解析、分布式存储、数据清洗与标准化,以及调度与运维等全链路环节,并提供可直接运行的代码示例。拼多多API返回的数据结构大致如下(已脱敏):json"goods_name": "某品牌运动鞋", 阅读详情

相关推荐

Paho Asynchronous MQTT C Client Library--MQTTAsync API

异步MQTT 客户端库for C

yoyo勰的博客 1264

Knight Moves(双向BFS)

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

qq_45706306的博客 467

Unity AssetBundle解包实战指南:从工具使用到编程解析

在游戏开发与资源管理中,资源打包与加载是核心技术环节。AssetBundle作为Unity引擎官方推荐的资源分发方案,其本质是一个包含头部信息、目录表、数据区及依赖关系的结构化文件系统,通过序列化机制将游戏资源转换为二进制格式。理解AssetBundle的机制对于实现高效的热更新、减少资源冗余、优化加载性能具有重要价值,广泛应用于游戏开发、竞品分析、技术研究等场景。本文聚焦AssetBundle解包这一实践需求,系统介绍如何利用AssetStudio、UABE等主流工具进行资源提取与可视化分析,并深入探讨通

weixin_28192383的博客 347

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

C#与西门子PLC S7-1500 S7协议PLC读写数据

/获取对应长度的字符串。//将val字符串转换为字符数组。//String写入。//String读取。//PLC类型 IP地址 机架号 槽号。1.类库使用S7netplus。

softshow1026的博客 5576

图信号处理中的多尺度分析:图小波变换与图傅里叶变换的对比与应用

本文深入探讨了图信号处理中的多尺度分析方法,重点对比了图小波变换(GWT)与图傅里叶变换(GFT)的技术特点与应用场景。通过分析图的拓扑结构,GWT展现出强大的局部洞察力,适用于异常检测和社区发现;而GFT则擅长全局频率分析,适合趋势分析和信号去噪。文章结合实战案例,为读者提供了方法选型指南和前沿进展,帮助开发者在生物信息学、社交网络分析等领域高效应用这些技术。

weixin_30369041的博客 406

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

R语言分段回归

1.利用虚拟变量进行线性回归 (MEdata.csv的数据来源:https://github.com/ChenQihome9/优快云-Data-Library/blob/master/MEdata.csv) 问题13:针对TAX~GDP,使用分段回归方法,找到其回归方程。编程,并根据程序结果叙述如下几方面问题:(1)分几段最佳?(不超过5段内)(2)分段位置定在哪儿最佳?(3)写出最佳方...

ChenQihome9的博客 1万+

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

Python计算时间差的方法

使用datetime模块是Python内置的方法,使用timedelta对象可以更精确地计算时间差,而使用dateutil库则提供了更为灵活和方便的操作方式。我们可以使用datetime模块中的datetime类来表示一个具体的日期和时间,并进行相应的运算。在Python中,我们经常需要计算日期和时间之间的差异,例如计算两个日期之间的天数、小时数、分钟数等。Python提供了多种方法来计算时间差,下面我们将介绍其中的几种常用方法,并附上相应的源代码。在上面的代码中,我们使用了dateutil库中的。

学习使你进步。 2086

[PCIe5.0] 5.3.1.4.2 D3Cold State

主电源(Vcc)完全断开,仅保留极少量辅助电源(Vaux)用于维持基本唤醒功能设备功能完全关闭,所有寄存器状态丢失(除非有特殊存储)需要完全重新初始化才能恢复工作比D3Hot状态功耗更低,但唤醒延迟更长。

系统固件工程师 452

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

16、保障Azure应用安全:密钥管理与身份认证

本文深入探讨了保障Azure应用安全的关键措施,包括使用网络安全工具(如Application Gateway和Azure Front Door)防范攻击,解决传统密钥和机密存储方式的安全隐患,以及通过Azure Key Vault实现敏感信息的安全管理。同时,介绍了使用托管标识实现Azure资源间安全访问的方法,并结合实际操作步骤展示了如何配置Key Vault、设置访问策略、存储机密以及授予Web应用对SQL Server的访问权限。最后,对不同安全措施进行了比较,并提出了在开发、测试及生产环境中的综合

rock5的博客 72

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

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

小华在努力~ 544

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

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

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

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

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

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、付费专栏及课程。

余额充值