-
Notifications
You must be signed in to change notification settings - Fork 0
/
1030. 距离顺序排列矩阵单元格.cpp
36 lines (31 loc) · 1.1 KB
/
1030. 距离顺序排列矩阵单元格.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
class Solution {
public:
vector<vector<int>> allCellsDistOrder(int R, int C, int r0, int c0) {
vector<vector<int>> out;
out.push_back(vector<int>{r0, c0});
for(int i=1;i<R+C;i++)
{
for(int k=0; k<=i; k++)
{
// cout<<r0+i-k<<","<<c0+k<<endl;
if(val(r0+i-k,c0+k,R,C))
out.push_back(vector<int>{r0+i-k,c0+k});
// cout<<r0-i+k<<","<<c0-k<<endl;
if(val(r0-i+k,c0-k,R,C))
out.push_back(vector<int>{r0-i+k,c0-k});
// cout<<r0+i+k<<","<<c0-k<<endl;
if(val(r0+i-k,c0-k,R,C) && k!=0 && k!=i)
out.push_back(vector<int>{r0+i-k,c0-k});
// cout<<r0-i-k<<","<<c0+k<<endl;
if(val(r0-i+k,c0+k,R,C) && k!=0 && k!=i)
out.push_back(vector<int>{r0-i+k,c0+k});
}
}
return out;
}
bool val(int r,int c,int R, int C)
{
if(r>=0 && r<R && c>=0 && c<C) return true;
return false;
}
};