zoukankan      html  css  js  c++  java
  • hihoCoder#1040 矩形判断

    原题地址

    一开始写了一个求交点然后算角度的,特别复杂不说,还很容易出错,WA了很久也不知道错在哪

    后来想想,应该用向量做

    四条直线能围成一个矩形 <=> 四条直线两两之间有4个平行关系,8个垂直关系

    注意,直线重合并不算平行

    代码清晰明了,一遍AC,这酸爽。

    代码:

     1 #include <iostream>
     2 #include <vector>
     3 
     4 using namespace std;
     5 
     6 struct Line {
     7   int x[2];
     8   int y[2];
     9   Line(int x1, int y1, int x2, int y2) {
    10     x[0] = x1;
    11     x[1] = x2;
    12     y[0] = y1;
    13     y[1] = y2;
    14   }
    15   int vx() {return x[0] - x[1];}
    16   int vy() {return y[0] - y[1];}
    17 };
    18 
    19 bool parallelp(Line &a, Line &b) {
    20   return a.vx() * b.vy() - b.vx() * a.vy() == 0;
    21 }
    22 
    23 bool samep(Line &a, Line &b) {
    24   Line c(a.x[0], a.y[0], b.x[0], b.y[0]);
    25   return parallelp(a, c);
    26 }
    27 
    28 bool verticalp(Line &a, Line &b) {
    29   return a.vx() * b.vx() + a.vy() * b.vy() == 0;
    30 }
    31 
    32 int parallel_count(vector<Line> &lines) {
    33   int res = 0;
    34 
    35   for (int i = 0; i < 4; i++) {
    36     for (int j = 0; j < 4; j++) {
    37       if (i == j)
    38         continue;
    39       if (parallelp(lines[i], lines[j]) && !samep(lines[i], lines[j]))
    40         res++;
    41     }
    42   }
    43 
    44   return res;
    45 }
    46 
    47 int vertical_count(vector<Line> &lines) {
    48   int res = 0;
    49 
    50   for (int i = 0; i < 4; i++) {
    51     for (int j = 0; j < 4; j++) {
    52       if (i == j)
    53         continue;
    54       if (verticalp(lines[i], lines[j]))
    55         res++;
    56     }
    57   }
    58 
    59   return res;
    60 }
    61 
    62 bool rectanglep(vector<Line> &lines) {
    63   int p_count = parallel_count(lines);
    64   int v_count = vertical_count(lines);
    65   return p_count == 4 && v_count == 8;
    66 }
    67 
    68 int main() {
    69   int n;
    70   double x1, y1, x2, y2;
    71 
    72   cin >> n;
    73   while (n--) {
    74     vector<Line> lines;
    75     for (int i = 0; i < 4; i++) {
    76       cin >> x1 >> y1 >> x2 >> y2;
    77       lines.push_back(Line(x1, y1, x2, y2));
    78     }
    79 
    80     if (rectanglep(lines))
    81       cout << "YES" << endl;
    82     else
    83       cout << "NO" << endl;
    84   }
    85   return 0;
    86 }
  • 相关阅读:
    记第一场省选
    POJ 2083 Fractal 分形
    CodeForces 605A Sorting Railway Cars 思维
    FZU 1896 神奇的魔法数 dp
    FZU 1893 内存管理 模拟
    FZU 1894 志愿者选拔 单调队列
    FZU 1920 Left Mouse Button 简单搜索
    FZU 2086 餐厅点餐
    poj 2299 Ultra-QuickSort 逆序对模版题
    COMP9313 week4a MapReduce
  • 原文地址:https://www.cnblogs.com/boring09/p/4364403.html
Copyright © 2011-2022 走看看