forked from abbh07/DataStructure-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArraySpiralPrint.cpp
67 lines (64 loc) · 934 Bytes
/
ArraySpiralPrint.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
#include <iostream>
#include <vector>
using namespace std;
void print_spiral(vector< vector<int> > v, int m, int n)
{
int top = 0, bottom = m-1;
int left = 0, right = n-1;
int dir = 0;
while(top<=bottom && left<=right)
{
if(dir == 0)
{
for(int i=left;i<=right;i++)
{
cout<<v[top][i]<<" ";
}
top++;
dir = 1;
}
else if(dir == 1)
{
for(int i=top;i<=bottom;i++)
{
cout<<v[i][right]<<" ";
}
right--;
dir = 2;
}
else if(dir == 2)
{
for(int i=right;i>=left;i--)
{
cout<<v[bottom][i]<<" ";
}
bottom--;
dir = 3;
}
else if(dir == 3)
{
for(int i = bottom;i>=top;i--)
{
cout<<v[i][left]<<" ";
}
left++;
dir = 0;
}
}
cout<<endl;
}
int main()
{
int m,n;//rows,column
cin>>m>>n;
vector < vector<int> > v(m,vector<int> (n) );
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cin>>v[i][j];
}
}
print_spiral(v,m,n);
return 0;
}