[Algorithm 06] clockwise print matrix

Source: Internet
Author: User

Question: Given a matrix, print every number in the matrix clockwise from the external to the inner.

For example, the given matrix:

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

The output should be}

 

Analysis: the meaning of this question is very intuitive, and it gives people the feeling of so easy. However, when we do it, we will find that if the structure is not well divided, there will be many cycles, it also includes the determination of various boundary conditions. The question is not difficult, but it gives people the feeling-it's annoying! I will share my thoughts on this question.

How can we determine the cyclic conditions? A matrix, given the start point (startx, starty) and end point (endx, Endy) (that is, the two points on the diagonal line) can print a week, then, let's go to the plus Week (++ startx, ++ starty, -- endx, -- Endy). If the start point coordinate is <End Point Coordinate (that is, startx <endx or starty <Endy) the loop ends.

For a given start point and end point, what are the conditions for printing the end of a week? I drew a matrix of 4x4, 3x5, and 5x3 in three cases (all matrices are only of these three types, square, and "fat, thin), and soon found that there are only three situations: until the end of the loop, only one row is left, and only one column is left. Therefore, our function first determines that there is only one row? Print the row. Only one column is printed. No. Print the numbers on the four sides.

So far: we can write the completeCodeAs follows:

1 # Include <iostream>
2 # Include < String >
3 Using Namespace STD;
4
5 Void Printmatrixcircle ( Int ** Num, Int SX, Int Sy, Int Ex, Int Ey );
6
7 // For a given matrix, a row or column is specified, and numbers are printed clockwise from the outer to the inner.
8 Void Printmatrixclockwisely ( Int ** Matrix, Int Rows, Int Columns)
9 {
10 If (Matrix = NULL | rows < 0 | Columns < 0 )
11 Return ;
12
13 Int Startx = 0 ;
14 Int Starty = 0 ;
15 Int Endx = rows- 1 ;
16 Int Endy = columns- 1 ;
17 While ( 1 )
18 {
19 If (Startx> endx & starty> Endy)
20 Break ;
21 If (Startx = endx & starty> Endy)
22 Break ;
23 If (Startx> endx & starty = Endy)
24 Break ;
25
26 Printmatrixcircle (matrix, startx, starty, endx, Endy );
27
28 ++ Startx;
29 ++ Starty;
30 -- Endx;
31 -- Endy;
32 }
33 }
34
35 // For a given matrix, the elements of the week are printed at two points on the diagonal line.
36 Void Printmatrixcircle ( Int ** Num, Int SX, Int Sy, Int Ex, Int Ey)
37 {
38 // If there is only one row, print it directly and return it.
39 If (SX = ex)
40 {
41 For ( Int J = sy; j <= ey; ++ J)
42 {
43 Cout <* (Num + SX) + J) < " \ T " ;
44 }
45
46 Return ;
47 }
48 // If there is only one column, print it directly and return it.
49 If (SY = ey)
50 {
51 For ( Int I = SX; I <= ex; ++ I)
52 {
53 Cout <* (Num + I) + Sy) < " \ T " ;
54 }
55 Return ;
56 }
57
58 // Print four rows in general.
59 For ( Int P = sy; P <ey; ++ P)
60 {
61 Cout <* (Num + SX) + p) < " \ T " ;
62 }
63
64 For ( Int Q = SX; q <ex; ++ q)
65 {
66 Cout <* (Num + q) + ey) < " \ T " ;
67 }
68
69 For ( Int M = ey; m> sy; -- m)
70 {
71 Cout <* (Num + ex) + M) < " \ T " ;
72 }
73
74 For ( Int N = ex; n> SX; -- N)
75 {
76 Cout <* (Num + n) + Sy) < " \ T " ;
77 }
78
79 }
80
81 Int Main ()
82 {
83 Int Example [ 6 ] [ 6 ];
84 Int * Point1 [ 6 ] = {Example [ 0 ], Example [ 1 ], Example [ 2 ], Example [ 3 ], Example [ 4 ], Example [ 5 ]};
85 Int ** Point = point1;
86 Int CNT = 1 ;
87
88 // Initialization
89 For ( Int I = 0 ; I < 6 ; I ++)
90 {
91 For ( Int J =0 ; J < 6 ; J ++)
92 {
93 Example [I] [J] = CNT;
94 CNT ++;
95 }
96 }
97
98 Cout < " The original matrix is: " <Endl;
99 For (I = 0 ; I < 6 ; I ++)
100 {
101 For ( Int J = 0 ; J < 6 ; J ++)
102 {
103 Cout <example [I] [J] < " \ T " ;
104 }
105 Cout <Endl;
106 }
107
108 Cout < " The clockwisely output of the matrix is: " <Endl;
109
110 Printmatrixclockwisely (point, 6 , 6 );
111 Cout <Endl;
112
113 Return 0 ;
114 }

The running result is as follows:

This is a square matrix. I have also tested the "fat" Matrix and thin matrix. To test this, you need to modify the matrix and pointer of the main function;

My test cases and test results are as follows:

"Fat" Matrix test:

 

"Thin" Matrix test:

 

Source:

He Haitao blog: http://zhedahht.blog.163.com/blog/static/254111742010111112236313/


Note:

1) Compile all the code environments in this blogWin7 + vc6. All code has been debugged by the blogger.

2) BloggerPython27This blogArticleCopyright. For Network reprinting, please indicate the sourceHttp://www.cnblogs.com/python27/. You have any suggestions for solving the problem. Please feel free to comment on them.

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.