算法成长之路------CF6A Triangle

学习目标:算法学习-Day8

题库: 洛谷题库
每天保持发布一篇Java或C算法题解!

题目
给定 4 根木棍的长度,如果它们中存在 3 根木棍可以组成三角形,输出 TRIANGLE ;如果它们无法组成三角形,但是它们中存在 3 根木棍可以组成退化的三角形(任意两边之和大于等于第三边,但是不是三角形),输出 SEGMENT ;否则,输出 IMPOSSIBLE 。

注意: 木棍不能折断,也不能只用一部分长度。

输入格式:
一行 4 个整数,44 根木棍的长度。

输出格式:
如果它们中存在 3 根木棍可以组成三角形,输出 TRIANGLE ;如果它们无法组成三角形,但是它们中存在3根木棍可以组成退化的三角形,输出 SEGMENT ;否则,输出 IMPOSSIBLE。

样例 1 :

输入:
4 2 1 3


输出:


TRIANGLE
样例 2 :

输入:
7 2 2 4


输出:
SEGMENT


样例 3 :

输入:
3 5 9 1


输出:
IMPOSSIBLE


思路:

	题目有两种三角形,一种输出TRIANGLE,另一种输出SEGMENT。
	给了4根木棍,其中任意3根组成三角形。
	如果第一根木棍和第二根木棍的总长大于第四根,那么就一定大于第三根,
	所以只要判断第一根木棍和第二根木棍的总长与第三根的关系。
	同理,如果第一根木棍和第三根木棍的总长大于第四根,那么第二根木棍和第三根木棍的总长也一定大于第四根
	所以只要判断第二根木棍和第三根木棍的总长与第四根的关系。
	也就是说退化的三角形也一样判断。

习题总结及反思:

	Arrays类的sort()方法,用于排序。

代码如下:

import java.util.*<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是基于Bowyer-Watson算法的Python代码实现: ``` import math class Vertex: def __init__(self, x, y): self.x = x self.y = y def __eq__(self, other): return self.x == other.x and self.y == other.y def __hash__(self): return hash((self.x, self.y)) class Triangle: def __init__(self, vertex1, vertex2, vertex3): self.vertices = [vertex1, vertex2, vertex3] def __eq__(self, other): return set(self.vertices) == set(other.vertices) def __hash__(self): return hash((self.vertices[0], self.vertices[1], self.vertices[2])) def get_circumcenter(self): A = self.vertices[0] B = self.vertices[1] C = self.vertices[2] a = (B.x - A.x)**2 + (B.y - A.y)**2 b = (C.x - B.x)**2 + (C.y - B.y)**2 c = (A.x - C.x)**2 + (A.y - C.y)**2 s = 2 * (a * b + b * c + c * a) - (a**2 + b**2 + c**2) px = (a * (b + c - a) * A.x + b * (c + a - b) * B.x + c * (a + b - c) * C.x) / s py = (a * (b + c - a) * A.y + b * (c + a - b) * B.y + c * (a + b - c) * C.y) / s return Vertex(px, py) class DelaunayTriangulation: def __init__(self, vertices): self.vertices = vertices self.triangles = [] # 计算超级三角形 max_x = max(vertex.x for vertex in self.vertices) max_y = max(vertex.y for vertex in self.vertices) min_x = min(vertex.x for vertex in self.vertices) min_y = min(vertex.y for vertex in self.vertices) dx = max(max_x - min_x, max_y - min_y) mid_x = min_x + dx / 2 mid_y = min_y + dx / 2 self.super_triangle = Triangle( Vertex(mid_x - 20 * dx, mid_y - dx), Vertex(mid_x, mid_y + 20 * dx), Vertex(mid_x + 20 * dx, mid_y - dx) ) self.triangles.append(self.super_triangle) # 插入所有的点 for vertex in self.vertices: bad_triangles = [] for triangle in self.triangles: if self.in_circumcircle(triangle, vertex): bad_triangles.append(triangle) polygon = [] for triangle in bad_triangles: for v in triangle.vertices: if v not in vertex and self.is_edge_shared_by_triangles(v, vertex, bad_triangles): polygon.append((v, vertex)) for triangle in bad_triangles: self.triangles.remove(triangle) for edge in polygon: self.triangles.append(Triangle(edge[0], edge[1], vertex)) def in_circumcircle(self, triangle, vertex): circle_center = triangle.get_circumcenter() radius = math.sqrt((circle_center.x - triangle.vertices[0].x)**2 + (circle_center.y - triangle.vertices[0].y)**2) distance = math.sqrt((circle_center.x - vertex.x)**2 + (circle_center.y - vertex.y)**2) return distance <= radius def is_edge_shared_by_triangles(self, vertex1, vertex2, triangles): edge_count = 0 for triangle in triangles: if vertex1 in triangle.vertices and vertex2 in triangle.vertices: edge_count += 1 return edge_count == 2 def get_triangles(self): triangles = [] for triangle in self.triangles: if not any(vertex in self.super_triangle.vertices for vertex in triangle.vertices): triangles.append(triangle) return triangles ``` 该代码使用了三角形和顶点的类来表示数据,以及Bowyer-Watson算法来进行三角剖分。在主函数,可以通过创建一个DelaunayTriangulation对象并传入一个顶点列表来获取三角形列表。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值