Implementation with C + +: loop data retrieval

Keywords: C++

Problem description
Loop fetching is fetching along the edge of the matrix. If there are countless or have been fetched in the current direction, turn 90 degrees to the left. It starts at the top left corner of the matrix and goes down.
Input format
The first input row is two positive integers m, n, representing the rows and columns of the matrix. Next, m lines, n integers per line, represent the matrix.
Output format
The output has only one line and mn number in total, which is the result of the input matrix loop. Numbers are separated by a space. There should be no extra space at the end of the line.
sample input
3 3
1 2 3
4 5 6
7 8 9
sample output
1 4 7 8 9 6 3 2 5
sample input
3 2
1 2
3 4
5 6
sample output
1 3 5 6 4 2
 
Thinking: according to the law of the number of loops, every four actions are a big cycle, that is, first go down, then go right, then go up, and finally go left. So a big cycle with four small cycles can be solved.
 1 #include<iostream>
 2 using namespace std;
 3 int main(void)
 4 {
 5     int m, n;
 6     int a, b, c, d, i, j, k, t, h, v, l, u;
 7     i = 0;
 8     k = 0;
 9     h = 0;
10     v = 1;
11     l = 0;
12     u = 0;
13     cin >> m >> n;
14     j = m * n;
15     int arr[m][n];
16     int brr[j];
17     for (int x = 0; x < m; x++)
18     {
19         for (int y = 0; y < n; y++)
20         {
21             cin >> arr[x][y];
22         }
23     }
24     for (t = 0; t < j;)   //Count, there are m*n Number, to traverse m*n second
25     {
26         for (a = h; a < m; a++)  //Let's go down and notice this a<m m In fact, it's not the same every time, because it's not the same every time
27         {
28             brr[t] = arr[a][i];
29             t++;
30             if (t == j)  //Pay attention to jump out of the whole cycle in time when reaching the critical point
31             {
32                 break;
33             }
34         }
35         for (b = v; b < n; b++)  //Go to the right again. Pay attention to this b<n n Actually, it's different every time, because it's not always possible to go to the far right
36         {
37             brr[t] = arr[m - 1][b];
38             t++;
39             if (t == j)
40             {
41                 break;
42             }
43         }
44         for (c = m - 2; c >= l; c--)  //Go up and don't write c>=0 It's not always down to the first line
45         {
46             brr[t] = arr[c][n - 1];
47             t++;
48             if (t == j)
49             {
50                 break;
51             }
52         }
53         for (d = n - 2; d > u; d--)  //Go left no more d>0 It's not always down to the first column
54         {
55             brr[t] = arr[k][d];
56             t++;
57             if (t == j)
58             {
59                 break;
60             }
61         }
62         m--;
63         n--;
64         i++;
65         k++;
66         h++;
67         v++;
68         l++;
69         u++;
70     }
71     for (int q = 0; q < j - 1; q++)
72     {
73         cout << brr[q] << " ";
74     }
75     cout << brr[j - 1];
76     return 0;
77 }

Note: (1) I used many variables in this code, so I must not confuse the relationship and function of each variable

(2) when t=m*n, jump out of the loop in time, otherwise the array will be out of bounds
(3) pay attention to the judgment conditions at the end of each small cycle, because without a large cycle, the judgment conditions will change
 
Take another look at a relatively simple code I found on the Internet:
 1 #include<iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int m = 0, n = 0;
 6     cin >> m >> n;
 7     int a[m][n];
 8     for (int i = 0; i < m; i++)
 9         for (int j = 0; j < n; j++)
10             cin >> a[i][j];
11     int circle = 0, count = 0; //Number of initialization cycles, output count 
12     while (count < m * n)
13     {
14         for (int i = circle; i < m - circle && count < m * n; i++) //The first edge (Left)
15         {
16             cout << a[i][circle] << " ";
17             count += 1;
18         }
19         for (int j = circle + 1; j < n - circle && count < m * n; j++) //Second side (bottom)
20         {
21             cout << a[m - 1 - circle][j] << " ";
22             count += 1;
23         }
24         for (int k = m - 2 - circle; k >= circle && count < m * n; k--) //Third side (right)
25         {
26             cout << a[k][n - 1 - circle] << " ";
27             count += 1;
28         }
29         for (int h = n - 2 - circle; h >= 1 + circle && count < m * n; h--) //Fourth side (top)
30         {
31             cout << a[circle][h] << " ";
32             count += 1;
33         }
34         circle += 1;    //Cycle times plus 1 
35     }
36     return 0;
37 }

Original link: https://blog.csdn.net/wjinjie/article/details/104954349

Posted by peppino on Wed, 25 Mar 2020 08:00:10 -0700