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

    你可能感兴趣的文章
    nrm —— 快速切换 NPM 源 (附带测速功能)
    查看>>
    nrm报错 [ERR_INVALID_ARG_TYPE]
    查看>>
    NS3 IP首部校验和
    查看>>
    NSDateFormatter的替代方法
    查看>>
    NSError 的使用方法
    查看>>
    NSGA-Ⅲ源代码
    查看>>
    nsis 安装脚本示例(转)
    查看>>
    NSJSON的用法(oc系统自带的解析方法)
    查看>>
    nslookup 的基本知识与命令详解
    查看>>
    NSOperation基本操作
    查看>>
    NSRange 范围
    查看>>
    NSSet集合 无序的 不能重复的
    查看>>
    NSURLSession下载和断点续传
    查看>>
    NSUserdefault读书笔记
    查看>>
    NS图绘制工具推荐
    查看>>
    NT AUTHORITY\NETWORK SERVICE 权限问题
    查看>>
    NT symbols are incorrect, please fix symbols
    查看>>
    ntelliJ IDEA 报错:找不到包或者找不到符号
    查看>>
    NTFS文件权限管理实战
    查看>>
    ntko web firefox跨浏览器插件_深度比较:2019年6个最好的跨浏览器测试工具
    查看>>