-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpiralOdereMatrixTraversal.cpp
79 lines (66 loc) · 1.37 KB
/
SpiralOdereMatrixTraversal.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
using namespace std;
#define SIZE 30
void spirallyTraverse(int m, int n, int ar[SIZE][SIZE]);
int main() {
int T = 0;
scanf("%d",&T);
while(T--)
{
int m,n;
scanf("%d",&m);
scanf("%d",&n);
int ar[SIZE][SIZE] = {{0}};
int i = 0;
int j = 0;
int row = 0;
int col = 0;
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
scanf("%d",&ar[i][j]);
}
}
spirallyTraverse(m, n, ar);
cout<<endl;
}
return 0;
}
void spirallyTraverse(int m, int n, int ar[SIZE][SIZE]){
//Your code here
int T,R,B,L;int count = 0;
T = 0;
R = n-1;
B = m-1;
L = 0;
int dir = 0;
while(T<=B && L<=R){
if(dir == 0)
{
for(int i = L;i<=R;i++){
cout<<ar[T][i]<<" ";
}
dir = 1;T++;
}
else if(dir == 1){
for(int i = T;i<=B;i++){
cout<<ar[i][R]<<" ";
}
dir = 2;R--;
}
else if (dir == 2){
for(int i = R;i>=L;i--){
cout<<ar[B][i]<<" ";
}
dir = 3;B--;
}
else
{
for(int i = B;i>=T;i--){
cout<<ar[i][L]<<" ";
}
dir = 0;L++;
}
}
}