博客
关于我
FZU 1015 土地划分
阅读量:157 次
发布时间:2019-02-27

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

为了解决这个问题,我们需要计算农庄主划分土地后的块数。农庄主使用矩形地图划分线段,每条线段从一条边界的点到另一条边界的点。我们的任务是计算这些线段划分后的区域数量。

方法思路

  • 问题分析:每条线段连接矩形边界的两个点,可能会与其他线段相交,导致区域数量增加。我们需要计算所有线段之间的交点数目,从而确定最终的区域数量。
  • 交点计算:使用行列式方法判断两条线段是否相交,并确定交点是否在线段内部。这样可以确保我们只计算有效的交点。
  • 区域计算:初始区域数为1,每条线段增加1个区域。每次两条线段相交,区域数增加1个。总的区域数等于1 + 线段数 + 交点数。
  • 解决代码

    #include 
    using namespace std;struct Point { int x; int y;};bool is_intersect(const Point& a, const Point& b, const Point& c, const Point& d) { int dx1 = b.x - a.x; int dy1 = b.y - a.y; int dx2 = d.x - c.x; int dy2 = d.y - c.y; int D = dx1 * dy2 - dx2 * dy1; if (D == 0) { return false; } int numerator_t = (c.x - a.x) * dy2 - (d.x - c.x) * (c.y - a.y); float t = static_cast
    (numerator_t) / D; int numerator_s = dx1 * (c.y - a.y) - dx2 * (b.y - a.y); float s = static_cast
    (numerator_s) / D; return (t >= 0 && t <= 1) && (s >= 0 && s <= 1);}int main() { int w, h; Point** lines = new Point*; while (true) { cin >> w >> h; if (w == 0 && h == 0) { break; } int L = 0; cin >> L; lines = new Point*[L + 1]; for (int i = 1; i <= L; ++i) { int x, y; cin >> x >> y; lines[i] = new Point(x, y); } int count = 0; for (int i = 1; i <= L; ++i) { Point a = lines[i][0]; Point b = lines[i][1]; for (int j = 1; j < i; ++j) { Point c = lines[j][0]; Point d = lines[j][1]; if (is_intersect(a, b, c, d)) { count++; } } } int regions = 1 + L + count; cout << regions << endl; } delete[] lines; return 0;}

    代码解释

  • 结构体Point:用于存储坐标点。
  • is_intersect函数:判断两条线段是否相交,并返回交点是否在线段内部。
  • 主函数:读取输入数据,处理每组数据,计算交点数目,并输出区域数量。对于每条线段,检查它与之前所有线段的交点,累加交点数目,最后计算总区域数。
  • 该方法通过计算线段的交点数目,准确地确定了区域数量,确保了结果的正确性。

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

    你可能感兴趣的文章
    NUMPY矢量化np.prod不能构造具有超过32个操作数的ufunc
    查看>>
    Numpy矩阵与通用函数
    查看>>
    numpy绘制热力图
    查看>>
    numpy转PIL 报错TypeError: Cannot handle this data type
    查看>>
    Numpy闯关100题,我闯了95关,你呢?
    查看>>
    nump模块
    查看>>
    Nutch + solr 这个配合不错哦
    查看>>
    NuttX 构建系统
    查看>>
    NutUI:京东风格的轻量级 Vue 组件库
    查看>>
    NutzCodeInsight 2.0.7 发布,为 nutz-sqltpl 提供友好的 ide 支持
    查看>>
    NutzWk 5.1.5 发布,Java 微服务分布式开发框架
    查看>>
    NUUO网络视频录像机 css_parser.php 任意文件读取漏洞复现
    查看>>
    NUUO网络视频录像机 upload.php 任意文件上传漏洞复现
    查看>>
    Nuxt Time 使用指南
    查看>>
    NuxtJS 接口转发详解:Nitro 的用法与注意事项
    查看>>
    NVDIMM原理与应用之四:基于pstore 和 ramoops保存Kernel panic日志
    查看>>
    NVelocity标签使用详解
    查看>>
    NVelocity标签设置缓存的解决方案
    查看>>
    Nvidia Cudatoolkit 与 Conda Cudatoolkit
    查看>>
    NVIDIA GPU 的状态信息输出,由 `nvidia-smi` 命令生成
    查看>>