程式碼如下:
size, lines = [int(i) for i in input().split()], int(input())
matrix = [[0 for i in range(size[0])] for j in range(size[1])]
for i in range(lines):
spots = [int(i) for i in input().split()]
if spots[0] == spots[2]:
for j in range(min(spots[1], spots[3]), max(spots[1], spots[3]) + 1):
matrix[j - 1][spots[0] - 1] = 1
elif spots[1] == spots[3]:
for j in range(min(spots[0], spots[2]), max(spots[0], spots[2]) + 1):
matrix[spots[1] - 1][j - 1] = 1
elif (spots[0] > spots[2] and spots[1] > spots[3]) or (spots[2] > spots[0] and spots[3] > spots[1]):
for j, k in zip([l for l in range(min(spots[0], spots[2]), max(spots[0], spots[2]) + 1)], [m for m in range(min(spots[1], spots[3]), max(spots[1], spots[3]) + 1)]):
matrix[k - 1][j - 1] = 1
else:
for j, k in zip([l for l in range(min(spots[0], spots[2]), max(spots[0], spots[2]))], [m for m in range(max(spots[1], spots[3]), min(spots[1], spots[3]) - 1, -1)]):
matrix[k - 1][j - 1] = 1
print(sum([sum(i) for i in matrix]))
範例測資也能過,但送出之後好像也只有範例測資過
求解