Use JavaScript to implement a spiral Matrix Algorithm

Source: Internet
Author: User

Spiral matrices need to be used in actual developmentAlgorithm, Using JSCodeImplemented:

VaR Cal = function (LEN) {var helix = [[], [], [], [], [], []; var min = 0; vaR max = len-1; var ROW = 0; var Col = 0; For (VAR I = 0; I <Len * Len; I ++) {helix [row] [col] = I + 1; if (ROW = min & Col <max) {Col = Col + 1 ;} else if (row <Max & Col = max) {ROW = row + 1;} else if (ROW = max & Col> min) {Col = col-1;} else if (row> min & Col = min) {ROW = row-1 ;} if (Row-1 = min & Col = min) {min = min + 1; max = max-1 ;}} return helix ;}; vaR helix = CAL (6); var html = ''; For (VAR I = 0; I  

Algorithm Analysis: First traverse the four outermost edges and then traverse all the edges in them. The traversal algorithm is used to place the edges of values one by one at the specified position.

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.