: This article mainly introduces the PHP version of the ant crawler path algorithm. if you are interested in the PHP Tutorial, please refer to it.
$ I) {// return $ direararr;} if (0 = $ direararr [$ I]) {// add the current bit 1 $ direararr [$ I] = 1; return $ direararr;} $ direararr [$ I] = 0; return add2 ($ direararr, $ count, $ I-1); // carry} $ positionArr = array (// Location: 3, 7, 11, 17, 23); function path ($ positionArr) {// Generate the test path $ pathCalculate = array (); $ count = count ($ positionArr); $ directionArr = array_fill (0, $ count, 0 ); // orientation $ end = str_repeat ('1', $ coun T); while (true) {$ path = implode ('', $ directionArr); $ pathArray = array_combine ($ positionArr, $ direararr); $ total = calculate ($ positionArr, $ direararr); $ pathCalculate ['P '. $ path] = $ total; if ($ end = $ path) {// break for traversal completion;} $ direararr = add2 ($ direararr, $ count, $ count-1);} return $ pathCalculate;} function calculate ($ positionArr, $ directionArr) {$ total = 0; // $ length = 27 in total; // Wood rod length while ($ PositionArr) {$ total ++; // step increase time $ nextArr = array (); // the next position foreach ($ positionArr as $ key => $ value) {if (0 = $ direararr [$ key]) {$ next = $ value-1; // take a step toward 0} else {$ next = $ value + 1; // take a step toward 1} if (0 = $ next) {// walk out of continue in the 0 direction;} if ($ length = $ next) {// exit continue in the 1 direction;} $ nextArr [$ key] = $ next;} $ positionArr = $ nextArr; foreach ($ nextArr as $ key => $ value) {$ findArr = array_keys ($ positionArr, $ Value); if (count ($ findArr) <2) {// no overlapped position continue;} foreach ($ findArr as $ findIndex) {$ direararr [$ findIndex] = $ direararr [$ findIndex]? 0: 1; // unset ($ positionArr [$ findIndex]); }}return $ total ;}$ pathCalculate = path ($ positionArr); echo'calculate-';print_r($pathCalculate);echo 'sort-';asort($pathCalculate);print_r($pathCalculate);
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.
The above introduces the PHP version of the ant crawler path algorithm, including the content, hope to be helpful to friends interested in PHP tutorials.