1 回答
TA贡献1775条经验 获得超11个赞
您可以使用属性来判断一个点是有效的upperLeft,upperRight,lowerLeft,或lowerRight点。
这是使用该信息的可能算法:
创建列表upperLeft,upperRight,lowerLeft,或lowerRight点。
// Attributes for inclusion:
// upperLeft === (toRight && down)
// upperRight === (toLeft && down)
// lowerLeft === (toRight && up)
// lowerRight === (toLeft && up)
for pt in points
if pt.toRight
if pt.down
upperLeft.append(pt)
end
if pt.up
lowerLeft.append(pt)
end
end
if pt.toLeft
if pt.down
upperRight.append(pt)
end
if pt.up
lowerRight.append(pt)
end
end
end
注意: a)有些点属于所有4个列表,因此不要else-if用于任何比较b)41个点中,只有26个属于每个列表
现在,通过从4个列表中选择点来找到正方形。如果您一直等到选择了一个潜在正方形的所有4个角,则将检查26*26*26*26 (456,976)正方形。您可以拒绝无法立即解决的问题,从而加快流程。例如,一旦有了左上点,便知道右上点必须具有相同的垂直(y值),并且右上值必须在左上值的右边。选择左下点时,它应位于左上点下方(相同的x值),并且距左上点的距离应与左上点距右上点的距离相同(因为寻找正方形)。当您选择右下角点时,它应与左下角点(相同的y值)和右上角点(相同的x值)对齐。
squares = 0
for ul in upperLeft
for ur in upperRight
next if ur is not at same vertical and to the right of ul
for ll in lowerLeft
next if ll is not at same horizontal and below ul
next if dist(ul,ll) != dist(ul,ur) // looking for squares
for lr in lowerRight
next if lr not at same vertical as ll
next if lr not at same horizontal as ur
squares += 1
end
end
end
end
这是我用Swift编写的解决方案:
typealias GridPoint = (x: Int, y: Int, left: Bool, right: Bool, up: Bool, down: Bool)
let points: [GridPoint] = [
(0, 0, false, true, false, true),
(2, 0, true, true, false, true),
(4, 0, true, true, false, true),
(6, 0, true, true, false, true),
(8, 0, true, false, false, true),
(0, 2, false, true, true, true),
(2, 2, true, true, true, true),
(4, 2, true, true, true, true),
(6, 2, true, true, true, true),
(8, 2, true, false, true, true),
(0, 4, false, true, true, true),
(2, 4, true, true, true, true),
(4, 4, true, true, true, true),
(6, 4, true, true, true, true),
(8, 4, true, false, true, true),
(0, 6, false, true, true, true),
(2, 6, true, true, true, true),
(4, 6, true, true, true, true),
(6, 6, true, true, true, true),
(8, 6, true, false, true, true),
(0, 8, false, true, true, false),
(2, 8, true, true, true, false),
(4, 8, true, true, true, false),
(6, 8, true, true, true, false),
(8, 8, true, false, true, false),
(3, 1, false, true, false, true),
(4, 1, true, true, true, true),
(5, 1, true, false, false, true),
(3, 2, true, true, true, true),
(5, 2, true, true, true, true),
(3, 3, false, true, true, false),
(4, 3, true, true, true, true),
(5, 3, true, false, true, false),
(3, 5, false, true, false, true),
(4, 5, true, true, true, true),
(5, 5, true, false, false, true),
(3, 6, true, true, true, true),
(5, 6, true, true, true, true),
(3, 7, false, true, true, false),
(4, 7, true, true, true, true),
(5, 7, true, false, true, false),
]
var upperLeft = [GridPoint]()
var upperRight = [GridPoint]()
var lowerLeft = [GridPoint]()
var lowerRight = [GridPoint]()
for pt in points {
if pt.right {
if pt.down {
upperLeft.append(pt)
}
if pt.up {
lowerLeft.append(pt)
}
}
if pt.left {
if pt.down {
upperRight.append(pt)
}
if pt.up {
lowerRight.append(pt)
}
}
}
print(upperLeft.count) // 26
print(upperRight.count) // 26
print(lowerLeft.count) // 26
print(lowerRight.count) // 26
var squares = 0
for ul in upperLeft {
for ur in upperRight {
if ul.y != ur.y || ul.x >= ur.x {
continue
}
for ll in lowerLeft {
if ll.x != ul.x || ll.y <= ul.y || (ll.y - ul.y != ur.x - ul.x) {
continue
}
for lr in lowerRight {
if lr.x != ur.x || lr.y != ll.y {
continue
}
squares += 1
}
}
}
}
print(squares) // 40
以下是符合左上角的26点:
- 1 回答
- 0 关注
- 136 浏览
添加回答
举报