-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathSpiral Code
59 lines (49 loc) · 1.15 KB
/
Spiral Code
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
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
vector<int> spiralOrder(vector<vector<int> >& matrix)
{
int m = matrix.size(), n = matrix[0].size();
vector<int> ans;
if (m == 0)
return ans;
vector<vector<bool> > seen(m, vector<bool>(n, false));
int dr[] = { 0, 1, 0, -1 };
int dc[] = { 1, 0, -1, 0 };
int x = 0, y = 0, di = 0;
// Iterate from 0 to m * n - 1
for (int i = 0; i < m * n; i++) {
ans.push_back(matrix[x][y]);
// on normal geeksforgeeks ui page it is showing
// 'ans.push_back(matrix[x])' which gets copied as
// this only and gives error on compilation,
seen[x][y] = true;
int newX = x + dr[di];
int newY = y + dc[di];
if (0 <= newX && newX < m && 0 <= newY && newY < n
&& !seen[newX][newY]) {
x = newX;
y = newY;
}
else {
di = (di + 1) % 4;
x += dr[di];
y += dc[di];
}
}
return ans;
}
// Driver code
int main()
{
vector<vector<int> > a{ { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
// Function call
for (int x : spiralOrder(a)) {
cout << x << " ";
}
return 0;
}
// This code is contributed by Yashvendra Singh