我正在解决一个问题,但似乎有问题。在调试时我发现在第15行,程序正在将完整矩阵更新为-1。而我已指定更新索引i
,j
。
下面是代码和示例输入。提前谢谢。
n = int(input().strip())
for i in range(n):
sa = input().strip()
s.append(sa)
import queue
ar =[[-10]*n]*n
q = queue.Queue()
error = False
while not q.empty():
i,j = q.get()
try:
if ar[i][j-1]==-10:
ar[i][j-1]=1+ar[i][j]
q.put([i,j-1])
except:
error =True
try:
if ar[i][j+1]==-10:
ar[i][j+1]=1+ar[i][j]
q.put([i,j+1])
except:
error =True
try:
if ar[i-1][j]==-10:
ar[i-1][j]=1+ar[i][j]
q.put([i-1,j])
except:
error =True
try:
if ar[i+1][j]==-10:
ar[i+1][j]=1+ar[i][j]
q.put([i+1,j])
print('here')
except:
error =True
m = -1
for i in range(n):
for j in range(n):
#m = max(ar[i][j],m)
print(ar[i][j],end='')
print('')
print(m)
示例输入: