Project1
标题:
求一范围算法
[打印本页]
作者:
九夜神尊
时间:
2010-11-1 22:51
标题:
求一范围算法
SOU到了敌人AI 开发了,对于只有一个主角
来说还暂时不考虑这个,但是如果开发队友系统
并且队友也会群攻的话……
未命名.jpg
(34.43 KB, 下载次数: 3)
下载附件
保存到相册
2010-11-1 22:46 上传
这里有一个固定长宽的矩形框
求矩形框的位置,在这个位置内有最多的敌人数量。
作者:
六祈
时间:
2010-11-1 23:50
本帖最后由 六祈 于 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
作者:
bbaugle
时间:
2010-11-2 17:03
拜托..这里是XP提问区..
欢迎光临 Project1 (https://rpg.blue/)
Powered by Discuz! X3.1