欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Matrix Cells in Distance Order

程序员文章站 2022-05-03 22:26:38
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

参考文献