JavaScript fun: Spiral Matrix

Source: Internet
Author: User
Tags types of functions
Given an n * n two-dimensional array, use the spiral matrix algorithm to traverse it and return the path. Example: A two-dimensional array of n * n is given. The spiral matrix algorithm is used to traverse the array and return the path.

Example:

array = [[1,2,3],        [4,5,6],        [7,8,9]]  snail(array) // => [1,2,3,6,9,8,7,4,5]

The example of this program may not be intuitive, so the previous figure shows the process.

As you can see, it is like a person moving forward in the maze. It is located at () first, and then goes to the right. when it encounters a boundary, it goes down and encounters a boundary, move to the left ....

Careful people will soon find a rule:

This person in the Maze has four basic travel strategies.

1. When you go to the right, if you encounter a boundary, or the grid on the right has already passed, go down. Otherwise, continue to the right.

2. When you go down, if you encounter a boundary, or the grid below has already passed, you will go to the left. Otherwise, you will continue to go down.

3. When you move to the left, if you encounter a boundary, or the grid on the left has already passed, go up. Otherwise, continue to the left.

4. When you go up, if you encounter a boundary, or the grid above has already passed, you will go to the right. Otherwise, you will continue to go up.

As you can see, this is a recursive process. When will it end?

When this person has accessed each grid, it will be terminated.

The program I wrote below uses a boolean two-dimensional array to record whether the lattice has passed.

Four types of functions are introduced, which indicate four policies.

Function snail il (array) {// current row var row = 0; // current column var col = 0; // the corresponding two-dimensional array that stores boolean values var hasVisited = []; // array for storing results var result = []; // Number of array elements var size = array. length * array [0]. length; // boolean two-dimensional array initialization for (var I = 0; I <array. length; I ++) {var temp = []; var len = array [I]. length; for (var j = 0; j <len; j ++) {temp. push (false);} hasVisited. push (temp);} function right () {if (result. length <size) {result. push (array [row] [col]); hasVisited [row] [col] = true; if (col + 1> = array. length | hasVisited [row] [col + 1]) {row + = 1; down ();} else {col + = 1; right ();}}} function down () {if (result. length <size) {result. push (array [row] [col]); hasVisited [row] [col] = true; if (row + 1> = array. length | hasVisited [row + 1] [col]) {col-= 1; left () ;}else {row + = 1; down ();}}} function left () {if (result. length <size) {result. push (array [row] [col]); hasVisited [row] [col] = true; if (col = 0 | hasVisited [row] [col-1]) {row-= 1; up () ;}else {col-= 1; left () ;}} function up () {if (result. length <size) {result. push (array [row] [col]); hasVisited [row] [col] = true; if (hasVisited [row-1] [col]) {col + = 1; right () ;}else {row-= 1; up () ;}}// first right (); return result ;}

The above is the JavaScript interesting question: the content of the spiral matrix. For more information, see the PHP Chinese website (www.php1.cn )!

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.