n = int(input())
col_ = list(map(int,input().strip().split()))
row_ = list(map(int,input().strip().split()))
# 路径标配 记录矩阵
martix =[[0 for j in range(n)]for i in range(n)]
ans=[]
# 移动方式
move=[(-1,0),(1,0),(0,-1),(0,1)]
# 记录射击情况
rc_ = [[0 for i in range(n)] for j in range(2)]
rc_[0][0] +=1
rc_[1][0] +=1
def dfs(pos,rc_,pas):
global ans
if pos ==(n-1,n-1):
if rc_[0] ==col_ and rc_[1] ==row_:
ans =pas.copy()
return
else:
for i in move:
new_x = pos[0] +i[0]
new_y = pos[1]+ i[1]
if 0 <=new_x < n and 0<=new_y<n and not martix[new_x][new_y]:
rc_[0][new_y] +=1
rc_[1][new_x] +=1
if rc_[0][new_y] >col_[new_y] or rc_[1][new_x]>row_[new_x]:
rc_[0][new_y] -=1
rc_[1][new_x] -=1
continue
martix[new_x][new_y] =1
pas.append((new_x,new_y))
dfs((new_x,new_y),rc_,pas)
rc_[0][new_y] -=1
rc_[1][new_x] -=1
martix[new_x][new_y] =0
pas.pop()
dfs((0,0),rc_,[(0,0)])
for i in ans:
print(i[0]*n+i[1],end=' ')
# print(ans)
【蓝桥杯】【dfs】路径之谜
最新推荐文章于 2024-11-01 19:25:44 发布