forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAC_simulation_nm.cpp
75 lines (65 loc) · 1.75 KB
/
AC_simulation_nm.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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_simulation_nm.cpp
* Create Date: 2014-12-26 10:58:30
* Descripton: simulation, use O(n*m) space
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
class Solution {
private:
bool vis[N][N];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
bool checkPoint(int x, int y, int n) {
return (x >= 0 && x < n && y >= 0 && y < n && !vis[x][y]);
}
bool checkNext(int x, int y, int n) {
int tx, ty;
for (int i = 0; i < 4; i++) {
tx = x + dx[i];
ty = y + dy[i];
if (checkPoint(tx, ty, n))
return true;
}
return false;
}
public:
vector<vector<int> > generateMatrix(int n) {
// generate a new matrix
vector<vector<int> > matrix;
for (int i = 0; i < n; i++)
matrix.push_back(vector<int>(n));
if (n == 0)
return matrix;
memset(vis, 0, sizeof(vis));
int curx = 0, cury = 0, curd = 0, cnt = 1;
vis[curx][cury] = true;
matrix[curx][cury] = cnt++;
while (checkNext(curx, cury, n)) {
if (checkPoint(curx + dx[curd], cury + dy[curd], n)) {
curx += dx[curd];
cury += dy[curd];
vis[curx][cury] = true;
matrix[curx][cury] = cnt++;
} else {
curd = (curd + 1) % 4;
}
}
return matrix;
}
};
int main() {
int n;
Solution s;
while (cin >> n) {
vector<vector<int> > ans = s.generateMatrix(n);
for (auto &i : ans) {
for (auto &j : i)
cout << j << ' ';
cout << endl;
}
}
return 0;
}