# 输入: # 22220 # 00000 # 00000 # 01111 # 输出: 8 # 输入: # 22220 # 00020 # 00010 # 01111 # 输出:15 # 输入: # 20000 # 00020 # 00000 # 00111 # 输出: 3 # 给你一个由'0'空地、'1'银矿、'2'金矿、组成的地图、堆矿只能由上下左右相邻的 金矿或银矿连接形成。 # 超出范围可以认为是空地,假设银矿价值1,金矿价值2、请你找出地图中最大价值的堆矿,并输出该堆矿价值 arrs = [] while True: line = list(map(int,list(input()))) if line: arrs.append(line) else: break print(arrs) from queue import Queue def search(i, j, arrs, max_valus,visit): directions = [[-1, 0], [1, 0], [0, 1], [0, -1]] queue = Queue() queue.put((i, j)) while not queue.empty(): i, j = queue.get() for dir in directions: new_x = i + dir[0] new_y = j + dir[1] if new_x >=0 and new_x <len(arrs) and new_y>=0 and new_y <len(arrs[0]): if arrs[new_x][new_y] == 0: continue if not visit[new_x][new_y]: visit[new_x][new_y] = True print("new_x,new_y=",new_x,new_y) max_valus += arrs[new_x][new_y] queue.put((new_x, new_y)) return max_valus def get_max_values(arrs): max_valus =0 res = [] visit = [[False for _ in range(len(arrs[0]))] for _ in range(len(arrs))] for i in range(len(arrs)): for j in range(len(arrs[0])): if arrs[i][j] == 1 or arrs[i][j] == 2: max_valus +=arrs[i][j] visit[i][j] = True valus = search(i, j, arrs, max_valus,visit) res.append(valus) max_valus =0 return max(res) print(get_max_values(arrs))