-
Notifications
You must be signed in to change notification settings - Fork 0
/
spiral_matrix_IV.cpp
62 lines (59 loc) · 1.47 KB
/
spiral_matrix_IV.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
#include <vector>
#include <iostream>
using namespace std;
#define LEFT 98
#define RIGHT 99
#define UP 100
#define DOWN 101
struct ListNode {
int val;
ListNode* next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode* next) : val(x), next(next) {}
};
vector<vector<int>> spiralMatrix(int m, int n, ListNode* head) {
vector<vector<int>> map(m);
for(auto& ele : map) {
ele.resize(n);
for(auto& e : ele) {
e = -1;
}
}
int x = 0, y = 0;
int direction = RIGHT;
while(head != NULL) {
map[x][y] = head->val;
head = head->next;
if(direction == RIGHT) {
if(y + 1 >= n || map[x][y + 1] != -1) {
direction = DOWN;
++x;
continue;
}
++y;
} else if(direction == DOWN) {
if(x + 1 >= m || map[x + 1][y] != -1) {
direction = LEFT;
--y;
continue;
}
++x;
} else if(direction == LEFT) {
if(y - 1 < 0 || map[x][y - 1] != -1) {
direction = UP;
--x;
continue;
}
--y;
} else if(direction == UP) {
if(x - 1 < 0 || map[x - 1][y] != -1) {
direction = RIGHT;
++y;
continue;
}
--x;
}
}
return map;
}