backtracking algorithm

Baidu Encyclopedia explains:

The backtracking algorithm is actually a search attempt process similar to enumeration, which is mainly to find the solution of the problem during the search attempt process. The backtracking method is an optimal search method, which searches forward according to the optimal conditions to achieve the goal. However, when the exploration reaches a certain step, and it is found that the original choice is not optimal or fails to achieve the goal, it will take a step back and re-selection. called "backtracking points". Many complex and large-scale problems can use the backtracking method, which is known as the "universal problem-solving method".

Now for some practical exercises

If we have 1, 2, 3, 4, how many permutations and combinations of numbers are allowed to repeat, we use the program to realize this problem.

directly to the code:

$result = array();
function pailie($arr, $res = array())
{
    global $result;
    static $arr_count;
    if(empty($arr_count))
    {
        $arr_count = count($arr);
    }
    if(count($res) == $arr_count)
    {
        $result[] = $res;
        return;
    }
    foreach($arr as $key => $num)
    {
        $res[] = $num;
        pailie($arr, $res);
        array_pop($res);
    }
}

$arr = array(1, 2, 3, 4);

pailie($arr);

echo count($result) . '<br>';

foreach($result as $r)
{
    echo implode(',', $r);
    echo '<br>';
}

If I change the previous question to a permutation and combination of numbers that cannot be repeated, we only need to change the function slightly:

function pailie($arr, $res = array())
{
    global $result;
    static $arr_count;
    if(empty($arr_count))
    {
        $arr_count = count($arr);
    }
    if(count($res) == $arr_count)
    {
        $result[] = $res;
        return;
    }
    foreach($arr as $key => $num)
    {
        $res[] = $num;
        //回溯的时候删掉当前这个数字
        $tm_arr = $arr;
        unset($tm_arr[$key]);
        pailie($tm_arr, $res);
        array_pop($res);
    }
}

One more question:

There is a staircase of N steps, one step can go up one or two steps. Design a function that takes any positive integer N and lists all possible moves. For example, if the number of steps is 4, the walking method is as follows:
1,1,1,1
1,2,1
1,1,2
2,1,1
2,2
If the person has long legs, it is possible to cross several steps at a time, such as incoming How to design the function of the ladder array {3,5,7}?

Personal use of backtracking algorithm to solve the problem code is as follows:

$result = array();
function zoulu($n, $exist = array(), $res = array())
{
    global $result;
    if(!empty($res))
    {
        $sum = array_sum($res);
        if($sum > $n)
            return false;
        else if($sum == $n)
        {
            $result[] = $res;
        }
    }
    foreach($exist as $e_one)
    {
        $res[] = $e_one;
        zoulu($n, $exist, $res);
        array_pop($res);
    }
}

$exist = array(4, 7);
zoulu(30, $exist);

echo count($result) . '<br>';

foreach($result as $r)
{
    echo implode(',', $r);
    echo '<br>';
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325168946&siteId=291194637