This article mainly introduces the method of implementing the Serpentine matrix, the loopback matrix and the digital spiral matrix in PHP, and analyzes the concept of the Serpentine matrix, the loopback matrix and the digital spiral matrix, the expression method and the PHP implementation technique with the concrete instance form, and the friends can refer to the following
Specific as follows:
The loopback matrix refers to a matrix sequence that increases sequentially from the beginning to the right, the bottom, and the left in order, for example:
1 2 38 9 47 6 5
Now requires:
Input: M, n, representing the number of rows and columns, respectively
Output: M * N loopback Matrix
Cases:
Input:
7 8
Output:
1 2 3 4 5 6 7 826 27 28 29 30 31 32 925 44 45 46 47 48 33 1024 43 54 57 56 49 34 1123 42 53 52 51 50 35 1222 41 40 39 38 3 7 36 1321 20 19 18 17 16 15 14
Next we'll do it in PHP, where we encapsulate it as a function call
The first way of thinking
Directly follow the top, right, bottom and left of the order to traverse, calculate the number of traversal layer is OK
Function Snake ($row = 5, $col = 5) { //result set $res = Array (); Initial value $start = 1; The number of currently traversed layers $flag = intval (($row + 1)/2); for ($i = 1; $i <= $flag; $i + +) { $startX = $i-1; $startY = $i-1; $width = $col-$i + 1; $height = $row-$i + 1; On for ($u = $startY; $u < $width; $u + +) { $res [$startX] [$u] = $start; $start + = 1; } Right for ($r = $startX + 1; $r < $height; $r + +) { $res [$r] [$u-1] = $start; $start + = 1; } Under for ($d = $u-1-1; $d >= $startY; $d-) { $res [$r -1][$d] = $start; $start + = 1; } Left for ($l = $r-1-1; $l >= $startX + 1; $l-) {$res [$l] [$d +1] = $start; $start + = 1; } } Output for ($i = 0; $i < $row; $i + +) {for ($j = 0; $j < $col; $j + +) { echo $res [$i] [$j]. " "; } echo "<br/>"; }} Snake (7, 8);
The second way of thinking
This way of thinking is similar to the first, but through a while all traversal, and then through a flag bit up to the left to judge and modify the current direction, through the if in the while to judge the flag bit, this does not post code
The third way of thinking
Using an iterator to control the direction, then the M * N Cycles, by judging the width and the isset to determine whether to turn, the idea is great, the code amount is greatly reduced, indicating that this way is not thought.
*/* * @param $w: w * @param $h: High * @param $s: Starting number * @param $x, $y: Starting position coordinates can only start at four vertices * @param $r: Direction default Shun time false to counterclockwise * */function Print_matrix ($w, $h, $s = 1, $l = 1, $x = 0, $y = 0, $r = True) {///= Four Directions $R = Array (array (1, 0), array (0 , 1), Array ( -1, 0), array (0,-1)); ! $r && $R = Array_reverse ($R); Create an infinite iterator $iterator = new Infiniteiterator (new Arrayiterator ($R)); $iterator->rewind (); List ($_x, $_y) = $iterator->current (); $result = []; $result [$x] [$y] = $s; for ($i = $s +1; $i < ($s + $w * $h); $i + +) {$new _x = $x + $_x; $new _y = $y + $_y; if (0 <= $new _x && 0 <= $new _y && $new _x < $w && $new _y < $h &&!isset ($result [$new _x] [$new _y])) {$result [$new _x][$new _y] = $i; $x = $new _x; $y = $new _y; } else {$iterator->next (); List ($_x, $_y) = $iterator->current (); $i--; }}//print for ($i = 0; $i < $h; $i + +) {for ($j = 0; $j < $w; $j + +) {echo $result[$j] [$i], "\ t"; } echo "<br/>"; }}
Related recommendations:
How PHP operates two-dimensional array matrix transpose
An example of how PHP implements a clockwise print matrix (Spiral matrix )
The method and case of implementing matrix transpose operation of two-dimensional array in PHP