Matrix Cells in Distance Order

2019-08-16 07:52:25来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

Matrix Cells in Distance Order

Matrix Cells in Distance Order

We are given a matrix with R rows and C columns has cells with integer coordinates (r, c), where 0 <= r < R and 0 <= c < C.

Additionally, we are given a cell in that matrix with coordinates (r0, c0).

Return the coordinates of all cells in the matrix, sorted by their distance from (r0, c0) from smallest distance to largest distance.  Here, the distance between two cells (r1, c1) and (r2, c2) is the Manhattan distance, |r1 - r2| + |c1 - c2|.  (You may return the answer in any order that satisfies this condition.)

Example 1:

Input: R = 1, C = 2, r0 = 0, c0 = 0
Output: [[0,0],[0,1]]
Explanation: The distances from (r0, c0) to other cells are: [0,1]

Example 2:

Input: R = 2, C = 2, r0 = 0, c0 = 1
Output: [[0,1],[0,0],[1,1],[1,0]]
Explanation: The distances from (r0, c0) to other cells are: [0,1,1,2]
The answer [[0,1],[1,1],[0,0],[1,0]] would also be accepted as correct.

Example 3:

Input: R = 2, C = 3, r0 = 1, c0 = 2
Output: [[1,2],[0,2],[1,1],[0,1],[1,0],[0,0]]
Explanation: The distances from (r0, c0) to other cells are: [0,1,1,2,2,3]
There are other answers that would also be accepted as correct, such as [[1,2],[1,1],[0,2],[1,0],[0,1],[0,0]].

Note:

1 <= R <= 100
1 <= C <= 100
0 <= r0 < R
0 <= c0 < C

Code

//
//  main.cpp
//  最短最长距离
//
//  Created by mac on 2019/7/21.
//  Copyright ? 2019 mac. All rights reserved.
//

#include <iostream>
#include <algorithm>
#include <unordered_map> //Hash Table

#include <vector>
#include <cmath>

using namespace std;

class Solution {
public:
    vector<vector<int>> allCellsDistOrder(int R, int C, int r0, int c0)
    {
        vector<vector<int>> res;
        vector<vector<vector<int>>> box(R+C-1);
        for(int i=0;i<R;i++)
            for(int j=0;j<C;j++)
            {
                vector<int> temp={i,j};
                int index=abs(i-r0)+abs(j-c0);
                box[index].push_back(temp);
            }
        for(auto i:box)
            if(i.size())
                for(auto j:i) res.push_back(j);
        return res;
    }
};

int main(int argc, const char * argv[]) {
    
    int R=2;
    int C=2;
    int r0=0;
    int c0=1;
    Solution so;
    vector<vector<int>> test = so.allCellsDistOrder(2, 2, 0, 1);
    cout<<"[";
    for (auto i : test) {
        cout<<"[";
        for (auto j :i) {
            cout<<j<<",";
        }
        cout<<"],";
        
    }
    cout<<"]";
    
//    vector<int> a(3);
//    a={1,2,3,4};
//    vector<vector<int>> b,e;
//    vector<vector<vector<int>>> c;
//
//    b.push_back(a);
//    b.push_back({2,3,4,5,9});
//    b.push_back({1,2,3,89,44});
//
//
//    e.push_back({9,3,4,5,9});
//    e.push_back({8,2,3,89,44});
//
//    c.push_back(b);
//    c.push_back(e);
//
//    for(auto i:c){
//        for (auto j:i) {
//            for (auto k : j) {
//                cout<<k<<" ";
//            }
//            cout<<endl;
//        }
//        cout<<endl<<endl;
//    }
    
    
    
//=============================
    
//    for (int k=0; k<b.size(); ++k) {
//        for (int i=0; i<a.size(); ++i) {
//            cout<<b[k][i]<<" ";
//        }
//        cout<<endl;
//    }

    
    return 0;
}

运行结果

[[0,1,],[0,0,],[1,1,],[1,0,],]Program ended with exit code: 0

参考文献

  • https://leetcode.com/problems/matrix-cells-in-distance-order/

原文链接:https://www.cnblogs.com/overlows/p/11226081.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:长乐培训Day2

下一篇:2019 Multi-University Training Contest 2: 1010 Just Skip The