博客
关于我
Weekly Contest 133
阅读量:426 次
发布时间:2019-03-06

本文共 1009 字,大约阅读时间需要 3 分钟。

为了解决这个问题,我们需要找到给定矩阵中每个细胞到指定点的曼哈顿距离,并按距离从小到大排序。

方法思路

  • 问题分析:我们需要计算每个细胞到指定点的曼哈顿距离,并将这些细胞按距离排序。
  • 曼哈顿距离:曼哈顿距离是两个点行和列坐标差的绝对值之和。
  • 遍历矩阵:遍历矩阵中的每个细胞,计算其到指定点的曼哈顿距离。
  • 排序:将所有细胞按距离从小到大排序。
  • 解决代码

    #include 
    #include
    using namespace std;vector
    > allCellsDistOrder(int R, int C, int r0, int c0) { vector
    > allPoints; for (int r = 0; r < R; ++r) { for (int c = 0; c < C; ++c) { int dis = abs(r - r0) + abs(c - c0); allPoints.push_back({r, c}); } } sort(allPoints.begin(), allPoints.end(), [](const pair
    & a, const pair
    & b) { return (abs(a.first - r0) + abs(a.second - c0)) < (abs(b.first - r0) + abs(b.second - c0)); }); vector
    > result; for (auto& p : allPoints) { result.push_back({p.first, p.second}); } return result;}

    代码解释

  • 初始化:创建一个向量allPoints来存储所有细胞的坐标。
  • 遍历矩阵:双重循环遍历矩阵中的每个细胞,计算其到指定点的曼哈顿距离,并将该距离与坐标一同存储在allPoints中。
  • 排序:使用std::sort函数对allPoints进行排序,排序依据是曼哈顿距离。
  • 结果构建:将排序后的结果转换为向量result,每个子向量表示一个细胞的坐标。
  • 这种方法确保了我们能够高效地计算并按距离排序所有细胞的坐标。

    转载地址:http://iftuz.baihongyu.com/

    你可能感兴趣的文章
    org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
    查看>>
    org.apache.poi.hssf.util.Region
    查看>>
    org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
    查看>>
    org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
    查看>>
    org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:processDebugManifest'
    查看>>
    org.hibernate.HibernateException: Unable to get the default Bean Validation factory
    查看>>
    org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
    查看>>
    org.springframework.amqp.AmqpConnectException:java.net.ConnectException:Connection timed out:connect
    查看>>
    org.springframework.beans.factory.BeanDefinitionStoreException
    查看>>
    org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata
    查看>>
    org.springframework.boot:spring boot maven plugin丢失---SpringCloud Alibaba_若依微服务框架改造_--工作笔记012
    查看>>
    SQL-CLR 类型映射 (LINQ to SQL)
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>
    org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded
    查看>>
    org.tinygroup.serviceprocessor-服务处理器
    查看>>
    org/eclipse/jetty/server/Connector : Unsupported major.minor version 52.0
    查看>>
    org/hibernate/validator/internal/engine
    查看>>
    Orleans框架------基于Actor模型生成分布式Id
    查看>>
    SQL-36 创建一个actor_name表,将actor表中的所有first_name以及last_name导入改表。
    查看>>