The 11th Group B provincial competition plane division

Insert picture description here
Derivation background: the number of planes added is equal to the number of intersections added plus one,
so the core idea is to solve the number of intersections newly generated each time a straight line is added;

Let each newly added straight line intersect with all previous straight lines. In the
initialization vv, the number of intersections added by the newly added node is placed.
If there is no intersection, continue to join.
There is an intersection.
Determine whether there is a point, whether it continues to be added to the v set.
Until the current line has crossed all the previous lines, the number of intersections is obtained, that is, v.size() is the number of intersections added after the new line is added .
ans does the cumulative sum.
Next, add a new line here.
Reference source
solution:

#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(s) memset(s, 0, sizeof(s))
const int INF = 0x3f3f3f3f;
const int maxn = 1010;

int n, ans;
int a[maxn],b[maxn];
struct Point {
    
    
    double x, y;
};

bool isequal(Point a, Point b)
{
    
    
	return (abs(a.x - b.x) <= 1e-2 && abs(a.y - b.y) <= 1e-2);
}
Point crosspoint(int m, int n)
{
    
    
	double x1 = a[m],x2 = a[n],y1 = b[m],y2 = b[n];
	//平行则无交点
	if(x1 == x2)
	{
    
    
		return Point {
    
    INF,INF};
	}
	Point cp = Point();
	cp.x = (y2 - y1) /(x2 - x1);
	return cp;
}
int main() 
{
    
    	
	cin >> n;
	for(int i = 1; i <= n; i++)
	{
    
    
		cin >> a[i] >> b[i];
	}
	ans = 2;
	for(int i = 2; i <= n; i++)
	{
    
    
		//新加入的i与之前的所有直线求焦点 不重复就放入v中;
		vector<Point> v;
		bool flag = false;
		for(int j = 1; j < i; j++)
		{
    
    
			Point now = crosspoint(i, j);
			if(now.x == INF || now.y == INF) continue;
			else
			{
    
    
				for(int k = 0; k < v.size(); k++)
				{
    
    
				//判断是否有重点 
					if(isequal(now,v[k])) flag = true;
				}
			}
			//没有就添加进入交点集合
			if(flag == false) v.push_back(now);
		}
		ans += v.size() + 1;
	}
	cout << ans << endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/moumoumouwang/article/details/109685975