Print a serpentine matrix

Source: Internet
Author: User

Problem Description: Given an integer matrix, the integer matrix is printed clockwise,

For example, the matrix is 1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

The clockwise output is: 1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10

Analysis: This problem actually does not involve the complex algorithm and the data structure question, also does not have the simple method to do, can only draw to find each cycle boundary to control,

By drawing you can see that the first element of the loop can be used as our benchmark element, and this element will be well-established. Can draw a regular, every time

The Datum element position is the position of the previous Datum element two subscript plus 1, the execution condition of the loop is that the number of rows and columns is required to be greater than the datum

Twice times the subscript of the element.

The specific Java code below, the wording is more general, the reader can easily convert to other language implementation.

1  Public classMain {2      Public Static voidOrderprint (inta[][]) {3         if(a==NULL){4SYSTEM.OUT.PRINTLN ("Array does not exist");5             return ;6         }7         intRow=a.length,column=a[0].length;8         if(Row==0 | | column==0){9SYSTEM.OUT.PRINTLN ("Array is an empty array");Ten             return ; One         } A         intStartx=0,starty=0, Endx,endy; -         inti,j; -          while(Row>starty*2 && column>startx*2) the         {    -Endx=column-1-startx;//calculates the column boundaries for each lap -Endy=row-1-starty;//calculates the row boundaries for each lap -              for(j=startx;j<=endx;j++)//upper left-to-right printing +System.out.print (a[starty][j]+ ","); -              for(i=starty+1;i<=endy;i++)//print from top to bottom on right +System.out.print (a[i][endx]+ ","); A              for(j=endx-1;j>=startx;j--)//lower right-to-left printing atSystem.out.print (a[endy][j]+ ","); -              for(i=endy-1;i>starty;i--)//left to print from bottom to top -System.out.print (a[i][startx]+ ","); -startx++;starty++;//change the position of the first number of each lap -         } -     } in      Public Static voidMain (string[] args) { -         //TODO Auto-generated method stubs to       inta[][]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}}; + Orderprint (a); -     } the  *}

The output is:

1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10,

Print a serpentine matrix

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.