PHP实现的蚂蚁爬杆路径算法代码


Posted in PHP onDecember 03, 2015

本文实例讲述了PHP实现的蚂蚁爬杆路径算法代码。分享给大家供大家参考,具体如下:

<?php
/**
 * 有一根27厘米的细木杆,在第3厘米、7厘米、11厘米、17厘米、23厘米这五个位置上各有一只蚂蚁。
 * 木杆很细,不能同时通过一只蚂蚁。开始 时,蚂蚁的头朝左还是朝右是任意的,它们只会朝前走或调头,
 * 但不会后退。当任意两只蚂蚁碰头时,两只蚂蚁会同时调头朝反方向走。假设蚂蚁们每秒钟可以走一厘米的距离。
 * 编写程序,求所有蚂蚁都离开木杆 的最小时间和最大时间。
 */
function add2($directionArr, $count, $i) {
 if(0 > $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 '<pre>calculate-';
print_r($pathCalculate);
echo 'sort-';
asort($pathCalculate);
print_r($pathCalculate);

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
用PHP动态创建Flash动画
Oct 09 PHP
发款php蜘蛛统计插件只要有mysql就可用
Oct 12 PHP
php写的简易聊天室代码
Jun 04 PHP
php学习笔记 php中面向对象三大特性之一[封装性]的应用
Jun 13 PHP
PHP和Shell实现检查SAMBA与NFS Server是否存在
Jan 07 PHP
php给每个段落添加空格的方法
Mar 20 PHP
WordPress中&quot;无法将上传的文件移动至&quot;错误的解决方法
Jul 01 PHP
利用“多说”制作留言板、评论系统
Jul 14 PHP
php结合mysql与mysqli扩展处理事务的方法
Jun 29 PHP
docker-compose部署php项目实例详解
Jul 30 PHP
php中错误处理操作实例分析
Aug 23 PHP
PHP常用函数之根据生日计算年龄功能示例
Oct 21 PHP
PHP实现QQ空间自动回复说说的方法
Dec 02 #PHP
如何在旧的PHP系统中使用PHP 5.3之后的库
Dec 02 #PHP
thinkphp微信开发(消息加密解密)
Dec 02 #PHP
thinkphp微信开之安全模式消息加密解密不成功的解决办法
Dec 02 #PHP
PHP接收json 并将接收数据插入数据库的实现代码
Dec 01 #PHP
实例讲解yii2.0在php命令行中运行的步骤
Dec 01 #PHP
PHP简单的MVC框架实现方法
Dec 01 #PHP
You might like
PHP提示Deprecated: mysql_connect(): The mysql extension is deprecated的解决方法
2014/08/28 PHP
9个比较实用的php代码片段
2016/03/15 PHP
PHP微信公众号自动发送红包API
2016/06/01 PHP
基于PHP的微信公众号的开发流程详解
2020/08/07 PHP
JavaScript 自动分号插入(JavaScript synat:auto semicolon insertion)
2009/11/04 Javascript
JS中批量给元素绑定事件过程中的相关问题使用闭包解决
2013/04/15 Javascript
js菜单点击显示或隐藏效果的简单实例
2014/01/13 Javascript
jquery trigger伪造a标签的click事件取代window.open方法
2014/06/23 Javascript
jQuery子属性过滤选择器用法分析
2015/02/10 Javascript
基于jquery实现可定制的web在线富文本编辑器附源码下载
2015/11/17 Javascript
jQuery基于扩展简单实现倒计时功能的方法
2016/05/14 Javascript
Bootstrap免费字体和图标网站(值得收藏)
2017/03/16 Javascript
使用async-validator编写Form组件的方法
2018/01/10 Javascript
Vue点击切换颜色的方法
2018/09/13 Javascript
[06:24]DOTA2亚洲邀请赛小组赛第三日 TOP10精彩集锦
2015/02/01 DOTA
python二叉树的实现实例
2013/11/21 Python
使用scrapy实现爬网站例子和实现网络爬虫(蜘蛛)的步骤
2014/01/23 Python
Python 功能和特点(新手必学)
2015/12/30 Python
Python的socket模块源码中的一些实现要点分析
2016/06/06 Python
基于Python的关键字监控及告警
2017/07/06 Python
Python计算斗牛游戏概率算法实例分析
2017/09/26 Python
python+pandas+时间、日期以及时间序列处理方法
2018/07/10 Python
Pycharm配置远程调试的方法步骤
2018/12/17 Python
Python基本socket通信控制操作示例
2019/01/30 Python
python爬虫简单的添加代理进行访问的实现代码
2019/04/04 Python
python多进程读图提取特征存npy
2019/05/21 Python
Python实现的对一个数进行因式分解操作示例
2019/06/27 Python
python有序查找算法 二分法实例解析
2020/02/18 Python
如何表示python中的相对路径
2020/07/08 Python
Python自定义sorted排序实现方法详解
2020/09/18 Python
python中编写函数并调用的知识点总结
2021/01/13 Python
canvas简易绘图的实现(海绵宝宝篇)
2018/07/04 HTML / CSS
如何高效率的查找一个月以内的数据
2012/04/15 面试题
幼儿园教师请假制度
2014/01/16 职场文书
企业介绍信范文
2015/01/30 职场文书
能用CSS实现的就不要麻烦JavaScript了
2021/10/05 HTML / CSS