蚂蚁爬杆问题php,PHP实现的蚂蚁爬杆路径算法代码_php技巧
$i) { // 超出计算范围
return $directionArr;
}
if(0 == $directionArr[$i]) { // 当前位加1
$directionArr[$i] = 1;
return $directionArr;
}
$directionArr[$i] = 0;
return add2($directionArr, $count, $i - 1); // 进位
}
$positionArr = array( // 所在位置
3,
7,
11,
17,
23
);
function path($positionArr) { // 生成测试路径
$pathCalculate = array();
$count = count($positionArr);
$directionArr = array_fill(0, $count, 0); // 朝向
$end = str_repeat('1', $count);
while (true) {
$path = implode('', $directionArr);
$pathArray = array_combine($positionArr, $directionArr);
$total = calculate($positionArr, $directionArr);
$pathCalculate['P'.$path] = $total;
if($end == $path) { // 遍历完成
break;
}
$directionArr = add2($directionArr, $count, $count - 1);
}
return $pathCalculate;
}
function calculate($positionArr, $directionArr) {
$total = 0; // 总用时
$length = 27; // 木杆长度
while ($positionArr) {
$total++; // 步增耗时
$nextArr = array(); // 下一步位置
foreach ($positionArr as $key => $value) {
if(0 == $directionArr[$key]) {
$next = $value - 1; // 向0方向走一步
} else {
$next = $value + 1; // 向1方向走一步
}
if(0 == $next) { // 在0方向走出
continue;
}
if($length == $next) { // 在1方向走出
continue;
}
$nextArr[$key] = $next;
}
$positionArr = $nextArr; // 将$positionArr置为临时被查找数组
foreach ($nextArr as $key => $value) {
$findArr = array_keys($positionArr, $value);
if(count($findArr) < 2) { // 没有重合的位置
continue ;
}
foreach ($findArr as $findIndex) {
$directionArr[$findIndex] = $directionArr[$findIndex] ? 0 : 1; // 反向处理
unset($positionArr[$findIndex]); // 防止重复查找计算
}
}
$positionArr = $nextArr; // 将$positionArr置为下一步结果数组
}
return $total;
}
$pathCalculate = path($positionArr);
echo 'calculate-';
print_r($pathCalculate);
echo 'sort-';
asort($pathCalculate);
print_r($pathCalculate);
蚂蚁爬杆问题php,PHP实现的蚂蚁爬杆路径算法代码_php技巧相关推荐
- php 路径算法,PHP实现的蚂蚁爬杆路径算法代码
/** * 有一根27厘米的细木杆,在第3厘米.7厘米.11厘米.17厘米.23厘米这五个位置上各有一只蚂蚁. * 木杆很细,不能同时通过一只蚂蚁.开始 时,蚂蚁的头朝左还是朝右是任意的,它们只会朝前 ...
- 蚂蚁爬绳java_腾讯面试题——蚂蚁爬行(Java版)
/** * 蚂蚁类,用于记录蚂蚁的状态及控制蚂蚁行为 */ public class Ant { /** * 蚂蚁爬行方向枚举值 */ public interface Direction { /** ...
- python爬取贴吧所有帖子-通过python爬取贴吧数据并保存为word
前言 Python是一种跨平台的计算机程序设计语言.是一种面向对象的动态类型语言,最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越来越多被用于独立的.大型项目的开发 ...
- python爬取百度百科词条-python简单爬虫爬取百度百科python词条网页
目标分析: 目标:百度百科python词条相关词条网页 - 标题和简介 入口页:https://baike.baidu.com/item/Python/407313 URL格式: - 词条页面URL: ...
- 蚂蚁金服高级技术专家徐红星 :蚂蚁金服大数据开放式创新实践
8月30-31日20:00-21:30,一场别开生面的技术大会-- "蚂蚁金服&阿里云在线金融技术峰会"将在线举办.本次将聚焦数据库.应用架构.移动开发.机器学习等热门领域 ...
- 蚂蚁金服11.11:支付宝和蚂蚁花呗的技术架构及实践
http://www.infoq.com/cn/articles/technical-architecture-of-alipay-and-ant-check-later 每年"双11&qu ...
- python 机器学习 爬取关键字_5行Python就能爬取 3000+ 上市公司的信息?
↑↑↑点击上方蓝字,回复资料,10个G的惊喜作者:苏克源自:https://www.makcyun.top/web_scraping_withpython18.html入门爬虫很容易,几行代码就可以, ...
- python爬去百度百科词条_python简单爬虫爬取百度百科python词条网页
目标分析: 目标:百度百科python词条相关词条网页 - 标题和简介 入口页:https://baike.baidu.com/item/Python/407313 URL格式: - 词条页面URL: ...
- python爬取58同城租房信息,用selenium爬取58同城租房信息(万级数据)
今天想做一个58同城的爬虫,然后到页面分析一下链接,发现58同城的链接的有些参数是由js动态生成的,然后我就想偷懒了.(当然其实去js文件中找到生成式并不难),但我就是不想去找.然后就想到了selen ...
最新文章
- 压缩感知专题笔记——目录
- 《Angular4从入门到实战》学习笔记
- python代码教程-(Python基础教程之三)Python代码中添加注释
- 新版ffmpeg PCM编码到AAC,swr_convert转换采样精度,稍微修改兼容PCM编码为G711A及MP3,记录下。
- Okhttp、Volley和Gson的简单介绍和配合使用
- kubesphere devops介绍、访问账密及nfs数据备份同步
- 修改用户名_新华美育查找用户名及修改密码的方法分享
- intellij中出現java.lang.NoClassDefFoundError: org/apache/commons/configuration/Configuration解決方案
- 如何用 javascript 做一个高逼格的进度条
- 预安装CentOS6.5,虚拟机VMware Workstation12配置
- 快速显示代码信息工具 Tokei
- c语言ics什么意思,[转载]C网来话筛选(ICS)业务及实现
- 解决LInux更新慢的问题, 更换国内软件源
- 算法--帕斯卡三角(杨辉三角)JS写法
- php模板怎么导入数据类型,smarty模板引擎之分配数据类型,smarty模板数据类型_PHP教程...
- word2vec 在 非 自然语言处理 (NLP) 领域的应用
- R语言中的字符串处理函数
- 受力分析软件_管桁架结构的受力特点是什么?如何计算?
- 证明彼得森图不是平面图
- 【ENVI】基于Landsat遥感影像的盐城市土地利用信息提取