Python/PHP code for the digital spiral matrix-PHP source code

Source: Internet
Author: User
In php or python, the matrix is just an algorithm. What we need to do is to use the program we have learned to implement this algorithm to meet our needs, the following describes their implementation methods. In php or python, the matrix is just an algorithm. What we need to do is to use the program we have learned to implement this algorithm to meet our needs, the following describes their implementation methods.

Script ec (2); script

What is a spiral matrix? A spiral matrix refers to a spiral matrix. Its numbers grow from the first row to the right, and become larger downward, larger to the left, and larger up. Here is an example of a spiral matrix: Zheng Xiao uses Python and PHP respectively to implement a spiral matrix of numbers. The PHP version is written as a function to control the matrix more flexibly. It was the first time to write Python, but it was a little unfamiliar. We used two methods to implement it... The code below: the first version of the Python spiral matrix (difficult to understand ?!): # Coding: gbkL = 6 # matrix size result = [[0] * L for n in range (L)] row = 0 # The initial row starts from the upper left corner col = 0 # The initial column starts from the upper left corner value = 1 # The initial value direction = 'R' # The initial direction is to the right circle = 1 # The first circle of the initial number of circles while True: # if direction = 'r': result [row] [col] = value if col> = L-circle: direction = 'D' continue col + = 1 # go down to if direction = 'D': result [row] [col] = value if row> = L-circle: direction = 'l' continue row + = 1 # To the left, if direction = 'l': result [row] [col] = value if col <= circle-1: direction = 'U' continue col-= 1 # go up if direction = 'U': result [row] [col] = value if row-1 <= circle: direction = 'R' circle + = 1 # continue row-= 1 value + = 1 if value> L * L: breakfor r in result: for c in R: print "% 3d" % (c), printraw_input ()


Next is the second version of the Python spiral matrix, where the iterator is used to control the direction, the amount of code is significantly reduced, and it is easier to understand. When you use a matrix generated from a different starting point multiple times, you will find that there is a bit of a "problem" written in the place. Have you found the problem?

# Coding: gbkimport itertools # parameters: matrix width (w), height (h), start abscissa, start ordinate def print_matrix (w, h, x = 0, y = 0 ): # operation op = [(), (-), (0,-1), ()] # The iterator can iterate the list infinitely. next () ction = itertools is required each time you change the direction. cycle (op) # generate all coordinates 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 + _ 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 = direction. next () flag = flag + 1 if flag> 4: break # print the result for y in range (h): for x in range (w ): print "% 3d" % (result [(x, y)]), print # Call the sample 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 above Python (in fact, I write according ...).

/*** @ Param $ w: width * @ param $ h: height * @ param $ s: Start Number * @ param $ x, $ y: the starting position coordinate can only start from four vertices * @ param $ r: the default direction shun time false is counterclockwise * @ author: Zheng Xiao * php5.6.11 * This is a PHP version of the spiral matrix, since I have previously written it in python and have some experience, I also used an SPL iterator in php for reversing. The algorithm was written in a procedural manner, and later changed to a function. Some user-defined parameters were added for easy calling ). Tabulation and line feed are used in the output. view the running result in the source code of the browser. */Function print_matrix ($ w, $ h, $ s = 1, $ l = 1, $ x = 0, $ y = 0, $ r = true) {$ R = array (), array (), array (-), array (0,-1 ));! $ R & $ R = array_reverse ($ R); $ iterator = new InfiniteIterator (new ArrayIterator ($ R )); // create an infinite iterator $ iterator-> rewind (); // The 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 -- ;}/// The following is the print matrix structure for ($ I = 0; $ I <$ h; $ I ++) {for ($ j = 0; $ j <$ w; $ j ++) {echo $ result [$ j] [$ I], "\ t" ;}echo "\ n ";}} // call the sample code to 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 are the running results in sequence:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.