202006-1 线性分类器
思路:数学问题中的判断点与直线位置关系
#include<iostream>
#include<vector>
using namespace std;
typedef struct point {
int x;
int y;
}P;
typedef struct line {
int k0, k1, k2;
}L;
bool Classify(vector<P> A, vector<P> B, L line);//判断某条直线是否满足条件
int main()
{
vector<P> A, B;
vector<L> lines;
int n, m; cin >> n >> m;
for (int i = 0; i < n; i++)
{
P p;
char ch;
cin >> p.x >> p.y >> ch;
if (ch == 'A') A.push_back(p);
else B.push_back(p);
}
for (int i = 0; i < m; i++)
{
L l;
cin >> l.k0 >> l.k1 >> l.k2;
lines.push_back(l);
}
for (int i = 0; i < m; i++)
if (Classify(A, B,lines[i])) cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
}
bool Classify(vector<P> A, vector<P> B, L line)
{
int A_cnt = 0, B_cnt = 0;
for (int i = 0; i < A.size(); i++)
{
if (line.k0 + line.k1 * A[i].x + line.k2 * A[i].y > 0) A_cnt++;
}
for (int i = 0; i < B.size(); i++)
{
if (line.k0 + line.k1 * B[i].x + line.k2 * B[i].y > 0) B_cnt++;
}
if((A_cnt==A.size()&&B_cnt==0)||(B_cnt==B.size()&&A_cnt==0))//A和B在直线两侧的情况
return true;
else return false;
}