围困

Time Limit: 1000 MS     Memory Limit: 65536 K

Total Submit: 360(138 users) Total Accepted: 157(129 users)   Rating:  Special Judge: No

Description

Leyni是一名猎人,一天他在草原中散步,遇到了三只老狼,三只老狼围成了一个三角形,如果Leyni在这个三角形中,那么他必死无疑,否则他还有一线生机。

现在已知三只老狼的坐标和Leyni的坐标,请问,Leyni还有活下去的机会吗?

Input

本题有多组测试数据,对于每组测试数据:

第一行,含有4对坐标,x1, y1, x2, y2, x3, y3, xLeyni, yLeyni。前3对坐标表示三只老狼的位置,最后一对表示Leyni的坐标。坐标的范围在[-10000, 10000],且都是整数。

注:输入数据保证Leyni不会在三角形的边上。

Output

对于每组测试数据:

第一行,如果Leyni还有一线生机,那么请输出Live,否则请输出Die

Sample Input

-1 -1 1 -1 0 1 0 0

-1 -1 1 -1 0 1 10 10

Sample Output

Die

Live

Author

齐达拉图


  计算几何,基础题,判断点是否在三角形内

  这道题可以用来练习叉积的应用,思路是如果Leyni位置的点与三个顶点构成的向量与三个边代表的向量(必须是一个顺序,都是逆时针,或都是顺时针)的叉积都大于0或者都小于0,则Leyni在三角形内。需要注意的是不能只考虑一种情况,例如只考虑>0的情况,而不考虑<0的情况,这样会WA。

  复杂代码

 #include <stdio.h>
#define eps 1e-10
typedef struct{ //定义点
double x,y;
}Point;
typedef Point Vector; //定义向量
double Cross(Vector a,Vector b) //叉积
{
return a.x*b.y - a.y*b.x;
}
bool isn(Point a,Point b,Point c,Point p) //判断点p是否在三角形abc内
{
Vector ab,bc,ca; //创建向量
Vector ap,bp,cp;
ab.x = b.x - a.x;ab.y = b.y - a.y; //向量赋值
bc.x = c.x - b.x;bc.y = c.y - b.y;
ca.x = a.x - c.x;ca.y = a.y - c.y;
ap.x = p.x - a.x;ap.y = p.y - a.y;
bp.x = p.x - b.x;bp.y = p.y - b.y;
cp.x = p.x - c.x;cp.y = p.y - c.y; if( Cross(ab,ap)>eps && Cross(bc,bp)>eps && Cross(ca,cp)>eps ||
Cross(ab,ap)<eps && Cross(bc,bp)<eps && Cross(ca,cp)<eps) //核心代码。注意不能单考虑一种情况(>0 或 <0)
return true;
else
return false;
}
int main()
{
int x1,y1,x2,y2,x3,y3,xLeyni,yLeyni;
while(scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3,&xLeyni,&yLeyni)!=EOF){ //输入四对坐标
if( (xLeyni==x1 && yLeyni==y1) ||
(xLeyni==x2 && yLeyni==y2) ||
(xLeyni==x3 && yLeyni==y3) ){
printf("Die\n"); //如果Leyni正好在三角形顶点上,则直接死掉
continue;
}
Point a,b,c,p; //将输入的坐标赋给点
a.x = double(x1),a.y = double(y1);
b.x = double(x2),b.y = double(y2);
c.x = double(x3),c.y = double(y3);
p.x = double(xLeyni),p.y = double(yLeyni); if(isn(a,b,c,p)) //判断是否在三角形内
printf("Die\n");
else
printf("Live\n");
}
return ;
}

  优化代码

 #include <stdio.h>
typedef struct { //定义点
double x,y;
}Point;
double Cross(Point a,Point b,Point c) //三点求叉积
{
return (b.x-a.x)*(c.y-a.y) - (b.y-a.y)*(c.x-a.x);
}
int main()
{
Point a,b,c,p; //定义四点
while(scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y,&p.x,&p.y)!=EOF){ //输入四对坐标
if( (Cross(a,b,p)> && Cross(b,c,p)> && Cross(c,a,p)>) ||
(Cross(a,b,p)< && Cross(b,c,p)< && Cross(c,a,p)<) ) //判断点p是否在三角形abc内。注意不能只考虑一种情况(<0 或 >0)
printf("Die\n");
else
printf("Live\n");
}
return ;
}

