AcWing 756. 蛇形矩阵---前缀和思想
原题链接
简单
作者:
何锦仙
,
2024-11-27 15:27:07
,
所有人可见
,
阅读 4
dx=[1,0,-1,0]
dy=[0,1,0,-1]
li=[]
way=[] #用于判断是否已经走过该位置
n,m=map(int,input().split())
for i in range(n):
st=[]
for j in range(m):
st.append(1)
li.append(st)
# print(li)
i,j,t=0,0,0
way.append((0,0))
for ii in range((n+1)*(m+1)):
if 0 <= i+dy[t] <= n-1 and 0 <= j+dx[t] <= m-1 and (i+dy[t],j+dx[t]) not in way:
way.append((i+dy[t],j+dx[t]))
li[i+dy[t]][j+dx[t]]+=li[i][j]
i+=dy[t]
j+=dx[t]
else:
t=(t+1)%4
# print(t)
# print(way)
for i in li:
for j in i:
print(j,end=' ')
print('')
# 1 1 1 1 1
# 1 1 1 1 1
# 1 1 1 1 1