[algorithm] cyclic printing matrix, serpentine Matrix special topic

Source: Internet
Author: User

1. Cyclic Print matrix

For example, provide the following matrices:

Print them in the following order:

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

This question directly write also no problem, is particularly prone to error, a little attention to write wrong, and this type of question I want a universal solution.

One way I think of it is to print a lap, from outside to inside, we determine a rectangle, usually through the coordinates of the upper left corner and the coordinates of the lower right corner, namely (TR,TC) and (DR,DC), we first write the method of printing a circle, and then loop the call, If we find that the coordinates of the upper-left corner go to the right or bottom of the lower-right coordinate, the whole process stops, so the extra space complexity is O (1). No bibi,show me the code.

 PackageCom.darrenchan;/*** Circular Print Matrix *@authorThink **/ Public classSheXingPrint2 { Public Static voidMain (string[] args) {int[] m =New int[][]{{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}}; intTR = 0, TC = 0, DR = 0, DC = 3;  while(TR <= DR && TC <=DC) {Printedge (M, TR+ +, tc++, DR--, dc--); }    }         Public Static voidPrintedge (int[] m,intTrintTcintDR,intDC) {        if(TR = =DR) {             for(inti = TC; I <= DC; i++) {System.out.print (M[tr][i]+ " "); }        }Else if(TC = =DC) {             for(inti = TR; I <= DR; i++) {System.out.print (M[I][TC]+ " "); }        }Else{            intCurrow =TR; intCurcol =TC;  while(Curcol! =DC) {System.out.print (M[tr][curcol]+ " "); Curcol++; }             while(Currow! =DR) {System.out.print (M[CURROW][DC]+ " "); Currow++; }                         while(Curcol! =TC) {System.out.print (M[dr][curcol]+ " "); Curcol--; }                         while(Currow! =TR) {System.out.print (M[CURROW][TC]+ " "); Currow--; }                    }    }}
2. Serpentine Matrix

Give a number, for example, 4, to generate the following matrix directly:

The idea is similar to the previous, just slightly changed:

 PackageCom.darrenchan;/*** Print the Serpentine Matrix *@authorThink **/ Public classSheXingPrint3 { Public Static intnum = 1;  Public Static voidMain (string[] args) {//given how big the number is, how much of a matrix is constructed        int[] m =New int[4] [4]; intTR = 0, TC = 0, DR = 3, DC = 3;  while(TR <= DR && TC <=DC) {Generateedge (M, TR+ +, tc++, DR--, dc--); }                 for(inti = 0; i < m.length; i++) {             for(intj = 0; J < M[i].length; J + +) {System.out.print (M[i][j]+ "\ T");        } System.out.println (); }    }     Public Static voidGenerateedge (int[] m,intTrintTcintDR,intDC) {intCurrow =TR; intCurcol =TC;  while(Curcol! =DC) {M[tr][curcol]=num; Num++; Curcol++; }         while(Currow! =DR) {M[CURROW][DC]=num; Num++; Currow++; }         while(Curcol! =TC) {M[dr][curcol]=num; Num++; Curcol--; }         while(Currow! =TR) {M[CURROW][TC]=num; Num++; Currow--; }    }}

[algorithm] cyclic printing matrix, serpentine Matrix special topic

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.