Wat1r @ 2024-03-16 14:09:50
#输入
queue=[]
l=int(input())
mark=land=[[0]*(l+2) for _ in range(l+2)]
tmp=[list(map(int,input().split())) for _ in range(l)]#需要的矩阵
for i in range(l):
mark[i+1][1:l+1]=land[i+1][1:l+1]=tmp[i]
#方向函数
dirs = [
lambda x,y:(x+1,y),
lambda x,y:(x-1,y),
lambda x,y:(x,y+1),
lambda x,y:(x,y-1),
]
#bfs
queue.append((0,0))
while(queue):
tmp=queue.pop(0)
mark[tmp[0]][tmp[1]]=-1
for dir in dirs:
x,y=dir(tmp[0],tmp[1])
if x>=0 and x<l+1 and y >=0 and y <l+1 and mark[x][y]==0:
queue.append((x,y))
#输出
for i in range(1,l+1):
for j in range(1,l+1):
if land[i][j]==1:
print('1',end=' ')
elif land[i][j]==0 and mark[i][j]==0:
print('2',end=' ')
else:
print('0',end=' ')
print()
by Wat1r @ 2024-03-16 14:10:33
看不到数据点,二三四六都错了,还不知道错在哪
by Wat1r @ 2024-03-16 14:12:07
找遍全网,没几个用python写的,帮助估计寥寥无几
by Wat1r @ 2024-03-16 15:00:15
会了,边界值有问题