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

【蓝桥杯】赛前练习

1. 排序

import os
import sys

n=int(input())
data=list(map(int,input().split(" ")))
data.sort()
for d in data:
    print(d,end=" ")
print()
for d in data[::-1]:
    print(d,end=" ")

2. 走迷宫BFS

import os
import sys
from collections import deque

n,m=map(int,input().split())
maze=[]
for _ in range(n):
    row=list(map(int,input().strip().split()))
    maze.append(row)
x1,y1,x2,y2=map(int,input().split())
x1 -= 1
y1 -= 1
x2 -= 1
y2 -= 1

def bfs_maze(n,m,maze,x1,y1,x2,y2):
    directions=[(-1,0),(1,0),(0,-1),(0,1)]
    queue=deque()
    queue.append((x1,y1,0))
    visited=[[False for _ in range(m)] for _ in range(n)]
    visited[x1][y1]=True

    while queue:
        x,y,step=queue.popleft()
        if x==x2 and y==y2:
            return step
        for dx,dy in directions:
            nx,ny=x+dx,y+dy
            if nx<0 or nx>=n or ny<0 or ny>=m:
                continue
            if maze[nx][ny]!=1 or visited[nx][ny]==True:
                continue
            visited[nx][ny]=True
            queue.append((nx,ny,step+1))
    return -1

print(bfs_maze(n,m,maze,x1,y1,x2,y2))

 优化后的代码

from collections import deque

# 读取迷宫的行数和列数
n, m = map(int, input().split())
# 直接读取迷宫矩阵
maze = [list(map(int, input().split())) for _ in range(n)]
# 读取起点和终点坐标并调整为 0 索引
x1, y1, x2, y2 = [i - 1 for i in map(int, input().split())]

def bfs_maze(n, m, maze, x1, y1, x2, y2):
    # 定义四个移动方向
    directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    # 初始化队列
    queue = deque([(x1, y1, 0)])
    # 初始化访问标记数组
    visited = [[False] * m for _ in range(n)]
    visited[x1][y1] = True

    while queue:
        x, y, step = queue.popleft()
        # 若到达终点,返回步数
        if (x, y) == (x2, y2):
            return step
        # 遍历四个方向
        for dx, dy in directions:
            nx, ny = x + dx, y + dy
            # 检查新位置是否合法且未访问
            if 0 <= nx < n and 0 <= ny < m and maze[nx][ny] == 1 and not visited[nx][ny]:
                visited[nx][ny] = True
                queue.append((nx, ny, step + 1))
    # 未找到路径,返回 -1
    return -1

# 调用函数并输出结果
print(bfs_maze(n, m, maze, x1, y1, x2, y2))

3. 01背包问题

import os
import sys

 # 从用户输入获取物品数量和背包容量
num_items, max_capacity = map(int, input().split())
# 存储每个物品的重量和价值
weights = []
values = []
# 循环获取每个物品的重量和价值
for _ in range(num_items):
    weight, value = map(int, input().split())
    weights.append(weight)
    values.append(value)

# 创建一个二维数组 dp 用于动态规划,dp[i][w] 表示前 i 个物品在容量为 w 的背包中的最大价值
dp = [[0 for _ in range(max_capacity + 1)] for _ in range(num_items + 1)]
# 记录每个状态下选择的物品
choices = [[False for _ 

相关文章:

  • STM32 模块化开发指南 · 第 3 篇 环形缓冲区 RingBuffer 模块设计与单元测试
  • WHAT - React 安全地订阅外部状态源 - useSyncExternalStore
  • 我的Hexo自动Webhook部署方案
  • tree-sitter 的 grammar.js 编写方法
  • 如何进行预算考核
  • Ubuntu22环境下,Docker部署阿里FunASR的gpu版本
  • 【力扣hot100题】(085)单词拆分
  • P8647 [蓝桥杯 2017 省 AB] 分巧克力
  • 智能配电保护:公共建筑安全的新 “防火墙”
  • 大模型评估框架-----OpenCompass模型评估简介
  • js触发隐式类型转换的场景
  • 5. 蓝桥公园
  • TCP/UDP的连接和数据发送过程详解
  • 【模拟电路】稳压二极管/齐纳二极管
  • SGLang实战:从KV缓存复用到底层优化,解锁大模型高效推理的全栈方案
  • vue实现在线进制转换
  • 自定义排序注意点
  • 解决:AttributeError: module ‘cv2‘ has no attribute ‘COLOR_BGR2RGB‘
  • 微信小程序中使用ECharts 并且动态设置数据
  • 如何使用AI辅助开发CSS3 - 通义灵码功能全解析
  • 东莞网站设计建设公司/沈阳关键词优化报价
  • 中国十大摄影网站排名/5000元做百度推广效果怎么样
  • 西安网站建设哪家公司好/宁德seo优化
  • 六安网站建设/重庆seo排名优化费用
  • 网页历史记录恢复/深圳seo优化排名公司
  • wordpress轻物语主题/seo是什么缩写