-
Notifications
You must be signed in to change notification settings - Fork 0
/
885. Spiral Matrix III.cpp
56 lines (53 loc) · 1.14 KB
/
885. Spiral Matrix III.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
///
/// @file :885. Spiral Matrix III.cpp
/// @author :wmjtxt(972213032@qq.com)
/// @date :2018-10-10 20:03:25(NewYork time)
/// @quote :
///
#include <iostream>
#include <vector>
#include <unistd.h>
using namespace std;
class Solution{
public:
vector<vector<int>> spiralMatrixIII(int R, int C, int r0, int c0){
//11223344
//right, down, left, left, up, up, right, right, right
vector<vector<int>> res;
int i = r0, j = c0, k = 0, t = 0, n = R * C, flag = 0, m = 1;
while(k < n){
int mm = m;
while(mm != 0){
if(i >= 0 && i < R && j >= 0 && j < C){
vector<int> tmp(2);
//cout << "(" << i << "," << j << ")" << endl;
tmp[0] = i;
tmp[1] = j;
res.push_back(tmp);
++k;
}
//cout << "[" << i << "," << j << "]" << endl;
if(flag == 0) j++;
if(flag == 1) i++;
if(flag == 2) j--;
if(flag == 3) i--;
mm--;
//sleep(3);
}
flag++;
flag = flag%4;
t++;
if(t%2 == 0) m++;
}
return res;
}
};
int main(){
Solution s;
vector<vector<int>> a;
a = s.spiralMatrixIII(1,4,0,0);
for(int i = 0; i < a.size(); i++){
cout << a[i][0] << a[i][1] << endl;
}
return 0;
}