This article mainly introduces the ant climbing path algorithm code implemented by PHP, and analyzes the principle and implementation method of the ant climbing Path Algorithm in the form of a complete example, skills related to php numeric calculation and Array Operations.
This article mainly introduces the ant climbing path algorithm code implemented by PHP, and analyzes the principle and implementation method of the ant climbing Path Algorithm in the form of a complete example, skills related to php numeric calculation and Array Operations.
<? Php/*** has a 27 cm sub-pole, each of which has an ant in the five positions 3 cm, 7 cm, 11 cm, 17 cm. * The wooden pole is very fine and cannot pass through an ant at the same time. At the beginning, the head of the ant financial system is either left or right. They only move forward or turn their heads, * but do not move back. When any two ants meet, they both turn their heads to the opposite direction. It is assumed that the ant Financial can walk 1 cm away every second. * Write a program to find the minimum time and maximum time for all ants to leave the wooden pole. */Function add2 ($ direararr, $ count, $ I) {if (0> $ I) {// return $ direararr beyond the calculation range ;} if (0 = $ direararr [$ I]) {// Add 1 $ direararr [$ I] = 1; return $ direararr ;} $ directionArr [$ 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); $ direc TionArr = array_fill (0, $ count, 0); // orientation $ end = str_repeat ('1', $ count); 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 calcul Ate ($ positionArr, $ directionArr) {$ total = 0; // $ length = 27 in total; // The length of the wood rod 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;} $ position Arr = $ nextArr; // set $ positionArr to a temporarily searched array 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]); // prevent repeated search calculation} $ positionArr = $ nextArr; // set $ positionArr to the next result array} return $ total;} $ pathCalculate = path ($ positionArr); echo'
calculate-';print_r($pathCalculate);echo 'sort-';asort($pathCalculate);print_r($pathCalculate);