赞 | 0 |
VIP | 135 |
好人卡 | 6 |
积分 | 3 |
经验 | 37799 |
最后登录 | 2020-4-30 |
在线时间 | 812 小时 |
Lv2.观梦者 旅之愚者
- 梦石
- 0
- 星屑
- 260
- 在线时间
- 812 小时
- 注册时间
- 2007-7-28
- 帖子
- 2148

|
本帖最后由 六祈 于 2010-11-1 23:52 编辑
于是跟上一个方案,接受批斗
- def find_rect(total_width, total_height, rect_width, rect_height, events)
- weight_array = Array.new(total_width * total_height){0}
- events.each{|event| add_weight(event, rect_width, rect_height)}
- max_index = weight_array.index weight_array.sort[-1]
- return max_index % total_width + 1, max_index / total_width + 1
- end
- def add_weight(event, rect_width, rect_height)
- index = event.x * total_width + event.y
- for i in 0...rect_width
- for j in 0...rect_height
- arr[index - i - j * total_width] += 1
- end
- end
- end
复制代码 算法思路:
构建各个初始点【将该点作为矩形左上点】的权重数组weight_array,并初始化所有元素为0;
迭代每一个事件,将被迭代事件作为矩形右下角,加成权重数组的元素;
取出权重数组的最大值索引,对total_width取余加1为x,除以total_width加1得到y |
评分
-
查看全部评分
|