02659: Bomb Game
matrices, http://cs101.openjudge.cn/practice/02659/
Bosko and Susko are playing an interesting game on a board made of rectangular fields arranged in A rows and B columns.
When the game starts, Susko puts its virtual pillbox in one field one the board. Then Bosko selects fields on which he will throw his virtual bombs. After each bomb, Susko will tell Bosko whether his pillbox is in the range of this bomb or not.
The range of a bomb with diameter P (P is always odd), which is thrown in field (R, S), is a square area. The center of the square is in the field (R, S), and the side of the square is parallel to the sides of the board and with length P.
After some bombs have been thrown, Bosko should find out the position of Susko's pillbox. However, the position may be not unique, and your job is to help Bosko to calculate the number of possible positions.
输入
First line of input contains three integers: A, B and K,
Each of the next K lines contains integers R, S, P and T, describing a bomb thrown in the field at R-th row and S-th column with diameter
输出
Output the number of possible fields, which Susko's pillbox may stay in.
样例输入
5 5 3
3 3 3 1
3 4 1 0
3 4 3 1样例输出
5来源
Croatia OI 2002 National – Juniors
矩阵遍历时候,用min,max避免索引越界,左侧是max(0,...),右侧是min(...,n-1)
def max_count(matrix):
maximum = max(max(row) for row in matrix)
count = sum(row.count(maximum) for row in matrix)
return count
def calculate_possible_positions(A, B, K, bombs):
positions = [[0] * B for _ in range(A)]
for (R, S, P, T) in bombs:
for i in range(max(0, R - (P - 1) // 2), min(A, R + (P + 1) // 2)):
for j in range(max(0, S - (P - 1) // 2), min(B, S + (P + 1) // 2)):
if T == 1 :
positions[i][j] += 1
elif T == 0:
positions[i][j] -= 1
#for row in positions:
# print(row)
return max_count(positions)
A, B, K = map(int, input().split())
bombs = []
for _ in range(K):
R, S, P, T = map(int, input().split())
bombs.append((R - 1, S - 1, P, T))
result = calculate_possible_positions(A, B, K, bombs)
print(result)