python如何实现最小矩形覆盖问题
作者:椰子奶糖
这篇文章主要介绍了python如何实现最小矩形覆盖问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
python实现最小矩形覆盖
Description
给定一些点的坐标,要求求能够覆盖所有点的最小面积的矩形,
输出所求矩形的面积和四个顶点坐标
Input
第一行为一个整数n(3<=n<=50000)
从第2至第n+1行每行有两个浮点数,表示一个顶点的x和y坐标,不用科学计数法
Output
第一行为一个浮点数,表示所求矩形的面积(精确到小数点后5位),
接下来4行每行表示一个顶点坐标,要求第一行为y坐标最小的顶点,
其后按逆时针输出顶点坐标.如果用相同y坐标,先输出最小x坐标的顶点
Sample Input
6 1.0 3.00000 1 4.00000 2.0000 1 3 0.0000 3.00000 6 6.0 3.0
Sample Output
18.00000 3.00000 0.00000 6.00000 3.00000 3.00000 6.00000 0.00000 3.00000
实际上它我们py老师布置的小作业,用py写
编写一个平面二维点集类,要求这个类的实例对象(也就是一个点)能够计算到另一个点的距离;再编写一个函数,能够计算覆盖一系列点的最小矩形
思路:找到凸包->旋转卡壳->计算顶点->输出
建议用编辑器打开,个人不是很喜欢简书这里的代码高亮风格
from math import sqrt
import random
import math
class Point(object):
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def distance_to(self, other):
dx = self.x - other.x
dy = self.y - other.y
return sqrt(dx ** 2 + dy ** 2)
# 计算两点相对于X轴的(cos)
def angle_cos(self, other):
# cos = dx/dis(self,other)
cos = (other.x - self.x)/self.distance_to(other)
return cos
def __str__(self):
return '(%s, %s)' % (str(self.x), str(self.y))
# 找到极坐标系原点
def get_bottom_point(points):
bot_point = points[0]
temp = 0
for i in range(1, len(points)):
# 找到最左下角的点作为极坐标系原点
if bot_point.y > points[i].y or (bot_point.y == points[i].y and bot_point.x > points[i].x):
bot_point = points[i]
temp = i
# 删除作为原点的那个点
del(points[temp])
return bot_point, points
# 极坐标排序,cos,从大到小
def sort_polar_angle_cos(points, bot_point):
dic = dict()
for point in points:
dic[bot_point.angle_cos(point)] = point
# for key,value in dic.items():
# print("{}:{}".format(key,value))
# for key ,value in [(k,dic[k]) for k in sorted(dic.keys(),reverse=True)]:
# print("{}::::::{}".format(key,value))
return [dic[k] for k in sorted(dic.keys(), reverse=True)]
# 叉积
def cross_product(p1, p2, p3):
x1 = p2.x-p1.x
y1 = p2.y-p1.y
x2 = p3.x-p1.x
y2 = p3.y-p1.y
return (x1*y2-x2*y1)
# Graham扫描法计算凸包
def graham_scan(points, bot_point):
# 凸包列表,先加前三个
con_list = []
con_list.append(bot_point)
con_list.append(points[0])
con_list.append(points[1])
# 寻找其他凸包上的点
for i in range(2, len(points)-1):
cro = cross_product(con_list[-2], con_list[-1], points[i])
if cro > 0:
con_list.append(points[i])
elif cro < 0:
con_list.pop()
con_list.append(points[i])
# 最后一个点也一定在凸包中
con_list.append(points[-1])
# # 打印所有凸包点坐标
# for each in con_list:
# print(each)
return con_list
# 找到最小矩形
def find_min_ret(con_list):
rec_area = 10000
rec_height = 0
rec_dot = []
rec_po = []
f_po = con_list[0]
for i in range(1, len(con_list)):
max_po = 0
min_po = 0
# 最大三角形面积,用于求高
max_height = 0
# 底边长
bot_length = 0
# 最大点积与最小点积
max_dot = 0
min_dot = 10000
s_po = con_list[i]
for t_po in con_list:
# 需要改
height = get_area_by_po(f_po, s_po, t_po)/f_po.distance_to(s_po)
# print("height={}".format(height))
if height > max_height:
max_height = height
# 点积求投影长度,同样存最值?俩?
# 向量w
x1 = (s_po.x - f_po.x)
y1 = (s_po.y - f_po.y)
# 向量v
x2 = (t_po.x - f_po.x)
y2 = (t_po.y - f_po.y)
# 点积
dot = x1*x2+y1*y2
# print("dot={}".format(dot/f_po.distance_to(s_po)))
if dot > max_dot:
max_dot = dot
max_po = t_po
if dot < min_dot:
min_dot = dot
min_po = t_po
# 由于是遍历,故此min(max_dot) = 底边^2 max(min_dot) = 0
bot_length = (max_dot-min_dot)/f_po.distance_to(s_po)
# 矩形面积 = 底*高
area = bot_length*max_height
if rec_area > area:
rec_area = area
# 记录疑似最小矩形的信息
rec_height = max_height
rec_po = [f_po, s_po, max_po, min_po]
rec_dot = [max_dot, min_dot]
# 下一轮
f_po = s_po
# # 根据数据计算顶点坐标
# for each in rec_po:
# print("rec_po={}".format(each))
# print("rec_dot={}\nrec_height={}".format(rec_dot,rec_height))
rec_po = get_point_list(rec_po, rec_dot, rec_height)
return round(rec_area,10), rec_po
# 叉积求四边形面积
def get_area_by_po(p1, p2, p3):
# 则平行四边形面积=[(x2y3-x3y2)-(x1y3-x3y1)+(x1y2-x2y1)]
return (p2.x*p3.y-p3.x*p2.y) - (p1.x*p3.y-p3.x*p1.y) + (p1.x*p2.y-p2.x*p1.y)
# 得到矩形顶点坐标
def get_point_list(rec_po, rec_dot, rec_height):
# rec_height = max_height
# rec_po = [f_po, s_po, max_po, min_po]
# rec_dot = [max_dot, min_dot]
x1 = rec_po[1].x - rec_po[0].x
y1 = rec_po[1].y - rec_po[0].y
# print("max_dot={}\nmin_dot={}".format(rec_dot[0],rec_dot[1]))
# 这么算会存在精度问题,可用round(rec_area,10)函数解决
# len_x1y1 = rec_po[0].distance_to(rec_po[1])
# a1 = x1*rec_dot[0]/math.pow(len_x1y1,2)+rec_po[0].x
# b1 = y1*rec_dot[0]/math.pow(len_x1y1,2)+rec_po[0].y
# print("{}*{}/{}+{}={}".format(x1,rec_dot[0],math.pow(len_x1y1,2),rec_po[0].x,a1))
# print("{}*{}/{}+{}={}".format(y1,rec_dot[0],math.pow(len_x1y1,2),rec_po[0].y,b1))
len_x1y1 = math.pow(rec_po[0].x-rec_po[1].x,2)+math.pow(rec_po[0].y-rec_po[1].y,2)
a1 = x1*rec_dot[0]/len_x1y1+rec_po[0].x
b1 = y1*rec_dot[0]/len_x1y1+rec_po[0].y
p1 = Point(a1,b1)
a2 = x1*rec_dot[1]/len_x1y1+rec_po[0].x
b2 = y1*rec_dot[1]/len_x1y1+rec_po[0].y
p2 = Point(a2,b2)
x2 = rec_po[2].x - a1
y2 = rec_po[2].y - b1
a3 = x2*rec_height/p1.distance_to(rec_po[2])+a1
b3 = y2*rec_height/p1.distance_to(rec_po[2])+b1
p3 = Point(a3,b3)
a4 = a3+a2-a1
b4 = b3+b2-b1
p4 = Point(a4,b4)
# print("__________{}____________________".format(p1))
# print("__________{}____________________".format(p2))
# print("__________{}____________________".format(p3))
# print("__________{}____________________".format(p4))
rec_po = [p1,p2,p3,p4]
return rec_po
if __name__ == '__main__':
points = []
# for i in range(0, 10):
# points.append(Point(random.randint(1, 10), random.randint(1, 10)))
# 使用算法题中的数据,便于验证
points.append(Point(1.0, 3.00000))
points.append(Point(1, 4.00000))
points.append(Point(2.0000, 1))
points.append(Point(3, 0.0000))
points.append(Point(3.00000, 6))
points.append(Point(6.0, 3.0))
# for each in points:
# print(each)
bot_point, points = get_bottom_point(points)
points = sort_polar_angle_cos(points, bot_point)
# # 拿到了角度排序的值
# for each in points :
# print(each)
# print()
# 拿到凸包集合
con_list = graham_scan(points, bot_point)
# 旋转卡壳计算面积
rec_area, rec_po = find_min_ret(con_list)
print("rec_area={}".format(rec_area))
for each in rec_po:
print("{}".format(each))注意:
这个对于我这种非ACM的菜鸡来说还是费了劲了,写完仍然存在一些小问题,比如极坐标排序没有去重,存在一些精度的问题,需要取整函数的帮助。。。。
python矩形覆盖问题
题目:

思路:
递归,用列表s[]来存储覆盖方法的个数
n=1时,s[0]=1
n=2时,s[1]=2
n=3时,此时分为两个不重复的覆盖方法
1+2:s[0]*s[1]
2+1: (s[1]-1)*s[0] %减一是为了不计算重复覆盖方法
n=4时,分为两种
1+3:s[0]*s[2]
2+2:(s[1]-1)*s[1]
…
以此类推,可得为n时的覆盖方法s[n-1]=s[0]*s[-1]+(s[1]-1)*s[-2]
python代码:
# -*- coding:utf-8 -*-
class Solution:
def rectCover(self, number):
# write code here
l=[1,2]
if number==0:
return 0
while len(l)<number:
t=len(l)
s=l[0]*l[-1]+(l[1]-1)*l[-2]
l.append(s)
return l[number-1]总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
