PHP implements graph-based depth-first traversal to output the full arrangement of 1, 2, 3... n, php3... n
The example in this article describes how PHP implements a graph-based depth-first traversal to output the full arrangement of 1, 2, 3... n. We will share this with you for your reference. The details are as follows:
<? Php $ n = $ _ REQUEST ["n"]; if ($ n> 8) {echo "{$ n} is too large, affecting server performance"; return ;} define ("N", $ n); $ d = array (); $ v = array (); for ($ I = 0; $ I <= N; $ I ++) {$ d [$ I] = $ v [$ I] = 0;} function dfs ($ depth) {global $ d, $ v; if ($ depth> = N) {for ($ I = 0; $ I! = N; $ I ++) {echo $ d [$ I];} echo "<br>"; return;} for ($ I = 1; $ I <= N; $ I ++) {if ($ v [$ I] = 0) {$ v [$ I] = 1; $ d [$ depth] = $ I; dfs ($ depth + 1); $ v [$ I] = 0 ;}} dfs (0 );
Here, the get method is used to input the parameter n = 4. The output is as follows:
123412431324134214231432213421432314234124132431312431423214324134123421412341324213423143124321