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://www.cnblogs.com/overlows/p/11226081.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值