poj2653

本文详细阐述了如何使用暴力算法解决几何问题,并着重介绍了vector容器的使用方法和erase函数的作用。通过实例展示了如何通过迭代器操作vector容器来优化算法效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

暴力题

注意vector用法,erase函数返回的是删除后的下一个元素的指针。迭代器的写法是:vector<  >::iterator i;

ContractedBlock.gifExpandedBlockStart.gifView Code
#include <iostream>
#include
<cstdlib>
#include
<cstring>
#include
<cstdio>
#include
<vector>
using namespace std;

#define maxn 100005
#define eps 1e-10

struct Point
{
double x, y;
};

struct Line
{
Point a, b;
} line[maxn];

vector
<int> stk(maxn);
int n;

void input()
{
for (int i = 0; i < n; i++)
scanf(
"%lf%lf%lf%lf", &line[i].a.x, &line[i].a.y, &line[i].b.x, &line[i].b.y);
}

bool inter(Point &a, Point &b, Point &c, Point &d)
{
if (min(a.x, b.x) > max(c.x, d.x) || min(a.y, b.y) > max(c.y, d.y) || min(c.x, d.x) > max(a.x, b.x) || min(c.y, d.y) > max(a.y, b.y))
return 0;
double h = (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
double i = (b.x - a.x) * (d.y - a.y) - (b.y - a.y) * (d.x - a.x);
double j = (d.x - c.x) * (a.y - c.y) - (d.y - c.y) * (a.x - c.x);
double k = (d.x - c.x) * (b.y - c.y) - (d.y - c.y) * (b.x - c.x);
return h * i <= eps && j * k <= eps;
}

bool cross(Line &p, Line &q)
{
return inter(p.a, p.b, q.a, q.b);
}

void make(Line &l)
{
for (vector<int>::iterator i = stk.begin(); i < stk.end();)
{
if (cross(line[*i], l))
i
= stk.erase(i);
else
i
++;
}
}

void work()
{
for (int i = 0; i < n; i++)
{
make(line[i]);
stk.push_back(i);
}
printf(
"Top sticks: %d", stk[0] + 1);
for (vector<int>::iterator i = stk.begin() + 1; i < stk.end(); i++)
printf(
", %d", (*i) + 1);
printf(
".\n");
}

int main()
{
//freopen("t.txt", "r", stdin);
while (scanf("%d", &n), n)
{
stk.clear();
input();
work();
}
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值