python - 现代艺术中的 CP Python 逻辑混淆(Bronze USACO 2017 美国公开赛第 3 页)

标签 python algorithm recursion logic

问题信息

USACO 2017 美国公开赛铜牌问题 3 现代艺术 Problem Link

我的作品

# Read in grid as 2D array
with open("art.in", 'r') as fin:
    n = int(fin.readline().strip())
    grid = [[int(i) for i in fin.readline().strip()] for _ in range(n)]
print(grid)

# Get all possible colors, which is everything visible excluding zero
possible = set()
for row in grid:
    for p in row:
        possible.add(p)
if 0 in possible:
    possible.remove(0)
print(possible)

# Recursive search function that gets the maximum x of the triangle and maximum y of the triangle, which will be used further down the road to calculate whether or not it is a valid rectangle
def search(grid, i, j, v):
    global max_x, max_y, searched, area
    if i < 0 or i >= n or j < 0 or j >= n or grid[i][j] != v or (i, j) in searched:
        max_x = max(max_x, j)
        max_y = max(max_y, i)
        return
    searched.append((i, j))
    area += 1
    search(grid, i+1, j, v)
    search(grid, i-1, j, v)
    search(grid, i, j+1, v)
    search(grid, i, j-1, v)

# Use the search, and check if there is a possibility of the rectangle being covered. It it is covered, eliminate the rectangle that covers it from the list of possibilities.
searched = []
for i, row in enumerate(grid):
    for j, p in enumerate(row):
        if (i, j) in searched or not p:
            continue
        max_x = 0 
        max_y = 0
        # The area variable is uneeded. Using it for debugging
        area = 0
        search(grid, i, j, p)
        print(area, (max_x-j) * (max_y-i))
        print()

        for k in range(i, max_y):
            for l in range(j, max_x):
                if grid[k][l] != p and grid[k][l] in possible:
                    possible.remove(grid[k][l])

# Write the answer to the output file
with open('art.out', 'w') as fout:
    fout.write(str(len(possible)))

问题

我的逻辑从代码中非常清晰,我可以从 10 个测试用例中得到 6 个,但是当我尝试这样的输入时:

4
1234
1234
1234
1334

我的程序输出 4 而不是 3,这是正确答案。这就是我的问题。我不知道为什么是 3 而不是 4

我尝试过的

这个问题我看了好几遍,还是没明白。

如果有人能帮忙解释一下,那就太好了。

最佳答案

我认为这个问题的一个重要部分是识别输入中的“破损”矩形(即有重叠的矩形)。对于上一个示例,124 是“完整”矩形,因为它们没有明确重叠任何其他瓷砖。但是,3 显然与 2 重叠,因此,23 先存在,而不是同时存在。因此,有两个完整的加上一组不完整的,最终结果为3。以下是此问题的可能解决方案的代码:

from collections import defaultdict
def w_h(points):
   d = {'w':set(), 'h':set()}
   for a, b in points:
      d['h'].add(a)
      d['w'].add(b)
   return [max(b) - min(b) + 1 for b in d.values()]

def original_paintings(p):
  d = defaultdict(list)
  for x, a in enumerate(p):
     for y, s in enumerate(a):
        if s:
           d[s].append((x, y))
  r = {a:(wh:=w_h(b))[0]*wh[1] - len(b) for a, b in d.items()}
  return len(r) - len(set(r.values())) + 1

t1 = [[2, 2, 3, 0], [2, 7, 3, 7], [2, 7, 7, 7], [0, 0, 0, 0]]
t2 = [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 3, 3, 4]]
print(original_paintings(t1))
print(original_paintings(t2))

输出:

1
3

关于python - 现代艺术中的 CP Python 逻辑混淆(Bronze USACO 2017 美国公开赛第 3 页),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70051305/

相关文章:

python - pserve 在 docker 容器内不工作

python - SageMaker 创建 PyTorchModel 而不部署

arrays - Ruby - 如何切片数组并根据条件对其元素求和

c - 递归反转单链表的函数中的段错误

java - 迷宫算法 KINDA 有效。一些迷宫,不是所有的帮助

python - tensorflow : What is actually tf. nn.dropout output_keep_prob?

c# - 线性代数库

python - 逼近大型对称矩阵的最高 3 个特征值和特征向量的快速方法

java - 在数组中搜索与其他数字不同的数字

haskell - 数一下我可以划分多少次