博客
关于我
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/

    你可能感兴趣的文章
    ntko web firefox跨浏览器插件_深度比较:2019年6个最好的跨浏览器测试工具
    查看>>
    ntko文件存取错误_苹果推送 macOS 10.15.4:iCloud 云盘文件夹共享终于来了
    查看>>
    ntp server 用法小结
    查看>>
    ntpdate 通过外网同步时间
    查看>>
    ntpdate同步配置文件调整详解
    查看>>
    NTPD使用/etc/ntp.conf配置时钟同步详解
    查看>>
    NTP及Chrony时间同步服务设置
    查看>>
    NTP服务器
    查看>>
    NTP配置
    查看>>
    NUC1077 Humble Numbers【数学计算+打表】
    查看>>
    NuGet Gallery 开源项目快速入门指南
    查看>>
    NuGet(微软.NET开发平台的软件包管理工具)在VisualStudio中的安装的使用
    查看>>
    nuget.org 无法加载源 https://api.nuget.org/v3/index.json 的服务索引
    查看>>
    Nuget~管理自己的包包
    查看>>
    NuGet学习笔记001---了解使用NuGet给net快速获取引用
    查看>>
    nullnullHuge Pages
    查看>>
    NullPointerException Cannot invoke setSkipOutputConversion(boolean) because functionToInvoke is null
    查看>>
    null可以转换成任意非基本类型(int/short/long/float/boolean/byte/double/char以外)
    查看>>
    Number Sequence(kmp算法)
    查看>>
    Numix Core 开源项目教程
    查看>>