再探跳马问题(骑士周游问题)

跳马问题(骑士周游问题)初探 跳马问题也称为骑士周游问题,是算法设计中的经典问题。其一般的问题描述是:考虑国际象棋棋盘上某个位置的一只马,它是否可能只走63步,正好走过除起点外的其他63个位置各一次?如果有一种这样的走法,则称所走的这条路线为一条马的周游路线。试设计一个算法找出这样一条马的周游路线。 此题实际上是一个汉密尔顿通路问题,可以描述为:在一个8×8的方格棋盘中,按照国际象棋中马的行走规则从棋盘上的某一方格 阅读详情

 买回了王晓东的《算法设计与分析习题解答》,书中代码是用Java写的,看了跳马问题的部分,基本理解了算法。首先说明一下,《算法设计与分析》原书的题目其实是要找一条哈密尔顿通路,而《习题解答》中是解哈密尔顿回路的,即不仅要不重复的跳过棋盘的每一个格子,最后还要能回到出发点。先解释一下寻找哈密尔顿回路的算法:

【问题描述】

对于给定的m × n的国际象棋棋盘,m和n均为大于5的偶数,且|m - n| <= 2,试设计一个分治算法找出一条马的哈密尔顿回路。

【算法】

首先,考虑n × n的棋盘,马踏棋盘是黑白相间的,对于一条哈密尔顿回路来说,马在棋盘上所踏过的黑色格子和白色格子相等,因此,棋盘的格子数应为偶数,n不能为奇数。即n为奇数的n × n棋盘不存在哈密尔顿回路(但可能存在哈密尔顿通路)。而对于m × n的棋盘(m != n),m, n均为奇数则不存在哈密尔顿回路。好在题目已

马的Hamilton周游路线问题 问题描述 8*8的国际象棋棋盘上的一只马,恰好走过除起点外的其他63个位置各一次,最后回到起点。这条路线称为马的一条Hamiltion周游路线。对于给定的m*n的国际象棋盘,m,n均为大于5的偶数,且|m-n|<=2,试分析分治算法找出马的一条Hamilton周游路线。 算法设计:对于给定的偶数m,n>=6,且|m-n|<=2,计算m*n的国际象棋盘上马的一条Hamilton周游... 阅读详情

相关推荐

实现2-2马的Hamilton周游路线问题.cpp

实现2-2马的Hamilton周游路线问题.cpp

马的Hamilton周游路线问题(国际象棋)

马的Hamilton周游路线问题,8*8 的国际象棋棋盘上的一只马,恰好走过除起点外的其它63 个位置各一次,最后回 到起点。这条路线称为一条马的Hamilton 周游路线。对于给定的m*n 的国际象棋棋盘,m和n均为大于5 的偶数,且|m-n|≤2,该算法找出一条马的Hamilton周游路线。-

用Python和NumPy搞定复合材料层合板ABD矩阵计算(附完整代码与避坑指南)

本文详细介绍了如何使用Python和NumPy高效计算复合材料层合板的ABD刚度矩阵,包括单层板刚度计算、偏轴转换和层合板刚度集成。通过完整的代码示例和工程实践技巧,帮助工程师快速构建定制化分析工具,解决航空航天、汽车制造等领域的复合材料力学性能分析问题。特别提供了避坑指南和性能优化策略,确保计算结果的准确性和效率。

weixin_30752699的博客 286

保姆级避坑指南:在NVMe SSD上安装Ubuntu 24.04,搞定RST错误和分区方案

本文提供在NVMe SSD上安装Ubuntu 24.04的详细指南,解决RST错误和分区方案问题。从BIOS设置到分区策略优化,帮助用户充分发挥NVMe固态硬盘性能,避免常见安装陷阱,实现无痛安装和后期调优。

weixin_26976635的博客 568

threejs开发可视化数字城市效果

theme: condensed-night-purple 灵感图 现在随着城市的发展,越来越多的智慧摄像头,都被互联网公司布到城市的各个角落,举一个例子,一个大楼上上下下都被布置了智能摄像头,用于监控火势,人员进出,工装工牌佩戴等监控,这时候为了美化项目,大公司都会将城市的区域作为对象,进行3d可视化交互,接下来的内容,就是基于以上元素,开发的一款城市数据可视化的demo,包含楼宇特...

weixin_41770018的博客 1591

ERA5数据驱动WRFV4.4实战:单层嵌套配置与关键报错排查指南

本文详细介绍了使用ERA5再分析数据驱动WRFV4.4模式进行单层嵌套模拟的完整实战流程。内容涵盖从数据精准下载、WPS前处理配置、WRF核心参数设置到高频报错的深度排查与解决方案,旨在帮助初学者在有限服务器资源下,高效完成WRF模拟并规避常见陷阱。

q5r6s7的博客 775

【MATLAB强化学习工具箱】学习笔记--在Simulink环境中训练智能体Create Simulink Environment and Train Agent

Simulink中便于搭建各类动力学与控制模型,通过将原有的控制器替换为AI控制器,可以方便使用已有模型,提供增量效果。 本节的重点是如何引入Simulink模型作为env,其他的内容在之前的文章中已有说明。 以水箱模型watertank为例,如下图所示: 采用PI控制器,控制效果如下所示: 将此PI控制器替换为神经网络控制器后,系统架构如下图所示: 具体替换策略如下所示: (1)删去PID控制器; (2)增加RL Agent模块; (3)观测器模块: 观测向量为:,其中...

bear_miao的博客 1万+

优秀的 Verilog/FPGA开源项目介绍(二十七)- 小型CPU

介绍在FPGA上实现一个CPU是一个令人兴奋的事情,但是太复杂的CPU实现起来太难,而且不适合去了解计算机的架构,所以今天介绍一些小型的CPU,比如8051、MIPS、MSP430等~关于RISC-V的项目,请查看《优秀的 Verilog/FPGA开源项目介绍(二)-RISC-V》ZipCPUhttp://zipcpu.com/about/zipcpu.html项目介绍Z...

OpenFPGA的博客 2583

华为MetaERP Oracle EBS OM(订单管理)模块详解作为ERP财务解决方案架构师,理解OM模块的业务全景和数据模型至关重要。OM是EBS分销链路的核心引擎,贯穿“订单到收款“(Orde

Oracle EBS OM模块是管理销售订单全生命周期的核心模块,涵盖客户管理、订单执行、发运开票、定价及信用控制等功能。其标准流程包括订单录入、预订、发放、发运确认和自动开票等环节,核心数据表涉及订单、客户、发运和定价等业务对象。OM模块与库存、应收、发运等模块深度集成,架构师需重点设计订单类型、信用控制策略和自动开票接口等关键环节,确保业务流程高效流转。

金牌架构师 371

【深度学习新浪潮】OpenClaw技术架构详解:从核心组件到生产级优化

OpenClaw 以三层解耦架构 + 四大核心模块为基础,实现了本地优先、易部署、易扩展、易优化的 AI 智能体平台。开发者可通过极简部署快速落地,再通过量化、缓存、并发、分布式等手段持续优化性能,适合从个人助理到企业级自动化的全场景应用。

专注:太空算力、AI infra、异构调度、大模型成本工程 2923

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

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

上一篇: 跳马问题(骑士周游问题)初探
eshow
博客等级 码龄25年 6粉丝 2原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值