-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspiral_print.cpp
65 lines (51 loc) · 1.19 KB
/
spiral_print.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
traverse the array along the border and keep decreasing the length.
input:
1 2 3 4 5
14 15 16 17 6
13 20 19 18 7
12 11 10 9 8
output:
1 2 3 ...... 18 19 20
*/
#include <iostream>
using namespace std;
void spiralPrint(int *arr[], int m, int n) {
int sr = 0, sc = 0;
int er = m - 1, ec = n - 1;
while(sr <= er and sc <= ec) {
// start row
for(int j = sc; j <= ec; j++)
cout << arr[sr][j] << " ";
sr++;
// end column
for(int i = sr; i <= er; i++)
cout << arr[i][ec] << " ";
ec--;
// end row
if(sr < er) {
for(int j = ec; j >= sc; j--)
cout << arr[er][j] << " ";
er--;
}
// start column
if(sc < ec) {
for(int i = er; i >= sr; i--)
cout << arr[i][sc] << " ";
sc++;
}
}
}
int main() {
int m, n;
cin >> m >> n;
int *arr[m];
for(int i = 0; i < n; i++)
arr[i] = new int[n];
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++)
cin >> arr[i][j];
}
spiralPrint(arr, m, n);
return 0;
}