UVa 143 - Orchard Trees
题目大意:果园里的树排列成矩阵,它们的x和y坐标均是1~99的整数。输入若干三角形,依次统计每一个三角形内部和边界上共有多少棵树。
三角形P0P1P2有向面积为A:2A = x0y1 + x2y0 + x1y2 - x2y1 - x0y2 - x1y0。如果三角形的三个顶点呈逆时针排列,那么有向面积为正,如果是顺时针排列,则有向面积为负。假设输入三角形为ABC,待判断点为O,则O在三角形ABC内部或边界上当且仅当SABC = SOAB + SOBC + SOAC。通过对有向面积取绝对值可以避免三个顶点是否是逆时针的判断,同时要注意计算x、y的最大值和最小值时要限制在[1, 99]的范围内。
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
#define EPS 1e-9 double area2(double x0, double y0, double x1, double y1, double x2, double y2)
{
return fabs(x0*y1 + x2*y0 + x1*y2 - x2*y1 - x0*y2 - x1*y0);
} int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
double x1, x2, x3, y1, y2, y3;
while (scanf("%lf%lf%lf%lf%lf%lf", &x1, &y1, &x2, &y2, &x3, &y3) != EOF)
{
if ((x1 || x2 || x3 || y1 || y2 || y3) == ) break;
int x_min = ceil(min(x1, min(x2, x3)));
x_min = max(, x_min);
int x_max = max(x1, max(x2, x3));
x_max = min(, x_max);
int y_min = ceil(min(y1, min(y2, y3)));
y_min = max(, y_min);
int y_max = max(y1, max(y2, y3));
y_max = min(, y_max);
double S = area2(x1, y1, x2, y2, x3, y3);
int ans = ;
for (int x = x_min; x <= x_max; x++)
for (int y = y_min; y <= y_max; y++)
{
double S1 = area2(x1, y1, x2, y2, x, y);
double S2 = area2(x2, y2, x3, y3, x, y);
double S3 = area2(x3, y3, x1, y1, x, y);
if (fabs(S-S1-S2-S3) < EPS) ans++;
}
printf("%4d\n", ans);
}
return ;
}
在取最大值和最小值时,如果min向下取整,max用ceil向上取整,那么考察的范围将会扩大,按理说应该没什么影响,可是那样的话会WA,为什么呢?唉,难懂的浮点数啊...
UVa 143 - Orchard Trees的更多相关文章
- UVA - 143 Orchard Trees (点在三角形内)
题意: 给出三角形的三个点的坐标(浮点数), 问落在三角形内及三角形边上的整点有多少? 思路:所有点暴力判断(点的范围1-99,三角形可能是0-100,因为这个WA了一下orz) AC代码: ...
- hud 1633 Orchard Trees 点是否在三角形内模板 *
Orchard Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- 【例题 6-7 UVA - 122 】Trees on the level
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 二叉树的话,直接用数组存就好了. 写个bfs记录一下答案. [代码] #include <bits/stdc++.h> ...
- UVa 10562 Undraw the Trees 看图写树
转载请注明: 仰望高端玩家的小清新 http://www.cnblogs.com/luruiyuan/ 题目大意: 题目传送门:UVa 10562Undraw the Trees 给定字符拼成的树,将 ...
- UVA题解三
UVA题解三 UVA 127 题目描述:\(52\)张扑克牌排成一列,如果一张牌的花色或者数字与左边第一列的最上面的牌相同,则将这张牌移到左边第一列的最上面,如果一张牌的花色或者数字与左边第三列的最上 ...
- HOJ题目分类
各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...
- Uva 10007 / HDU 1131 - Count the Trees (卡特兰数)
Count the Trees Another common social inability is known as ACM (Abnormally Compulsive Meditation) ...
- UVA.122 Trees on the level(二叉树 BFS)
UVA.122 Trees on the level(二叉树 BFS) 题意分析 给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete 代码总览 #include ...
- Trees on the level UVA - 122 复习二叉树建立过程,bfs,queue,strchr,sscanf的使用。
Trees are fundamental in many branches of computer science (Pun definitely intended). Current state- ...
随机推荐
- linux找回密码
开机启动系统,在进入系统之前按键盘上面的Esc键,会进入下面的界面 按键盘上的e键,出现下面界面 用键盘上的方向键移动光标到第二项,然后再按键盘上面的e,会出现下面的界面 输入一个空格键,然后在输入数 ...
- C#获取客服端ip和用户名
. 在asp.Net中专用属性: 获取服务器电脑名:page.server.manchinename 获取用户信息:page.user 获取客户端电脑名:page.request.userhostna ...
- Tree of Life (easy)
Tree of Life (easy) Heidi has finally found the mythical Tree of Life – a legendary combinatorial st ...
- mkconfig文件解析
#!/bin/sh -e #mkconfig 100ask24x0 arm arm920t 100ask24x0 Null s3c24x0#s0 s1 ...
- Bootstrap 容器(Container)及网格说明-(二)
1.容器(Container) 2.网格 来自为知笔记(Wiz)
- libevent和libev的区别对比(二)
之前有一篇文章描述过一些对比: http://www.cnblogs.com/charlesblc/p/6078029.html 这里在代码和应用方面再说一下. 看一下两边的Helloworld基本就 ...
- [转]:如何使用Android Studio把自己的Android library分享到jCenter和Maven Central
http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0623/3097.html
- java中基本数据类型和C语言中基本数据类型转换
java中 1 short = 2 byte 1 char = 2 byte 1 int = 4 byte 1 long = 8 byte C语言中 typedef unsigned char ...
- 转发:iOS之textfield用法大全
转发至:http://m.blog.csdn.net/article/details?id=8121915 //初始化textfield并设置位置及大小 UITextField *text = [[U ...
- openstack controller ha测试环境搭建记录(十五)——创建实例
# source demo-openrc.sh # ssh-keygenGenerating public/private rsa key pair.Enter file in which to sa ...