Leetcode-1030 Matrix Cells in Distance Order(距离顺序排列矩阵单元格)
1 #define pb push_back 2 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 3 #define INF 0x3f3f3f3f 4 5 typedef long long ll; 6 const int maxn = 50003; 7 8 int readint() 9 { 10 int t; 11 scanf("%d",&t); 12 return t; 13 } 14 15 struct k 16 { 17 int r; 18 int c; 19 int diss; 20 bool operator < (k b) 21 { 22 return diss < b.diss; 23 } 24 }; 25 class Solution 26 { 27 public: 28 vector<vector<int>> allCellsDistOrder(int R, int C, int r0, int c0) 29 { 30 vector<k> v; 31 32 _for(i,0,R) 33 { 34 _for(j,0,C) 35 { 36 k kk; 37 kk.r = i; 38 kk.c = j; 39 kk.diss = abs(i-r0)+abs(j-c0); 40 v.pb(kk); 41 } 42 } 43 44 sort(v.begin(),v.end()); 45 vector<vector<int>> rnt; 46 _for(i,0,R*C) 47 { 48 vector<int> tmp; 49 tmp.pb(v[i].r); 50 tmp.pb(v[i].c); 51 rnt.pb(tmp); 52 } 53 return rnt; 54 } 55 };