首页 > 其他分享 >UVa 143 Orchard Trees (数学&计算几何&枚举)

UVa 143 Orchard Trees (数学&计算几何&枚举)

时间:2023-04-12 10:32:48浏览次数:51  
标签:x1 143 Orchard int Trees x2 y1 y3 y2


143 - Orchard Trees

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=79

An Orchardist has planted an orchard in a rectangle with trees uniformly spaced in both directions. Thus the trees form a rectangular grid and we can consider the trees to have integer coordinates. The origin of the coordinate system is at the bottom left of the following diagram:

Consider that we now overlay a series of triangles on to this grid. The vertices of the triangle can have any real coordinates in the range 0.0 to 100.0, thus trees can have coordinates in the range 1 to 99. Two possible triangles are shown.

Write a program that will determine how many trees are contained within a given triangle. For the purposes of this problem, you may assume that the trees are of point size, and that any tree (point) lying exactly on the border of a triangle is considered to be in the triangle.

Input and Output

Input will consist of a series of lines. Each line will contain 6 real numbers in the range 0.00 to 100.00 representing the coordinates of a triangle. The entire file will be terminated by a line containing 6 zeroes (0 0 0 0 0 0).

Output will consist of one line for each triangle, containing the number of trees for that triangle right justified in a field of width 4.

Sample input

1.5 1.5  1.5 6.8  6.8 1.5
10.7 6.9  8.5 1.5  14.5 1.5
0 0 0 0 0 0


Sample output


15
  17



参考《入门经典》,注意输出要求。


完整代码:

/*0.179s*/

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

double area(double x1, double y1, double x2, double y2, double x3, double y3)//三角形有向面积的两倍
{
	return fabs(x1 * y3 + x2 * y1 + x3 * y2 - x2 * y3 - x3 * y1 - x1 * y2);
}

int main(void)
{
	double x1, y1, x2, y2, x3, y3;
	while (scanf("%lf%lf%lf%lf%lf%lf", &x1, &y1, &x2, &y2, &x3, &y3), x1 || y1 || x2 || y2 || x3 || y3)
	{
		int minx = max(1, (int)ceil(min(x1, min(x2, x3))));
		int maxx = min(99, (int)max(x1, max(x2, x3)));
		int miny = max(1, (int)ceil(min(y1, min(y2, y3))));
		int maxy = min(99, (int)max(y1, max(y2, y3)));
		int ans = 0;
		for (int i = minx; i <= maxx; i++)
			for (int j = miny; j <= maxy; j++)
			{
				double x = (double)i, y = (double)j;
				if (fabs(area(x1, y1, x2, y2, x, y) + area(x2, y2, x3, y3, x, y) + area(x3, y3, x1, y1, x, y) - area(x1, y1, x2, y2, x3, y3)) < 1e-8) //判断是否在三角形内或边界上
					ans++;
			}
		printf("%4d\n", ans);
	}
	return 0;
}



标签:x1,143,Orchard,int,Trees,x2,y1,y3,y2
From: https://blog.51cto.com/u_5535544/6185188

相关文章

  • nvim-treesitter coc.nvim
    PSC:\Users\dev\AppData\Local\nvim-data\plugged\nvim-treesitter\parser>lsdbash.socpp.sodockerfile.sohtml.solatex.soninja.sorust.sotypescript.soc.socss.sodot.sojava.sollvm......
  • The Forset NC14325
    link代码#include<bits/stdc++.h>usingnamespacestd;constintN=1010;//如果坏人可以到达终点,并且距离终点的距离小于等于起点到终点的距离,那么必然会相遇//所以我们从终点出发找起点,在找到起点之前如果到达坏人所在地方,就更新距离数组dintd[N][N];charg[N][N];......
  • POJ - 2029 Get Many Persimmon Trees(暴力水题)
    题目大意:给你一个矩阵,矩阵上面有N个柿子树,现在要求你画一个s*t的矩阵,使得这个矩阵内的柿子树达到最多解题思路:100*100,直接暴力#include<cstdio>#include<cstring>#include<algorithm>usingnamespacestd;constintN=110;intn,w,h,s,t;intmap[N][N];voidin......
  • jeesite #form中获取控件的值,以绑定的treeselect为例
    绑定部分代码:<divclass="form-group"><#form:formid="searchForm"model="${iotDevice}"action="${ctx}/iot/iotItem/listGroupData"method="post"class="form-inline"......
  • 1438. 绝对差不超过限制的最长连续子数组
    力扣题目链接给你一个整数数组 nums ,和一个表示限制的整数 limit,请你返回最长连续子数组的长度,该子数组中的任意两个元素之间的绝对差必须小于或者等于 limit 。如果不存在满足条件的子数组,则返回 0 。 示例1:输入:nums=[8,2,4,7],limit=4输出:2解释:所有子数组......
  • 202031603143-郭思彤 实验一 软件工程准备-对软件工程的初步了解
    项目内容班级博客链接20级卓越班本次作业要求链接实验一软件工程准备我的课程学习目标1.深入使用博客园进行学习2.了解Github的基本操作3.拓展关于软件工程的知识本次作业在哪些方面帮我实现学习目标1.学会了使用markdown编辑器的基本操作2.通过提问......
  • P5074 Eat the Trees
    P5074EattheTrees套着板子写,写了份四进制和二进制的四进制中与板子不同的是其实插头不需要区分左右了,之前左右匹配的情况可以消去插头继续转移注意特判全为0情况四进制代码:点击查看代码#include<bits/stdc++.h>#include<unordered_map>#defineintlonglong#define......
  • CSCI-1200 Simplified B+ Trees
    CSCI-1200DataStructures—Spring2023Homework8—SimplifiedB+TreesInthisassignmentwewillbeimplementingapartialandmodifiedversionofB+trees.......
  • CS143——第一章
    课程地址:Youtu视频:StanfordCS143CompilersIntrotoCompilers编译器和解释器编译器:offline离线输入:程序输出:exec过程:在对输入数据进行处理前不会对程序进行处理......
  • leetcode-1437-easy
    CheckIfAll1'sAreatLeastLengthKPlacesAwayGivenanbinaryarraynumsandanintegerk,returntrueifall1'sareatleastkplacesawayfromeachoth......