华为OD机试 Python【最优聚集点】

题目

马是象棋(包括中国象棋和国际象棋)中的棋子,走法是每步直一格再斜一格,即先横着或者直者走一格,然后再斜着走一个对角线,可进可退,可越过河界,俗称"马走日"字。

给定 m 行 n 列的棋盘(网格图),棋盘上只有棋子象棋中的棋子“马”,并且每个棋子有等级之分,等级为 k 的马可以跳 1~k 步(走的方式与象棋中“马”的规则一样,不可以超出棋盘位置),问是否能将所有马跳到同一位置,如果存在,输出最少需要的总步数(每匹马的步数相加),不存在则输出-1。

注:允许不同的马在跳的过程中跳到同一位置,坐标为(x,y)的马跳一次可以跳到的坐标为:(x+1, y+2),(x+1, y-2),(x+2, y+1),(x+2, y-1),(x-1, y+2),(x-1, y-2),(x-2, y+1),(x-2, y-1),的格点上,但是不可以超出棋盘范围。

输入描述
第一行输入m,n,代表 m 行 n 列的网格图棋盘(1 ≤ m, n ≤ 25)
接下来输入 m 行 n 列的网格图棋盘,如果第 i 行,第 j 列的元素为 “.” ,代表此格点没有棋子,如果为数字 k(1 ≤ k ≤ 9),代表此格点存在等级为 k 的“马”

输出描述
输出最少需要的总步数(每匹马的步数相加),不存在则输出-1。

用例

输入	
3 2
..

 2.
..
输出	
0

解题思路

首先需要确定所有马能够到达的公共位置,然后计算出将所有

【跳马】最少需要的总步数 (Java & JS & Python & C & C++),实现:广度优先搜索 阅读详情

相关推荐

2025华为OD机试题库目录【E卷+A卷+B卷+C卷+D卷】(JAVA、Python、C++、JS)

