PHP實(shí)現(xiàn)的螞蟻爬桿路徑算法代碼
更新時(shí)間:2015年12月03日 09:19:47 作者:lsjlnd
這篇文章主要介紹了PHP實(shí)現(xiàn)的螞蟻爬桿路徑算法代碼,以完整實(shí)例形式分析了螞蟻爬桿路徑算法的原理與實(shí)現(xiàn)方法,涉及php數(shù)值計(jì)算與數(shù)組操作的相關(guān)技巧,需要的朋友可以參考下
本文實(shí)例講述了PHP實(shí)現(xiàn)的螞蟻爬桿路徑算法代碼。分享給大家供大家參考,具體如下:
<?php /** * 有一根27厘米的細(xì)木桿,在第3厘米、7厘米、11厘米、17厘米、23厘米這五個(gè)位置上各有一只螞蟻。 * 木桿很細(xì),不能同時(shí)通過一只螞蟻。開始 時(shí),螞蟻的頭朝左還是朝右是任意的,它們只會(huì)朝前走或調(diào)頭, * 但不會(huì)后退。當(dāng)任意兩只螞蟻碰頭時(shí),兩只螞蟻會(huì)同時(shí)調(diào)頭朝反方向走。假設(shè)螞蟻們每秒鐘可以走一厘米的距離。 * 編寫程序,求所有螞蟻都離開木桿 的最小時(shí)間和最大時(shí)間。 */ function add2($directionArr, $count, $i) { if(0 > $i) { // 超出計(jì)算范圍 return $directionArr; } if(0 == $directionArr[$i]) { // 當(dāng)前位加1 $directionArr[$i] = 1; return $directionArr; } $directionArr[$i] = 0; return add2($directionArr, $count, $i - 1); // 進(jìn)位 } $positionArr = array( // 所在位置 3, 7, 11, 17, 23 ); function path($positionArr) { // 生成測(cè)試路徑 $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; // 總用時(shí) $length = 27; // 木桿長(zhǎng)度 while ($positionArr) { $total++; // 步增耗時(shí) $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置為臨時(shí)被查找數(shù)組 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]); // 防止重復(fù)查找計(jì)算 } } $positionArr = $nextArr; // 將$positionArr置為下一步結(jié)果數(shù)組 } return $total; } $pathCalculate = path($positionArr); echo '<pre>calculate-'; print_r($pathCalculate); echo 'sort-'; asort($pathCalculate); print_r($pathCalculate);
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- php 大數(shù)據(jù)量及海量數(shù)據(jù)處理算法總結(jié)
- PHP大轉(zhuǎn)盤中獎(jiǎng)概率算法實(shí)例
- 一組PHP可逆加密解密算法實(shí)例代碼
- php加密算法之實(shí)現(xiàn)可逆加密算法和解密分享
- 適用于抽獎(jiǎng)程序、隨機(jī)廣告的PHP概率算法實(shí)例
- php中最簡(jiǎn)單的字符串匹配算法
- php對(duì)稱加密算法示例
- PHP使用棧解決約瑟夫環(huán)問題算法示例
- PHP常用算法和數(shù)據(jù)結(jié)構(gòu)示例(必看篇)
- php使用高斯算法實(shí)現(xiàn)圖片的模糊處理功能示例
- PHP經(jīng)典算法集錦【經(jīng)典收藏】
- PHP實(shí)現(xiàn)的迪科斯徹(Dijkstra)最短路徑算法實(shí)例
相關(guān)文章
php中magic_quotes_gpc對(duì)unserialize的影響分析
這篇文章主要介紹了php中magic_quotes_gpc對(duì)unserialize的影響,以實(shí)例的形式分析了magic_quotes_gpc安全過濾對(duì)unserialize造成的影響以及對(duì)此的解決方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-12-12php的數(shù)組與字符串的轉(zhuǎn)換函數(shù)整理匯總
以下是對(duì)php中的數(shù)組與字符串的轉(zhuǎn)換函數(shù)進(jìn)行了詳細(xì)的整理匯總,需要的朋友可以參考下2013-07-07PHP上傳文件時(shí)文件過大$_FILES為空的解決方法
發(fā)現(xiàn)一張gif圖片上傳失敗 size為0,實(shí)際大小為4.66M,下面為大家介紹下PHP上傳文件時(shí)文件過大的解決方法2013-11-11php字符串截取函數(shù)mb_substr用法實(shí)例分析
這篇文章主要介紹了php字符串截取函數(shù)mb_substr用法,結(jié)合實(shí)例形式分析了php使用mb_substr針對(duì)中文字符串截取與編碼控制相關(guān)操作技巧,需要的朋友可以參考下2019-06-06使用PHP提取視頻網(wǎng)站頁面中的FLASH地址的代碼
這幾天工作中需要寫個(gè)程序?qū)τ谝粋€(gè)視頻網(wǎng)站地址,如優(yōu)酷的某個(gè)地址,提取出其中的FLASH地址來。2010-04-04