-
Notifications
You must be signed in to change notification settings - Fork 255
/
Spiral_Matrix.cpp
50 lines (46 loc) · 959 Bytes
/
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
#include <iostream>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
int a[n][m];
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>a[i][j];
}
}
int row_start=0,col_start=0,row_end=n-1,col_end=m-1;
while(row_start<=row_end && col_start<=col_end)
{
for(int col=col_start;col<=col_end;col++)
{
cout<<a[row_start][col]<<" ";
}
row_start++;
for(int row=row_start;row<=row_end;row++)
{
cout<<a[row][col_end]<<" ";
}
col_end--;
if(row_start<=row_end)
{
for(int col=col_end;col>=col_start;col--)
{
cout<<a[row_end][col]<<" ";
}
}
row_end--;
if(col_start<=col_end)
{
for(int row=row_end;row>=row_start;row--)
{
cout<<a[row][col_start]<<" ";
}
}
col_start++;
}
return 0;
}