-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspiralmatrix.cpp
More file actions
65 lines (54 loc) · 1.24 KB
/
spiralmatrix.cpp
File metadata and controls
65 lines (54 loc) · 1.24 KB
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
// Print a 2D array spirally
#include <iostream>
using namespace std;
void spiral(int **a, int n, int m)
{
int top = 0;
int left = 0;
int down = n - 1;
int right = m - 1;
int dir = 0;
while (top <= down && left <= right)
{
if (dir == 0)
{
for (int i = left; i <= right; i++)
cout << a[top][i] << " ";
top++;
}
else if (dir == 1)
{
for (int i = top; i <= down; i++)
cout << a[i][right] << " ";
right--;
}
else if (dir == 2)
{
for (int i = right; i >= left; i--)
cout << a[down][i] << " ";
down--;
}
else if (dir == 3)
{
for (int i = down; i >= top; i--)
cout << a[i][left] << " ";
left++;
}
dir = (dir + 1) % 4;
}
}
int main()
{
int n, m; // n = rows, m = columns
cin >> n >> m;
int **a = new int *[n];
for (int i = 0; i < n; i++)
a[i] = new int[m];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> a[i][j];
spiral(a, n, m);
for (int i = 0; i < n; i++)
delete[] a[i];
delete[] a;
}