题目传送门

题意:给了若干个图形,问每个图形与哪些图形相交

分析:题目说白了就是处理出每个图形的线段,然后判断是否相交。但是读入输出巨恶心,就是个模拟题加上线段相交的判断,我第一次WA不知道输出要按字母序输出,第二次WA是因为忘记多边形的最后一条线段,还好找到了,没有坚持的话就不会AC了。

/************************************************
* Author :Running_Time
* Created Time :2015/10/31 星期六 13:38:11
* File Name :POJ_3449.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;
}
double area_triangle(Point a, Point b, Point c) { //三角形面积,叉积
return fabs (cross (b - a, c - a)) / 2.0;
}
double area_poly(Point *p, int n) { //多边形面积,叉积
double ret = 0;
for (int i=1; i<n-1; ++i) {
ret += fabs (cross (p[i] - p[0], p[i+1] - p[0]));
}
return ret / 2;
}
struct Seg {
Point a, b;
Seg () {}
Seg (Point a, Point b) : a (a), b (b) {}
};
struct Pic {
char ch;
int cnt;
vector<Seg> seg;
vector<int> ans;
bool operator < (const Pic &r) const {
return ch < r.ch;
}
}p[33], pp[33]; bool cmp(int i, int j) {
return p[i].ch < p[j].ch;
} void run(int tot) {
for (int i=0; i<tot; ++i) {
p[i].cnt = 0;
for (int j=0; j<tot; ++j) {
if (i == j) continue;
int sz1 = p[i].seg.size (), sz2 = p[j].seg.size ();
bool flag = false;
for (int ii=0; ii<sz1; ++ii) {
for (int jj=0; jj<sz2; ++jj) {
if (can_seg_seg_inter (p[i].seg[ii].a, p[i].seg[ii].b, p[j].seg[jj].a, p[j].seg[jj].b)) {
flag = true; p[i].cnt++; p[i].ans.push_back (j);
break;
}
}
if (flag) break;
}
}
sort (p[i].ans.begin (), p[i].ans.end (), cmp);
}
} int main(void) {
//freopen ("POJ_3449.in", "r", stdin);
//freopen ("POJ_3449.out", "w", stdout);
char str[22];
int x[22], y[22];
int tot = 0;
int x1, y1, x2, y2, x3, y3, x4, y4;
Point p1, p2, p3, p4;
while (scanf ("%s", &str) == 1) {
if (strcmp (str, ".") == 0) break;
else if (strcmp (str, "-") == 0) {
run (tot);
for (int i=0; i<tot; ++i) pp[i] = p[i];
sort (p, p+tot);
for (int i=0; i<tot; ++i) {
if (p[i].cnt == 0) {
printf ("%c has no intersections\n", p[i].ch);
}
else {
if (p[i].cnt == 1) {
printf ("%c intersects with %c\n", p[i].ch, pp[p[i].ans[0]].ch);
}
else if (p[i].cnt == 2) {
printf ("%c intersects with %c and %c\n", p[i].ch, pp[p[i].ans[0]].ch, pp[p[i].ans[1]].ch);
}
else {
printf ("%c intersects with %c", p[i].ch, pp[p[i].ans[0]].ch);
int sz = p[i].ans.size ();
for (int j=1; j<sz-1; ++j) {
printf (", %c", pp[p[i].ans[j]].ch);
}
printf (", and %c\n", pp[p[i].ans[sz-1]].ch);
}
}
p[i].ans.clear (); p[i].seg.clear ();
}
puts ("");
tot = 0; continue;
} p[tot].ch = str[0];
scanf ("%s", &str);
if (str[0] == 's') {
scanf (" (%d,%d) (%d,%d)", &x1, &y1, &x3, &y3);
//cal x2 x4
p1 = Point (x1, y1), p3 = Point (x3, y3), p2, p4;
Vector V = p3 - p1;
double len = length (V);
p2 = p1 + rotate (V, PI / 4) / sqrt (2.0);
p4 = p1 + rotate (V, -PI / 4) / sqrt (2.0);
p[tot].seg.push_back (Seg (p1, p2));
p[tot].seg.push_back (Seg (p2, p3));
p[tot].seg.push_back (Seg (p3, p4));
p[tot].seg.push_back (Seg (p4, p1));
}
else if (str[0] == 'l') {
scanf (" (%d,%d) (%d,%d)", &x1, &y1, &x2, &y2);
p[tot].seg.push_back (Seg (Point (x1, y1), Point (x2, y2)));
}
else if (str[0] == 't') {
scanf (" (%d,%d) (%d,%d) (%d,%d)", &x1, &y1, &x2, &y2, &x3, &y3);
p[tot].seg.push_back (Seg (Point (x1, y1), Point (x2, y2)));
p[tot].seg.push_back (Seg (Point (x2, y2), Point (x3, y3)));
p[tot].seg.push_back (Seg (Point (x3, y3), Point (x1, y1)));
}
else if (str[0] == 'p') {
int n;
scanf ("%d", &n);
for (int i=0; i<n; ++i) {
scanf (" (%d,%d)", &x[i], &y[i]);
}
for (int i=0; i<n-1; ++i) {
p[tot].seg.push_back (Seg (Point (x[i], y[i]), Point (x[i+1], y[i+1])));
p[tot].seg.push_back (Seg (Point (x[n-1], y[n-1]), Point (x[0], y[0]))); //忘记加了
}
}
else if (str[0] == 'r') {
scanf (" (%d,%d) (%d,%d) (%d,%d)", &x1, &y1, &x2, &y2, &x3, &y3);
//cal x4
p1 = Point (x1, y1), p3 = Point (x3, y3), p2 = Point (x2, y2), p4;
p4 = p1 + (p3 - p2);
p[tot].seg.push_back (Seg (p1, p2));
p[tot].seg.push_back (Seg (p2, p3));
p[tot].seg.push_back (Seg (p3, p4));
p[tot].seg.push_back (Seg (p4, p1));
}
tot++;
} //cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; return 0;
}

  

简单几何(线段相交)+模拟 POJ 3449 Geometric Shapes的更多相关文章

  1. 简单几何(线段相交) POJ 2653 Pick-up sticks

    题目传送门 题意:就是小时候玩的一种游戏,问有多少线段盖在最上面 分析:简单线段相交,队列维护当前最上的线段 /******************************************** ...

  2. POJ 3449 Geometric Shapes(判断几个不同图形的相交,线段相交判断)

    Geometric Shapes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1243   Accepted: 524 D ...

  3. 简单几何(线段相交) POJ 1410 Intersection

    题目传送门 题意:一个矩形和一条线段,问是否有相交 分析:考虑各种情况.坑点:给出的矩形的两个端点是无序的,还有线段完全在矩形内也算相交 /****************************** ...

  4. 简单几何(线段相交) POJ 1066 Treasure Hunt

    题目传送门 题意:从四面任意点出发,有若干障碍门,问最少要轰掉几扇门才能到达终点 分析:枚举入口点,也就是线段的两个端点,然后选取与其他线段相交点数最少的 + 1就是答案.特判一下n == 0的时候 ...

  5. 简单几何(线段相交+最短路) POJ 1556 The Doors

    题目传送门 题意:从(0, 5)走到(10, 5),中间有一些门,走的路是直线,问最短的距离 分析:关键是建图,可以保存所有的点,两点连通的条件是线段和中间的线段都不相交,建立有向图,然后用Dijks ...

  6. POJ 3449 Geometric Shapes 判断多边形相交

    题意不难理解,给出多个多边形,输出多边形间的相交情况(嵌套不算相交),思路也很容易想到.枚举每一个图形再枚举每一条边 恶心在输入输出,不过还好有sscanf(),不懂可以查看cplusplus网站 根 ...

  7. POJ 3449 Geometric Shapes (求正方形的另外两点)

    Geometric Shapes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1470   Accepted: 622 D ...

  8. POJ 3449 Geometric Shapes --计算几何,线段相交

    题意: 给一些多边形或线段,输出与每一个多边形或线段的有哪一些多边形或线段. 解法: 想法不难,直接暴力将所有的图形处理成线段,然后暴力枚举,相交就加入其vector就行了.主要是代码有点麻烦,一步一 ...

  9. 简单几何(线段相交) POJ 2826 An Easy Problem?!

    题目传送门 题意:两条线段看成两块木板,雨水从上方往下垂直落下,问能接受到的水的体积 分析:恶心的分类讨论题,考虑各种情况,尤其是入口被堵住的情况,我的方法是先判断最高的两个点是否在交点的同一侧,然后 ...

随机推荐

  1. 在线调试和演示的前端开发工具------http://jsfiddle.net/

    在线调试和演示的前端开发工具------http://jsfiddle.net/

  2. Nmap备忘单:从探索到漏洞利用(Part 4)

    这是我们的Nmap备忘单的第四部分(Part 1. Part 2. Part 3).本文中我们将讨论更多东西关于扫描防火墙,IDS / IPS 逃逸,Web服务器渗透测试等.在此之前,我们应该了解一下 ...

  3. poj2996 模拟

    Help Me with the Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3713   Accepted:  ...

  4. LocalResizeIMG前端HTML5本地压缩图片上传,兼容移动设备IOS,android

    LocalResizeIMG前端HTML5本地压缩图片上传,兼容移动设备IOS,android jincon 发表于 2015-02-26 18:31:01 发表在: php开发 localresiz ...

  5. Java构造方法的含义和使用

    我们实例化对象时,一般使用"类名 对象名 = new 类名()"来实例化,其实这样并不是十分严谨,只是编译器帮我们自动补全了一个空的构造方法,当实例化对象时,构造方法会被自动调用, ...

  6. Extjs读取本地下拉选框数据源,分为text和value,显示text,传值value

    this.rdTypeCom=new Ext.form.ComboBox({              hiddenName:'rdType',              store:new Ext. ...

  7. CodeForces - 407A

    Triangle Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Sta ...

  8. 获取4G以上的文件大小

    1.DWORD dwFileSizeHigh;  // 得到文件大小的高位  __int64 qwFileSize = GetFileSize(m_hSrcBigFile, &dwFileSi ...

  9. docke跨主机通信之gre隧道

    GRE简介 GRE可以对网络层的任何协议来进行封装,类似LVS的IPIP协议,在原有的数据报上增加GRE协议数据报.然后在网络上传输,到达对端后,解开GRE数据报头,得到真实的数据报.其中的mac地址 ...

  10. css3学习总结3--CSS3图像边框

    border-image属性 .className{ border-image:url(/course/54d1cae088dba03f2cd1fec1/img/border.png) 20 20 2 ...