Skip to content

Commit 63664b8

Browse files
authored
Merge pull request #233 from Shwetank14/shwetank14
Spiral Order traversal of Matrix
2 parents b766039 + 15feade commit 63664b8

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

SpiralOdereMatrixTraversal.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
#define SIZE 30
5+
6+
void spirallyTraverse(int m, int n, int ar[SIZE][SIZE]);
7+
8+
int main() {
9+
int T = 0;
10+
11+
scanf("%d",&T);
12+
13+
while(T--)
14+
{
15+
int m,n;
16+
scanf("%d",&m);
17+
scanf("%d",&n);
18+
int ar[SIZE][SIZE] = {{0}};
19+
int i = 0;
20+
int j = 0;
21+
int row = 0;
22+
int col = 0;
23+
24+
for(i=0; i<m; i++)
25+
{
26+
for(j=0; j<n; j++)
27+
{
28+
scanf("%d",&ar[i][j]);
29+
}
30+
}
31+
32+
spirallyTraverse(m, n, ar);
33+
cout<<endl;
34+
35+
}
36+
return 0;
37+
}
38+
void spirallyTraverse(int m, int n, int ar[SIZE][SIZE]){
39+
//Your code here
40+
int T,R,B,L;int count = 0;
41+
T = 0;
42+
R = n-1;
43+
B = m-1;
44+
L = 0;
45+
int dir = 0;
46+
while(T<=B && L<=R){
47+
if(dir == 0)
48+
{
49+
for(int i = L;i<=R;i++){
50+
cout<<ar[T][i]<<" ";
51+
}
52+
dir = 1;T++;
53+
}
54+
else if(dir == 1){
55+
for(int i = T;i<=B;i++){
56+
cout<<ar[i][R]<<" ";
57+
}
58+
dir = 2;R--;
59+
}
60+
else if (dir == 2){
61+
for(int i = R;i>=L;i--){
62+
cout<<ar[B][i]<<" ";
63+
}
64+
dir = 3;B--;
65+
}
66+
else
67+
{
68+
for(int i = B;i>=T;i--){
69+
cout<<ar[i][L]<<" ";
70+
}
71+
dir = 0;L++;
72+
}
73+
74+
75+
}
76+
}
77+
78+
79+

0 commit comments

Comments
 (0)