-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#65.cpp
75 lines (65 loc) · 1.69 KB
/
#65.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
#include <iostream>
#include <vector>
using namespace std;
void spiralPrint(vector<vector<int>> &m) {
int top = 0;
int bottom = m.size()-1;
int left = 0;
int right = m[0].size()-1;
int x = 0;
int y = 0;
bool end = false;
cout << m[y][x] << endl;
while (!end) {
for (int i = 0; i < 4; i++) {
if (i == 0) {
if (x == right) {
end = true;
break;
}
while (x < right) {
x++;
cout << m[y][x] << endl;
}
top++;
} else if (i == 1) {
if (y == bottom) {
end = true;
break;
}
while (y < bottom) {
y++;
cout << m[y][x] << endl;
}
right--;
} else if (i == 2) {
if (x == left) {
end = true;
break;
}
while (x > left) {
x--;
cout << m[y][x] << endl;
}
bottom--;
} else if (i == 3) {
if (y == top) {
end = true;
break;
}
while (y > top) {
y--;
cout << m[y][x] << endl;
}
left++;
}
}
}
}
int main() {
vector<vector<int>> v{{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16}};
spiralPrint(v);
}