202006-1 线性分类器
二分类的线性分类器,通过一条直线将二维坐标上的点分成两部分,判断分类结果是否正确。
每个点带入直线方程 θ 0 + θ 1 x + θ 2 y = 0 \theta_0 + \theta_1x + \theta_2y = 0 θ0+θ1x+θ2y=0。
- 在直线上方的点,直线方程大于零
- 在直线下方的点,直线方程小于零
- 在直线上的点,直线方程等于零
对于每条直线,将所有的点带入判断即可
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int node_size = in.nextInt();
int test_size = in.nextInt();
int[] x_index = new int[node_size];
int[] y_index = new int[node_size];
char[] type = new char[node_size];
for(int i=0; i<node_size; i++){
x_index[i] = in.nextInt();
y_index[i] = in.nextInt();
type[i] = in.nextLine().toCharArray()[1];
}
for(int i=0; i<test_size; i++){
int q1 = in.nextInt();
int q2 = in.nextInt();
int q3 = in.nextInt();
boolean res = true;
int[] judge = new int[2];
for(int j=0; j<node_size; j++){
if(j == 0){
//确定A,B类点在直线上方还是在直线下方
if(q1 + q2*x_index[j] + q3*y_index[j] > 0){
if(type[j] == 'A'){
judge[0] = 1;
judge[1] = -1;
}else{
judge[0] = -1;
judge[1] = 1;
}
}else if(q1 + q2*x_index[j] + q3*y_index[j] < 0){
if(type[j] == 'A'){
judge[0] = -1;
judge[1] = 1;
}else{
judge[0] = 1;
judge[1] = -1;
}
}else{
res = false;
break;
}
}else{
if((q1 + q2*x_index[j] + q3*y_index[j] > 0 && judge[type[j] - 'A'] == 1) ||
(q1 + q2*x_index[j] + q3*y_index[j] < 0 && judge[type[j] - 'A'] == -1)){
continue;
}else{
res = false;
break;
}
}
}
System.out.println(res?"Yes":"No");
}
}
}