当前位置: 首页 > news >正文

笔试-广度优先搜索BFS-信号强度

应用

网络信号经过传递会逐层衰减,遇到阻碍物无法直接穿透,但可以绕过,在此情况下计算某个位置的网络信号值。

array[m][n]是一个二维数组,代表网格地图。array[i][j]=0代表空旷位置,array[i][j]=x代表强度为x的信号源,array[i][j]=-1代表阻碍物有1个。 整个网格地图包含多个阻碍物,信号衰减是信号源上下左右相邻网格-1。
输入:
m、n代表mn的数组;
一串以空格分隔的整数,共m
n个,数值含义如上述解释;
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_x
            previous_y = current_y

            for 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 = None

visited = 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:
        # 信号强度为负,就标记成0
        print(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

相关文章:

  • 手机端Flutter、React Native与原生安卓、iOS交互的方案及设计原理
  • 数据结构与算法-图论-拓扑排序
  • An error occurred: ‘numpy.ndarray‘ object has no attribute ‘clone‘
  • 创建表空间和表
  • 再学:Solidity数据类型
  • [Linux]进程控制
  • 实战ansible-playbook
  • 论文修改参考文献的数字顺位顺序技巧
  • leetcode每日一题:使字符串平衡的最小交换次数
  • 6个月的Go语言学习甘特图路线图 从零基础到项目实战
  • GB9706.1-2020部分定义
  • flutter Json转Dart Model类
  • EF框架数据库更新后自动更新类及上下文
  • NA611系列WiFi串口服务器常见问题以及解决办法
  • Android PC 要来了?Android 16 Beta3 出现 Enable desktop experience features 选项
  • 爬虫逆向:详细讲述Android底层原理及机制
  • 安全无事故连续天数计算,python 时间工具的高效利用
  • 算法016——最小覆盖子串
  • 209、不大于n的数的组合(python)
  • Linux(8.4)NFS
  • 百利天恒董事长向复旦捐赠三千万元,用于支持创新药物靶点发现等师资建设需要
  • 中国工程院院士、国医大师、现代中国针灸奠基人石学敏逝世
  • 技术派|巴基斯坦导弹:让印度保持克制的“定海神针”?
  • 国家税务总局泰安市税务局:山东泰山啤酒公司欠税超536万元
  • Meta正为AI眼镜开发人脸识别功能
  • 调节负面情绪可以缓解慢性疼痛