前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 1030. 距离顺序排列矩阵单元格(排序&Lambda表达式&BFS)

LeetCode 1030. 距离顺序排列矩阵单元格(排序&Lambda表达式&BFS)

作者头像
Michael阿明
发布2020-07-13 16:08:38
3370
发布2020-07-13 16:08:38
举报

1. 题目

给出 R 行 C 列的矩阵,其中的单元格的整数坐标为 (r, c),满足 0 <= r < R 且 0 <= c < C。

另外,我们在该矩阵中给出了一个坐标为 (r0, c0) 的单元格。

返回矩阵中的所有单元格的坐标,并按到 (r0, c0) 的距离从最小到最大的顺序排,其中,两单元格(r1, c1) 和 (r2, c2) 之间的距离是曼哈顿距离,|r1 - r2| + |c1 - c2|。(你可以按任何满足此条件的顺序返回答案。)

代码语言:javascript
复制
示例 1:
输入:R = 1, C = 2, r0 = 0, c0 = 0
输出:[[0,0],[0,1]]
解释:从 (r0, c0) 到其他单元格的距离为:[0,1]

示例 2:
输入:R = 2, C = 2, r0 = 0, c0 = 1
输出:[[0,1],[0,0],[1,1],[1,0]]
解释:从 (r0, c0) 到其他单元格的距离为:[0,1,1,2]
[[0,1],[1,1],[0,0],[1,0]] 也会被视作正确答案。

示例 3:
输入:R = 2, C = 3, r0 = 1, c0 = 2
输出:[[1,2],[0,2],[1,1],[0,1],[1,0],[0,0]]
解释:从 (r0, c0) 到其他单元格的距离为:[0,1,1,2,2,3]
其他满足题目要求的答案也会被视为正确,例如 [[1,2],[1,1],[0,2],[1,0],[0,1],[0,0]]。

提示:
1 <= R <= 100
1 <= C <= 100
0 <= r0 < R
0 <= c0 < C

来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/matrix-cells-in-distance-order 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

2.1 multimap

  • 利用其有序性,用距离作为key,vector 作为 value
代码语言:javascript
复制
class Solution {
public:
    vector<vector<int>> allCellsDistOrder(int R, int C, int r0, int c0) {
        vector<vector<int>> ans;
        multimap<int,vector<int>> m;
        int i, j, k = 0;
        for(i = 0; i < R; ++i)
        {
        	for(j = 0; j < C; ++j)
        		m.emplace(pair<int,vector<int>> (abs(i-r0)+abs(j-c0),vector<int> ({i,j})));
        }
        for(auto& kv : m)
        	ans.push_back(kv.second);//可以把多个vector<int>一起push进去,神奇
        return ans;
    }
};
在这里插入图片描述
在这里插入图片描述

2.2 Lambda 表达式排序

代码语言:javascript
复制
class Solution {
public:
    vector<vector<int>> allCellsDistOrder(int R, int C, int r0, int c0) {
        vector<vector<int>> ans(R*C);
        int i, j, k = 0;
        for(i = 0; i < R; ++i)
        	for(j = 0; j < C; ++j)
        		ans[k++] = {i,j};
   		//[&],里面的&表示表示式所在区域的外部变量可见,且是引用传递,之前没写,报错
        sort(ans.begin(), ans.end(), [&](auto& a, auto& b)
        		{return abs(a[0]-r0)+abs(a[1]-c0) < abs(b[0]-r0)+abs(b[1]-c0);});
        return ans;
    }
};
在这里插入图片描述
在这里插入图片描述

2.3 BFS搜索

代码语言:javascript
复制
class Solution {
	vector<vector<int>> dir = {{1,0},{0,1},{0,-1},{-1,0}};
public:
    vector<vector<int>> allCellsDistOrder(int R, int C, int r0, int c0) {
        vector<vector<int>> ans(R*C);
        bool visited[R][C];
        memset(visited, 0, sizeof visited);
        queue<pair<int,int>> q;
        pair<int,int> tp;
        q.push({r0,c0});
        visited[r0][c0] = true;
        int x, y, k, i = 0;
        while(!q.empty())
        {
        	tp = q.front();
        	q.pop();
        	ans[i++] = {tp.first, tp.second};
        	for(k = 0; k < 4; ++k)
        	{
        		x = tp.first + dir[k][0];
        		y = tp.second + dir[k][1];
        		if(x>=0 && x<R && y>=0 && y<C && !visited[x][y])
        		{
        			q.push({x,y});
        			visited[x][y] = true;
        		}
        	}
        }
        return ans;
    }
};
在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-11-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 题目
  • 2. 解题
    • 2.1 multimap
      • 2.2 Lambda 表达式排序
        • 2.3 BFS搜索
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档