Home  >  Article  >  Backend Development  >  Detailed graphic explanation of how to implement snake matrix, loop matrix and digital spiral matrix in PHP

Detailed graphic explanation of how to implement snake matrix, loop matrix and digital spiral matrix in PHP

墨辰丷
墨辰丷Original
2018-05-22 10:04:181470browse

This article mainly introduces the method of realizing snake matrix, loop matrix and digital spiral matrix in PHP. It analyzes the concepts, representation methods and PHP implementation skills of snake matrix, loop matrix and digital spiral matrix based on specific examples. Friends in need can refer to the following

. The details are as follows:

The loop matrix refers to a matrix sequence that starts from the beginning and continues to increase in the order of top, right, bottom, and left. For example:

1 2 3
8 9 4
7 6 5

Now requires:

Input: m, n, representing the number of rows and columns respectively
Output: m * n Loop matrix

Example:

Input:

7 8

Output:

1 2 3 4 5 6 7 8
26 27 28 29 30 31 32 9
25 44 45 46 47 48 33 10
24 43 54 57 56 49 34 11
23 42 53 52 51 50 35 12
22 41 40 39 38 37 36 13
21 20 19 18 17 16 15 14

Next we use PHP to implement it, here we encapsulate it into a function call

The first idea

Directly follow the above, Traverse in the order of right, bottom and left, and it will be OK after calculating the number of traversed layers

function snake($row = 5, $col = 5)
{
  // 结果集
  $res = array();
  // 初始值
  $start = 1;
  // 当前遍历层数
  $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;
    // 上
    for ($u = $startY; $u < $width; $u++) {
      $res[$startX][$u] = $start;
      $start += 1;
    }
    // 右
    for ($r = $startX + 1; $r < $height; $r++) {
      $res[$r][$u-1] = $start;
      $start += 1;
    }
    // 下
    for ($d = $u - 1 - 1; $d >= $startY; $d--) {
      $res[$r-1][$d] = $start;
      $start += 1;
    }
    // 左
    for ($l = $r - 1 - 1; $l >= $startX + 1; $l--) {
      $res[$l][$d+1] = $start;
      $start += 1;
    }
  }
  // 输出
  for ($i = 0; $i < $row; $i++) {
    for ($j = 0; $j < $col; $j++) {
      echo $res[$i][$j] . " ";
    }
    echo "<br />";
  }
}
snake(7, 8);

Second idea

This idea is similar to the first one, but it traverses everything through a while, and then uses a flag bit up right down left to judge and modify the current direction, and uses the if in the while to judge the flag bit. This I won’t post the code

The third idea

Use an iterator to control the direction, and then loop m * n times, by judging the width, height and isset To determine whether to turn or not, the idea is great and the amount of code is greatly reduced, indicating that this method has not been thought of. .

/* *
 * @param $w : 宽
 * @param $h : 高
 * @param $s : 起始数字
 * @param $x, $y : 起始位置坐标 只能从四顶点开始
 * @param $r :方向 默认顺时间 false为逆时针
 *
 */
function print_matrix($w, $h, $s = 1, $l = 1, $x = 0, $y = 0, $r = true) {
  // 表示四个方向
  $R = array(array(1, 0), array(0, 1), array(-1, 0), array(0, -1));
  !$r && $R = array_reverse($R);
  // 创建一个无限迭代器
  $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--;
    }
  }
  // 打印
  for ($i = 0; $i < $h; $i++) {
    for ($j = 0; $j < $w; $j++) {
      echo $result[$j][$i], "\t";
    }
    echo "<br />";
  }
}

Related recommendations:

How to operate two-dimensional arrays with PHPMatrixTranspose

PHP implements clockwise printingMatrix(spiralMatrix) method example

##PHP implements two-dimensional array Matrix Methods and cases of transposition operations

##

The above is the detailed content of Detailed graphic explanation of how to implement snake matrix, loop matrix and digital spiral matrix in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn