简单几何(直线求交点) POJ 2074 Line of Sight
题意:从一条马路(线段)看对面的房子(线段),问连续的能看到房子全部的最长区间
分析:自己的思路WA了:先对障碍物根据坐标排序,然后在相邻的障碍物的间隔找到区间,这样还要判断是否被其他障碍物遮挡住(哇
网上有很好的思路,先对每条线段找到阴影的端点,然后根据坐标排序,求和左端点的距离的最大值,这样省去线段相交的判断。
trick点应该就是障碍物的位置随意,可能在房子和马路的外面。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | /************************************************ * Author :Running_Time * Created Time :2015/11/2 星期一 20:33:38 * File Name :POJ_2074.cpp ************************************************/ #include <cstdio> #include <algorithm> #include <iostream> #include <sstream> #include <cstring> #include <cmath> #include <string> #include <vector> #include <queue> #include <deque> #include <stack> #include <list> #include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime> using namespace std; #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 typedef long long ll; const int N = 1e5 + 10; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = acos (-1.0); int dcmp( double x) { //三态函数,减少精度问题 if ( fabs (x) < EPS) return 0; else return x < 0 ? -1 : 1; } struct Point { //点的定义 double x, y; Point () {} Point ( double x, double y) : x (x), y (y) {} Point operator + ( const Point &r) const { //向量加法 return Point (x + r.x, y + r.y); } Point operator - ( const Point &r) const { //向量减法 return Point (x - r.x, y - r.y); } Point operator * ( double p) const { //向量乘以标量 return Point (x * p, y * p); } Point operator / ( double p) const { //向量除以标量 return Point (x / p, y / p); } bool operator < ( const Point &r) const { //点的坐标排序 return x < r.x || (x == r.x && y < r.y); } bool operator == ( const Point &r) const { //判断同一个点 return dcmp (x - r.x) == 0 && dcmp (y - r.y) == 0; } }; typedef Point Vector; //向量的定义 Point read_point( void ) { //点的读入 double x, y; scanf ( "%lf%lf" , &x, &y); return Point (x, y); } double dot(Vector A, Vector B) { //向量点积 return A.x * B.x + A.y * B.y; } double cross(Vector A, Vector B) { //向量叉积 return A.x * B.y - A.y * B.x; } double polar_angle(Vector A) { //向量极角 return atan2 (A.y, A.x); } double length(Vector A) { //向量长度,点积 return sqrt (dot (A, A)); } double angle(Vector A, Vector B) { //向量转角,逆时针,点积 return acos (dot (A, B) / length (A) / length (B)); } Vector rotate(Vector A, double rad) { //向量旋转,逆时针 return Vector (A.x * cos (rad) - A.y * sin (rad), A.x * sin (rad) + A.y * cos (rad)); } Vector nomal(Vector A) { //向量的单位法向量 double len = length (A); return Vector (-A.y / len, A.x / len); } Point line_line_inter(Point p, Vector V, Point q, Vector W) { //两直线交点,参数方程 Vector U = p - q; double t = cross (W, U) / cross (V, W); return p + V * t; } double point_to_line(Point p, Point a, Point b) { //点到直线的距离,两点式 Vector V1 = b - a, V2 = p - a; return fabs (cross (V1, V2)) / length (V1); } double point_to_seg(Point p, Point a, Point b) { //点到线段的距离,两点式 if (a == b) return length (p - a); Vector V1 = b - a, V2 = p - a, V3 = p - b; if (dcmp (dot (V1, V2)) < 0) return length (V2); else if (dcmp (dot (V1, V3)) > 0) return length (V3); else return fabs (cross (V1, V2)) / length (V1); } Point point_line_proj(Point p, Point a, Point b) { //点在直线上的投影,两点式 Vector V = b - a; return a + V * (dot (V, p - a) / dot (V, V)); } bool can_seg_seg_inter(Point a1, Point a2, Point b1, Point b2) { //判断线段相交,两点式 double c1 = cross (a2 - a1, b1 - a1), c2 = cross (a2 - a1, b2 - a1), c3 = cross (b2 - b1, a1 - b1), c4 = cross (b2 - b1, a2 - b1); return dcmp (c1) * dcmp (c2) < 0 && dcmp (c3) * dcmp (c4) < 0; } bool can_line_seg_inter(Point a1, Point a2, Point b1, Point b2) { //判断直线与线段相交,两点式 double c1 = cross (a2 - a1, b1 - a1), c2 = cross (a2 - a1, b2 - a1); return dcmp (c1 * c2) <= 0; } bool on_seg(Point p, Point a1, Point a2) { //判断点在线段上,两点式 return dcmp (cross (a1 - p, a2 - p)) == 0 && dcmp (dot (a1 - p, a2 - p)) < 0; } struct Line2 { double x1, x2; Line2 () {} Line2 ( double x1, double x2) : x1 (x1), x2 (x2) {} bool operator < ( const Line2 &r) const { return x1 < r.x1; } }; struct Line { Point a, b; Line () {} Line (Point a, Point b) : a (a), b (b) {} }; int main( void ) { Line h, p; double x1, x2, y; while ( scanf ( "%lf%lf%lf" , &x1, &x2, &y) == 3) { if (dcmp (x1) == 0 && dcmp (x2) == 0 && dcmp (y) == 0) break ; h.a = Point (x1, y); h.b = Point (x2, y); scanf ( "%lf%lf%lf" , &x1, &x2, &y); p.a = Point (x1, y); p.b = Point (x2, y); int n; scanf ( "%d" , &n); vector<Line2> xs; for ( int i=0; i<n; ++i) { scanf ( "%lf%lf%lf" , &x1, &x2, &y); if (dcmp (y - h.a.y) >= 0 || dcmp (y - p.a.y) <= 0) continue ; Point p1 = Point (x1, y), p2 = Point (x2, y); x1 = line_line_inter (h.b, h.b - p1, p.b, p.b - p.a).x; x2 = line_line_inter (h.a, h.a - p2, p.b, p.b - p.a).x; if (dcmp (x1 - x2) >= 0) continue ; xs.push_back (Line2 (x1, x2)); } xs.push_back (Line2 (p.b.x, p.b.x)); sort (xs.begin (), xs.end ()); double left = p.a.x, ans = 0; for ( int i=0; i<xs.size (); ++i) { x1 = xs[i].x1; x2 = xs[i].x2; if (x1 - left > ans) ans = x1 - left; if (dcmp (x2 - left) > 0) { left = x2; if (dcmp (left - p.b.x) > 0) break ; } } if (dcmp (ans) == 0) puts ( "No View" ); else printf ( "%.2f\n" , ans); } //cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; return 0; } |
编译人生,运行世界!
分类:
/* 圣人不死 AC不止!*/
, 数学
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)