import copydef getsumbfs(i,j,m,n,maps):
#如果是廣度優先 先找到所有的鄰接點 再依次往下找鄰接點
quenue = [[i,j]]
sum = maps[i][j]
maps[i][j] = 0
while quenue:
###找八個方向
i, j = quenue[0][0], quenue[0][1]
if i-1 >=0 and j-1>=0 and maps[i-1][j-1]!=0:#左上
sum += maps[i-1][j-1]
maps[i-1][j-1] = 0
if i-1 >=0 and j=0 and j+1=0 and maps[i][j-1]!=0:#左
sum += maps[i][j-1]
maps[i][j-1] = 0
if i < m and j+1=0 and maps[i+1][j-1]!=0:#左下
sum += maps[i+1][j-1]
maps[i+1][j-1] = 0
if i+1 < m and j < n and maps[i+1][j]!=0:#下
sum += maps[i+1][j]
maps[i+1][j] = 0
if i+1 < m and j+1 < n and maps[i+1][j+1]!=0:#右下
sum += maps[i+1][j+1]
maps[i+1][j+1] = 0
del quenue[0]
return sum
def getsumdfs(i,j,m,n,maps,stack,bol_list):
#如果是深度優先 先找到最近的鄰接點 再找鄰接點最近的鄰接點
bol_list[i][j] = true
sum = maps[i][j]
maps[i][j] = 0
while stack:
###找八個方向
i, j = stack[-1][0], stack[-1][1]
if bol_list[i][j]:
stack.pop()
if i-1 >=0 and j-1>=0 and maps[i-1][j-1]!=0:#左上
sum += maps[i-1][j-1]
maps[i-1][j-1] = 0
bol_list[i-1][j-1] = true
if i-1 >=0 and j=0 and j+1=0 and maps[i][j-1]!=0:#左
sum += maps[i][j-1]
maps[i][j-1] = 0
bol_list[i][j - 1] = true
if i < m and j+1=0 and maps[i+1][j-1]!=0:#左下
sum += maps[i+1][j-1]
maps[i+1][j-1] = 0
bol_list[i + 1][j - 1] = true
if i+1 < m and j < n and maps[i+1][j]!=0:#下
sum += maps[i+1][j]
maps[i+1][j] = 0
bol_list[i+1][j] = true
if i+1 < m and j+1 < n and maps[i+1][j+1]!=0:#右下
sum += maps[i+1][j+1]
maps[i+1][j+1] = 0
bol_list[i + 1][j + 1] = true
return sum
if __name__ == '__main__':
maps = [[34, 0, 0, 0, 0,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, 30, 0, 7, 0, 0, 0, 0, 0, 5, 5, 0, 0, 0, 0, 0, 0, 10, 0, 50],
[0, 40, 7, 0, 0, 0, 0, 0, 0, 5, 5, 0, 0, 0, 0, 0, 0, 0, 50, 0],
[43, 30, 25, 10, 50, 0, 0, 0, 6, 6, 6, 6, 0, 0, 0, 0, 0, 50, 0, 0]]
maps2 = copy.deepcopy(maps)
m,n = len(maps),len(maps[0])
sum_tot =
sum_tot2 =
stack =
bol_list = [[false for i in range(m)] for j in range(n)]
for i in range(m):
for j in range(n):
if maps[i][j]!=0:
if maps2[i][j]!=0:
阿里筆試題 八卦陣
具體題目忘記了,大致就是說乙個矩陣裡面有8個陣仗。不為零的元素相連線的湊成乙個陣。求陣的最大值和最小值 基本思路就是深度優先搜尋 廣度 遍歷8次,搜尋中記得把搜過的值做標記,我的方法是直接賦為0,方便判斷,下面是 importsys defhelp arr,i,j ifarr i j 0 retur...
愛情不需要八卦陣
我對乙個好朋友說 這個圈子裡,我不喜歡太多的人對我的生活瞭如指掌,所以我疏遠了那麼多人。有時的出現只是為了做做表面文章,比如說 比如說同事,不論是誰,在那個時候,只能讓你看到假象乙個,包括言談到穿著,從小到大,我穿我兩個表姐穿過的衣服,家裡經濟情況不太好,我對物質上的東西更無所謂 焚膏繼晷地做我喜歡...
八卦和天干地支的正確讀音
八卦讀音 幹qi n 坎k n 艮 n 震zh n,巽x n 離l 坤k n 兌du 周易 中用兩短畫 表示陰,用一長畫 表示陽。稱為陰爻 y o 稱為陽爻。以陽或陰三個爻為一組分別組成的八種符號排列,這八個由三個爻組成的卦,也叫經卦或單卦。由八個單卦以不同的次序兩兩重合 就產生了六十四卦,六十 四...