"ACM" Prints The matrix clockwise

Source: Internet
Author: User

Problem Description:
Enter a matrix that prints each number in a clockwise order from the outside,
For example, if you enter the following matrix: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
The word 1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10 is printed in turn.

Algorithm Description:
The (x, y) tuple as a pointer to the currently printed element, with the current position plus -1,0,1 respectively
Indicates that the x, Y coordinates are back, remain unchanged, and advance. such as Matrix
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

If the current position is (0,0), print 1;
When printing clockwise, move to the next position is x+1,y+0, therefore (1,0), that is, printing 2;
Further down, (2,0), print 3;
......
The last element is (10), printing.
First, the detection is a single row, if it is only one for the loop to resolve, otherwise go to the next step:
Starting with the coordinates (0,0), x increments sequentially, y is unchanged, and the maximum minimum value of x, y X_min,x_max and Y_min.y_max are recorded, and then the coordinates are moved;
When moved to the boundary position (x_max,y_min), indicates that the first line has been printed, so x_min+=1, and X will remain unchanged, y increments;
When moved to the boundary position (X_max,y_max), indicates that the last column has been printed, so y_max-=1, while Y will remain unchanged, x decrements;
When moved to the boundary position (X_min,y_max), indicates that the last line has been printed, so x_max-=1, and X will remain unchanged, y upward decrement;
When moving to the boundary position (x_min,y_min), indicating that the first column has been printed, so y_min-=1, and y will remain unchanged, X forward, it should be noted that this step must have been a clockwise loop before detection.
At the time of encoding, the changes of Min and Max need to be moved to the next boundary point in order to proceed, otherwise you will fall into a dead loop resulting in an array overflow (see Code).

The code is as follows:

classSolution { Public: vector<int>Printmatrix ( vector<vector<int> >Matrix) { vector<int>Resultsif(matrix.size () = =0) {returnResults }intx =0;inty =0;//position of the current element        intXF =0;intYF =1;//xf and YF represent the direction of the current element position pointer, -1 table back, 0 stop, 1 forward        introws = Matrix.size ()-1;//Line        intcols = matrix.at (0). Size ()-1;//Column        //When there is only one row        if(Rows = =0) { for(inti =0; I <= cols; i++) {Results.push_back (matrix.at (0). at (i)); }returnResults }//When there is only one column        if(cols = =0) { for(inti =0; I <= rows; i++) {Results.push_back (matrix.at (i). at (0)); }returnResults }intX_start =0;intY_start =0;intX_end = rows;intY_end = cols;BOOLIsFirst =true;intSize = (rows+1) * (cols+1);//Number of elements         while(Results.size () < size) {Results.push_back (matrix[x][y]);//Record current elementx + = XF;//Horizontal shiftY + = YF;//Ordinate change            //Start making four-corner judgments and related operations            if(x = = X_start && y = = y_end) {XF =1; YF =0;if(!isfirst) {Y_start + =1; }            }if(x = = X_end && y = = y_end) {XF =0; YF =-1; X_start + =1; }if(x = = X_end && y = = Y_start) {XF =-1; YF =0; Y_end-=1; }if(x = = X_start && y = = Y_start) {XF =0; YF =1; X_end-=1; IsFirst =false; }        }returnResults }};

Test code:

intMain () { vector<vector<int> >Matrixintx =1; for(inti =0; I <4; i++) { vector<int>tmp for(intj =0; J <1;        J + +) {Tmp.push_back (x + +);    } matrix.push_back (TMP); } solution S; vector<int>Re = S.printmatrix (matrix); for(inti =0; I < re.size (); i++) {cout<< Re[i] <<" "; }}

"ACM" Prints The matrix clockwise

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.