国外电商平台排名谷歌seo是什么
应用
网络信号经过传递会逐层衰减,遇到阻碍物无法直接穿透,但可以绕过,在此情况下计算某个位置的网络信号值。
array[m][n]是一个二维数组,代表网格地图。array[i][j]=0代表空旷位置,array[i][j]=x代表强度为x的信号源,array[i][j]=-1代表阻碍物有1个。 整个网格地图包含多个阻碍物,信号衰减是信号源上下左右相邻网格-1。
输入:
m、n代表mn的数组;
一串以空格分隔的整数,共mn个,数值含义如上述解释;
i、j代表待求信号强度的位置;
实现
m, n = input("请输入网格地图的行数、列数:").split()
M = int(m)
N = int(n)strs = input("请输入网格地图的数据,以空格分开:").split()
data = [int(i) for i in strs]# 创建二维列表
array = []
for i in range(0, M):r = []# 填入数据:一次填N个,填M次for j in range(i * N, i*N + N):r.append(data[j])array.append(r)
# print(array)x, y = input("请输入待求信号强度的位置:").split()
X = int(x)
Y = int(y)# 获取起点坐标
begin = []
for i in range(0, M):for j in range(0, N):if array[i][j] > 0:begin.append(i)begin.append(j)
A = begin[0]
B = begin[1]# 以当前位置为本次坐标,计算下一次的所有坐标
def calculate_next(previous_x, previous_y, current_x, current_y, array, M, N):# 下一次坐标位置:上、下、右、左lst = [[current_x - 1, current_y], [current_x + 1, current_y], [current_x, current_y + 1],[current_x, current_y - 1]]nxt = []for i in lst:next_x = i[0]next_y = i[1]# 坐标数值合理if 0 <= next_x <= M-1 and 0 <= next_y <= N-1:# 向“0”传播if array[next_x][next_y] == 0:# 禁止返回上次位置if next_x != previous_x and next_y != previous_y:nxt.append([next_x, next_y])return nxt# 传播
def broadcast(previous_x, previous_y, current_x, current_y, visited, step, result, array, M, N, X, Y):# 坐标作为元组放入集合,记录已访问的位置visited.add((previous_x, previous_y))visited.add((current_x, current_y))if current_x == X and current_y == Y:# 到达目标地点,就把步数保存result.append(step)else:# 计算所有可能下一步nxt = calculate_next(previous_x, previous_y, current_x, current_y, array, M, N)# print(nxt)if nxt:# 存在下一步的可能,开始传播,步数加一step += 1# 下一位置的前坐标是当前位置的坐标previous_x = current_xprevious_y = current_yfor i in nxt:# 下一位置的当前坐标赋值current_x = i[0]current_y = i[1]# 检测下一位置是否被访问过if (current_x, current_y) not in visited:# print(previous_x, previous_y, current_x, current_y, step)# 继续下一次传播broadcast(previous_x, previous_y, current_x, current_y, visited, step, result, array, M, N, X, Y)else:print("无路可走")current_x = A
current_y = B
previous_x = None
previous_y = Nonevisited = set()step = 0
result = []broadcast(previous_x, previous_y, current_x, current_y, visited, step, result, array, M, N, X, Y)if result:# 目的可达,从小到大排序,选最小步数result.sort()signal = array[A][B] - result[0]if signal >= 0:print(signal)else:# 信号强度为负,就标记成0print(0)
else:print("目的不可达")
请输入网格地图的行数、列数:6 5
请输入网格地图的数据,以空格分开:0 0 0 -1 0 0 0 0 0 0 0 0 -1 4 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0
请输入待求信号强度的位置:1 4
2请输入网格地图的行数、列数:6 5
请输入网格地图的数据,以空格分开:0 0 0 -1 0 0 0 0 0 0 0 0 -1 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
请输入待求信号强度的位置:2 1
0