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

    你可能感兴趣的文章
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    no session found for current thread
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>
    NO.23 ZenTaoPHP目录结构
    查看>>
    NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
    查看>>
    NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
    查看>>
    Node JS: < 一> 初识Node JS
    查看>>
    Node-RED中使用JSON数据建立web网站
    查看>>
    Node-RED中使用json节点解析JSON数据
    查看>>
    Node-RED中使用node-random节点来实现随机数在折线图中显示
    查看>>
    Node-RED中使用node-red-browser-utils节点实现选择Windows操作系统中的文件并实现图片预览
    查看>>
    Node-RED中使用node-red-contrib-image-output节点实现图片预览
    查看>>
    Node-RED中使用node-red-node-ui-iframe节点实现内嵌iframe访问其他网站的效果
    查看>>
    Node-RED中使用Notification元件显示警告讯息框(温度过高提示)
    查看>>
    Node-RED中实现HTML表单提交和获取提交的内容
    查看>>
    Node-RED中通过node-red-ui-webcam节点实现访问摄像头并截取照片预览
    查看>>
    Node.js 8 中的 util.promisify的详解
    查看>>
    Node.js 函数是什么样的?
    查看>>