forked from rost0413/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpiral_Matrix_II.cpp
85 lines (75 loc) · 1.52 KB
/
Spiral_Matrix_II.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
76
77
78
79
80
81
82
83
84
85
/*
Author: Weixian Zhou, [email protected]
Date: Jul 23, 2012
Problem: Spiral Matrix II
Difficulty: easy
Source: http://www.leetcode.com/onlinejudge
Notes:
Given an integer n, generate a square matrix filled with elements from 1 to
n2 in spiral order.
For example,
Given n = 3,
You should return the following matrix:
[
[ 1, 2, 3 ],
[ 8, 9, 4 ],
[ 7, 6, 5 ]
]
Solution:
*/
#include <vector>
#include <set>
#include <climits>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
using namespace std;
class Solution {
public:
void nextStep(int dir, int x, int y, int &nx, int &ny) {
nx = x;
ny = y;
if (dir == 0) {
ny++;
} else if (dir == 1) {
nx++;
} else if (dir == 2) {
ny--;
} else {
nx--;
}
}
vector<vector<int> > generateMatrix(int n) {
int x = 0, y = 0, dir = 0, nx, ny;
int count = 1;
vector<vector<int> > result;
int matrix[n][n];
if (n == 0) {
return result;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] = 0;
}
}
while (true) {
matrix[x][y] = count++;
nextStep(dir, x, y, nx, ny);
if (!(0 <= nx && nx < n && 0 <= ny && ny < n && !matrix[nx][ny])) {
dir = (dir + 1) % 4;
nextStep(dir, x, y, nx, ny);
if (!(0 <= nx && nx < n && 0 <= ny && ny < n && !matrix[nx][ny])) {
break;
}
}
x = nx, y = ny;
}
for (int i = 0; i < n; i++) {
vector<int> row(matrix[i], matrix[i] + n);
result.push_back(row);
}
return result;
}
};