white_board=[[]]
for x in range(100):
white_board[0].append(0)
for x in range(99):
white_board.append(white_board[0]) #100*100 0 two dimensional array
n=int(input())
posL=[] #dot's position List
for x in range(n):
a,b=map(int,input().split())
posL.append((a,b))
for y in posL:
indY=100-y[1]
for z in range(10):
indX=y[0]-1
for h in range(10):
if white_board[indY][indX]==0:
white_board[indY][indX]=1
indX+=1
indY-=1
(我正在尝试解决这个问题https://www.acmicpc.net/problem/2563) 我尝试了这段代码,但是在更改元素的过程中,它更改了所有列表的元素。 前输入) 3 3 7 15 7 5 2
在 3 7 情况下,它将所有white_board列表的第三个索引更改为1
如何解决这个问题?
我认为管理索引是个问题......
要解决此问题,您需要为每一行创建单独的列表。
white_board = [[0] * 100 for _ in range(100)]
n = int(input())
posL = [] # dot's position List
for _ in range(n):
a, b = map(int, input().split())
posL.append((a, b))
for y in posL:
indY = 100 - y[1]
for _ in range(10):
indX = y[0] - 1
for _ in range(10):
if white_board[indY][indX] == 0:
white_board[indY][indX] = 1
indX += 1
indY -= 1
total_inked_area = sum(row.count(1) for row in white_board)
print(total_inked_area)