-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathspiral_matrix.cpp
63 lines (61 loc) · 1.05 KB
/
spiral_matrix.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
#include<bits/stdc++.h>
#include<conio.h>
using namespace std;
#define M 4
#define N 4
void displayMatrix(int matrix[][N])
{
for(int i=0; i<M; i++)
{
for(int j=0; j<N; j++)
{
cout<<matrix[i][j]<<" ";
}
cout<<endl;
}
}
void SpiralMatrix(int matrix[][N])
{
int top = 0; // Rows traversing
int bottom = M-1; // starting row to last row
int left = 0; // starting column
int right = N-1; // last column
while(top<=bottom && left<=right)
{
for(int i = left; i<=right; i++)
{
cout<<matrix[top][i]<<" ";
}
top++;
for(int j = top; j<=bottom; j++)
{
cout<<matrix[j][right]<<" ";
}
right--;
if(bottom>=top) // edge cases handle
{
for(int k = right; k>=left; k--)
{
cout<<matrix[bottom][k]<<" ";
}
bottom--;
}
if(right>=left)
{
for(int l = bottom; l>=top; l--)
{
cout<<matrix[l][left]<<" ";
}
left++;
}
}
}
int main()
{
int matrix[M][N]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
displayMatrix(matrix);
cout<<"After spirally tranversing: "<<endl;
SpiralMatrix(matrix);
getch();
return 0;
}