第11届B组省赛 平面分割

在这里插入图片描述
推导背景: 增加的平面个数等于增加的交点个数加一
所以核心思想就变成了求解每次加入直线时新产生的交点个数;

让每次新加入的直线与之前的所有直线求交点
初始化v v里放的是新加入的节点增加的交点个数。
无交点 继续加入
有交点
判断是否有重点 有继续 没有加入v集合
直到当前直线与之前所有直线求过交点后,得出交点个数也就是v.size()为新加入直线后增加的交点个数。
ans做累计求和
接下来在此增加新的直线。
参考出处
题解:

#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;
}

猜你喜欢

转载自blog.csdn.net/moumoumouwang/article/details/109685975