题解 | 如果直接枚举所有的肯定会超时,所以要先预处理记录#的位置

最大正方形

https://www.nowcoder.com/practice/f6beb4837e8f43c997141e0b6041285d

n = int(input())
list0 = [list(input().strip()) for _ in range(n)]
# 收集所有#的位置
points = []
for i in range(n):
    for j in range(n):
        if list0[i][j] == '#':
            points.append((i, j))
max = 0
best = []
m = len(points)
# 在#点之间枚举
for a in range(m):
    x1, y1 = points[a]
    for c in range(a+1, m):  # 避免重复
        x3, y3 = points[c]
        # 中心点
        mx = (x1 + x3) / 2.0
        my = (y1 + y3) / 2.0
        # 向量
        dx = x1 - mx
        dy = y1 - my
        # 计算B, D
        x2 = int(mx - dy + 0.5)
        y2 = int(my + dx + 0.5)
        x4 = int(mx + dy + 0.5)
        y4 = int(my - dx + 0.5)
        # 检查B,D是否为整数格点
        if abs((x1 + x3)-(x2 + x4)) > 1e-9 or (abs((y1 + y3) - (y2 + y4)) > 1e-9):
            continue
        # 检查边界
        if not (0 <= x2 < n and 0 <= y2 < n and 0 <= x4 < n and 0 <= y4 < n):
            continue
        # 检查B,D是否为#
        if list0[x2][y2] == '#' and list0[x4][y4] == '#':
            area = ((x1 - x3) ** 2 + (y1 - y3) ** 2) // 2
            if area > max:
                max = area
                best = [(x1, y1), (x2, y2), (x3, y3), (x4, y4)]

for x, y in best:
    print(x + 1, y + 1)


                

全部评论

相关推荐

轻絵梨花泪沾衣:南泵,大少爷驾到通通闪开
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务