Freecode : www.cnblogs.com/yym2013

hrbustoj 1142:围困(计算几何基础题,判断点是否在三角形内)的更多相关文章

  1. hrbustoj 1429:凸多边形(计算几何,判断点是否在多边形内,二分法)

    凸多边形 Time Limit: 2000 MS    Memory Limit: 65536 K Total Submit: 130(24 users)   Total Accepted: 40(1 ...

  2. 2D空间中判断一点是否在三角形内

    要注意如果是XY坐标轴的2D空间,要取差乘分量z而不是y. 实现原理是,将三角形ABC三个边(AB,BC,CA)分别与比较点判断差乘,如果这3个差乘结果表示的方向一致,说明就在三角形内. 效果: 代码 ...

  3. 【TOJ 3005】Triangle(判断点是否在三角形内+卡精度)

    描述 Given the coordinates of the vertices of a triangle,And a point. You just need to judge whether t ...

  4. B - Toy Storage(POJ - 2398) 计算几何基础题,比TOYS多了个线段排序

    Mom and dad have a problem: their child, Reza, never puts his toys away when he is finished playing ...

  5. POJ 2398 - Toy Storage - [计算几何基础题][同POJ2318]

    题目链接:http://poj.org/problem?id=2398 Time Limit: 1000MS Memory Limit: 65536K Description Mom and dad ...

  6. POJ 2318 - TOYS - [计算几何基础题]

    题目链接:http://poj.org/problem?id=2318 Time Limit: 2000MS Memory Limit: 65536K Description Calculate th ...

  7. 算法复习——计算几何基础(zoj1081)

    题目: Statement of the Problem Several drawing applications allow us to draw polygons and almost all o ...

  8. hrbustoj 1306:再遇攻击(计算几何,判断点是否在多边形内,水题)

    再遇攻击 Time Limit: 1000 MS    Memory Limit: 65536 K Total Submit: 253(37 users)   Total Accepted: 56(2 ...

  9. nyis oj 68 三点顺序 (计算几何基础)

    三点顺序 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描写叙述 如今给你不共线的三个点A,B,C的坐标,它们一定能组成一个三角形,如今让你推断A,B,C是顺时针给出的还是逆 ...

随机推荐

  1. Android OpenGL 开发

    2013-06-30 Android OpenGL 开发 Android提供OpenGL包,专门用于3D的加速和渲染等. OpenGL, Open Graphics Library, 是一个专业的图形 ...

  2. 【linux】FTP添加用户,设置权限和目录

    一.目的,新建一个用户 test2,登录ftp,它只有自己的主目录权限,其他同级和上级目录没有权限 二.ftp安装.配置 yum -y install vsftpd //通过yum来安装vsftpd ...

  3. Java注解总结

    注解是Spring和Mybatis框架所大量使用的技术,要想掌握框架相关技术,注解是必须要掌握的. 掌握注解的优势: 1.能够读懂别人写的代码,特别是框架相关的代码. 2.本来可能需要很多配置文件,需 ...

  4. 数据库字符集与排序规则(Character Set And Collation)

    数据库需要适应各种语言和字符就需要支持不同的字符集(Character Set),每种字符集也有各自的排序规则(Collation). (注意:Collation原意为校对,校勘,但是根据实际使用场景 ...

  5. Python OS 文件

    Python OS 文件: 1 os.access(path, mode) 检验权限模式 2 os.chdir(path) 改变当前工作目录 3 os.chflags(path, flags) 设置路 ...

  6. 04-树7. Search in a Binary Search Tree (25)

    04-树7. Search in a Binary Search Tree (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 ...

  7. 解决Enter键与input 、a标签触发的事件的冲突

    无论是 <button type="button" onclick="console.log('123');">123</button> ...

  8. unity, 鼠标与场景交点

    在鼠标与场景交点上放一个mark,并于1s后消失: 新建一个空GameObject,命名为moushHitTest,添加下面脚本: using UnityEngine;using System.Col ...

  9. Spring Cloud概述

    Spring Cloud简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁 ...

  10. 安卓API首页

    http://developer.android.com/reference/packages.html