1:进入机考网页之后,如果链接上写着【2023.Q1 A卷】,就表示是2023年的Q1题库(4:订阅专栏后,后面更新的答案都可以查看。以后更新的题目也会放到这个专栏里。,如果时间紧迫,建议直接刷2023年新题。5:如果对题解有问题,私信我解答。2:2022年的题库。

AlgorithmHero的博客 2万+

华为OD机试 - 最优资源分配(Python/JS/C/C++ 2024 E卷 200分)

本专栏收录于《华为OD机试真题(Python/JS/C/C++)》。刷的越多,抽中的概率越大,私信哪吒,备注华为OD,加入华为OD刷题交流群,每一题都有详细的答题思路、详细的代码注释、3个测试用例、为什么这道题采用XX算法、XX算法的适用场景,发现新题目,随时更新。某块业务芯片最小容量单位为 1.25G,总容量为 M*1.25G,对该芯片资源编号为 1,2,… M。该芯片支持 3 种不同的配置,分别为 A、B、C.某块板卡上集成了 N 块上述芯片,对芯片编号为 1,2,…,N,各个芯片之间彼此独立,不能跨芯

学Java,找哪吒 1198

华为od机考 Python 目录【E卷+A卷+B卷+C卷+D卷】

1:进入机考网页之后如果链接上写着【B卷】,就表示是B卷题库(**对应着目录中的时间这一列 2023Q2**)。在之后,大多数同学收到的应该是B卷题库。2:2022年的题库,如果时间紧迫,建议直接刷2023年新题。3:如果机试挂了,6个月之后可以再考。4:订阅专栏后,后面更新的答案都可以查看。以后更新的题目也会放到这个专栏里。5:如果对题解有问题,私信我解答。

wtswts1232的博客 2万+

基于 Transformer 架构的大模型智能体高效训练策略研究

本文提出了一套针对Transformer架构大模型智能体的高效训练策略,从内存、样本和计算三个维度优化训练效率。在内存方面,采用梯度检查点+混合精度、LoRA自适应和序列并行技术,将Llama-2-7B模型在1M上下文长度下的显存占用降至72GB。样本效率方面,通过拒绝采样、课程标签平滑和离线-在线混合训练,使WebShop任务的训练步数减少2.1倍。计算效率方面,结合梯度累积、1-bit Adam和FP8训练,将单次实验成本从1000美元压缩至200美元。文章提供了完整的PyTorch代码实现,包括LoR

博客 1095

【华为OD机试真题2023C&D卷 JAVA&JS】跳马

跳马 马是象棋(包括中国象棋和国际象棋)中的棋子,走法是每步直一格再斜一格,即先横着或直着走一格,然后再斜着走一个对角线,可进可退,可越过河界,俗称“马走‘日’字。 给顶m行n列的棋盘(网格图),棋盘上只有有棋子象棋中的棋子“马”,并且每个棋子有等级之分,等级为k的马可以跳1~k步(走的方式与象棋中“马”的规则一样,不可以超出棋盘位置),问是否能将所有马跳到同一位置,如果存在,输出最少需要的总步数(每匹马的步数相加),不存在则输出-1。注:允许不同的马在跳的过程中跳到同一位置,坐标为(x,

qq_34465338的博客 995

VOF模型求解方法:从SIMPLEC到PISO的稳定性优化策略

本文深入探讨了VOF模型求解方法中SIMPLEC与PISO算法的稳定性优化策略,重点分析了两种算法在稳态和瞬态问题中的应用差异。通过对比PRESTO离散化方法与其他格式的优劣,提供了针对多相流模拟的实用设置建议和稳定性优化技巧,帮助工程师有效提升计算效率和精度。

yellow的专栏 1154

从原理到调试:手把手教你用Matlab解码互补格雷码与相移码(附数据与常见错误排查)

本文详细介绍了如何使用Matlab解码互补格雷码与相移码,涵盖从原理到调试的全过程。通过实际案例和代码示例,帮助读者掌握结构光三维重建中的相位解包裹技术,并提供了常见错误排查与优化建议,提升解码效率与准确性。

weixin_29090917的博客 221

土地利用数据从1972到2023:如何利用Landsat和Sentinel数据构建长期变化监测模型

本文详细解析了如何利用1972至2023年的Landsat和Sentinel遥感数据构建土地利用长期变化监测模型。通过多源数据融合策略、时间序列预处理流程和深度学习算法,实现高精度的土地利用分类与变化检测,为生态评估和城市规划提供科学依据。文章还探讨了验证方法和实际应用案例,展示了长期遥感数据在环境建模中的独特价值。

weixin_28720573的博客 447

透传产品上报AEP平台数据流程

1. 什么是数据透传 数据透传:指的是在通讯中不管传输的业务内容如何,只负责将传输的内容由源地址传输到目的地址,而不对业务数据内容做任何改变。 这篇文章描述了如何将一台NB设备通过透传方式将数据传输到AEP平台,再通过数据订阅的方式获取到这些数据。 2. 步骤 2.1 设备在AEP平台创建 AEP:https://dm.ctwing.cn/ 新建产品 添加设备 根据模组的15位IMEI添加设备 开启内网穿透 具体可参照我的这篇博客:内网穿透-如何进行本地数据调试 设置数据推送

qq_42265394的博客 2297

手把手教您国内安装 Claude Code 并配置自定义大模型(包含New-API中转和可视化管理工具的使用)

国内免魔法畅用 Claude Code!手把手教你 Windows 安装、配置文件写法、对接阿里百炼 / DeepSeek / 智谱✨,附完整参数、正确命令、三方工具管理方案,新手一次成功!✅

XiaoqiangClub的博客 3788

【报错解决】彻底解决 CondaHTTPError: HTTP 000 CONNECTION FAILED for url 报错

CondaHTTPError: HTTP 000 CONNECTION FAILED for url 错误本质上是由于 Conda 无法访问远程仓库引起的,常见原因包括网络不通、防火墙或安全软件阻拦、SSL 证书异常以及默认仓库不可达。解决该问题的关键在于:确保网络畅通、配置国内稳定镜像源、调整防火墙和安全设置、处理 SSL 证书问题,并保持 Conda 版本更新。通过这些方法,几乎可以彻底消除连接失败问题,保证 Conda 环境的正常使用和包管理的稳定性。

博客 1164

鲁棒控制理论 --- H∞ 控制(被裁篇)

鲁棒控制理论 --- H∞ 控制

记录老板的每次pua,分享领导每天的毒鸡汤... 6万+

【网络】叶脊(Spine-Leaf)网络拓扑下全三层网络设计与实践(二) - 网络规划

2. 全三层架构设计思路 采用全三层网络架构设计的出发点是: 使用尽量少的交换机高级功能,可以尽可能减少由于功能bug故障造成的问题; 使用尽量少的网络协议,以兼容不通厂商的网络设备 实现简单,易于维护,绝对可靠的路由协议; 通过控制路由来调度流量,尽量减少设备或协议的故障域; 牺牲高级功能带来的便捷,得到更高的可用性和性能; 由上述原则,本文的设计选择只使用静态路由和BGP...

因吹斯汀 1万+

深度学习工具包 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")

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

lwip-2.0.3

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

上一篇: 华为OD机试 Java【最优聚集点】
下一篇: 华为OD机试 C++【亲子糖果赛】
AlgorithmHero
博客等级 码龄4年 606粉丝 1067原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

打赏作者

AlgorithmHero

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

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

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

打赏作者

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

抵扣说明:

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

余额充值