What is a spiral matrix? The spiral matrix is a spiral matrix, its number from the first line began to the right to become larger, down to the big, to the left, the larger upward, so cycle. The following figure is an example of a helical matrix: Zhengxiao uses Python and PHP to implement the spiral matrix of numbers, respectively. PHP version of which is written as a function, more flexible control of the matrix. When writing python, because it is the first time to write, some unfamiliar, respectively, using two methods to achieve ... Here's the code: the first version of the Python Spiral matrix (more difficult to understand?!) : #coding: GBK l = 6 #矩阵大小 Result = [[0]*l for n in range (L)] Row =0 #初始行 starting from top left corner col = 0 #初始列 Start value = 1# Initial value direction = ' R ' #初始方向 right circle = 1 #初始圈数 from upper left corner First lap While true: #向右走 if direction == ' R ': result[row][col] = value if col>=l-circle: direction = ' d ' continue col += 1 # Go down if direction == ' d ': result[row][col] = value if row >= l-circle: direction = ' l ' &Nbsp; continue
row += 1 #向左走 if direction == ' l ': result[row][col] = value if col <= circle -1: direction = ' u ' continue &nBsp; col -= 1 #向上走 if direction == ' u ': result[row][col] = value if row-1 <= circle: direction = ' R ' circle += 1 #continue &Nbsp; row -= 1 value += 1 if value > l &NBSP;*&NBSP;L&NBSP: break For r in result: for c in r: print "%3d" % (c) , print raw_input ()
is followed by the second edition of the Python Spiral Matrix, which uses the iterator control direction, the amount of code is significantly reduced, and is easier to understand. When you use a matrix generated from a different starting point, you will find a place where there is a "problem", do you have a problem?
#coding: GBK import itertools #参数: Matrix width (w), High (h), starting horizontal axis, starting ordinate Def print_matrix (w, h, x = 0, y = 0): #方向移动时的操作 op = [(1,0), ( -1,0), (0,-1), (0,1)] #迭代器可无限迭代列表 Next () every time you change direction direction = itertools.cycle (OP) #根据行列生成所有坐标 result = {(XX,YY): None for xx in range (W) for yy in Range (h)} result[(x,y)] = 1 _x, _y = Direction.next () i = 1 flag = 0 while true: new_ X = x + _x new_y = y&nBsp;+ _y if (new_x, new_ Y) in result and result[(new_x, new_y)] is none: i = i + 1 result[(new_x, new_y)] = i x = new_x y = new_y flag = 0 else: _x, _y =&nBsp;direction.next () flag = flag + 1 if flag>4: break #打印结果 for y in range (h): for x in range (w):
print "%3d" % (result[(x,y)), print #调用示例 Print_matrix (6,6,5,0) raw_input ()
The following is the PHP version of the spiral matrix, the idea is the same as the python above (in fact, I wrote ...).
/* * * @param $w : wide * @param $h : high * @param $s : starting number * @param $x, $y : start position coordinates only starting at four vertices * @
param $r  : direction Default Shun time false to counterclockwise * @author : Zhengxiao * php5.6.11 * This is a PHP version of the Spiral Matrix, which has been written before in Python and has some experience, so it also uses SPL's iterator in PHP for reversing. The algorithm is written using the procedure, and then changed to a function, added some custom parameters, easy to invoke (but also the eggs).
The output uses a tab and line break, and you can view the results in your browser's source code. */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)); // Creates an infinite iterator $iterator->rewind (); //pointer points to the first element 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--;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP}  } //below is the print matrix structure for ($i =0; $i < $h; $i + +) { for ($j =0; $j < $w; $j + +) {
echo $result [$j] [$i], "T"; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP} echo \ n
"; &NBSP;&NBSP;&NBSP;&NBSP}//Call sample Test Print_matrix (5, 5); echo "\ n";
Print_matrix (7, 4); echo "\ n";
Print_matrix (5, 5, 1, 4, 0); echo "\ n"; Print_matrix (5, 5, 10, 0, 4, false); echo "\ n";
The following is the result of running sequentially: