【lanqiao, this is for U】【必胜客自助】【智商税】年轻是要付出代价的

前言

昨天和GPT4.0一较高下,被虐的体无完肤,现在距离考试还有17天,继续干!
今天蓝桥杯平台上发布了几个拿奖题单,今天我们就把python组和蓝桥杯省一必刷压轴题题单刷一下,一共25道题,我就筛选一下
当然了,我也请GPT4.0来督战,解释我的代码后会贴上GPT的代码
在这里插入图片描述

1、迷宫

题目描述
本题为填空题,只需要算出结果后,在代码中使用输出语句将所填结果输出即可。

下图给出了一个迷宫的平面图,其中标记为 1 的为障碍,标记为 0 的为可以通行的地方。

010000
000100
001001
110000

迷宫的入口为左上角,出口为右下角,在迷宫中,只能从一个位置走到这 个它的上、下、左、右四个方向之一。

对于上面的迷宫,从入口开始,可以按 DRRURRDDDR 的顺序通过迷宫, 一共 10 步。其中 D、U、L、R 分别表示向下、向上、向左、向右走。 对于下面这个更复杂的迷宫(30 行 50 列),请找出一种通过迷宫的方式,其使用的步数最少,在步数最少的前提下,请找出字典序最小的一个作为答案。

请注意在字典序中 D<L<R<U。

n,m = 30,50
see = set()  # 记录走过的点
nums = [[int(x) for x  in input()] for _ in range(n)]//每行输入是字符串
def stop(x,y):
  if x < 0 or y < 0 or x >= 30 or y >= 50 or nums[x][y] == 1 or ((x,y) in see)://判断这个点是否能走,超出边界不要,不能走或走过也不要
    return False
  return True
def bfs(x,y):
  que = [(x,y,'')]
  while que:
    x,y,z = que.pop(0)
    if stop(x,y):  # x,y没有超界就继续,不符合就下一次循环,反正已经pop掉了,对应的z值也会更新
      see.add((x,y))
      que.append((x+1,y,z + 'D'))  # 按字母顺序添加
      que.append((x,y-1,z + 'L'))
      que.append((x,y+1,z + 'R'))
      que.append((x-1,y,z + 'U'))
    if x == 29 and y == 49://到达终点
      return z
print(bfs(0,0))

GPT

from collections import deque

maze = [
    "010000",
    "000100",
    "001001",
    "110000"
]

def bfs(maze):
    rows = len(maze)
    cols = len(maze[0])
    
    # 将字符串迷宫转换为二维整数数组
    int_maze = [[int(c) for c in row] for row in maze]
    
    visited = [[False for _ in range(cols)] for _ in range(rows)]
    visited[0][0] = True
    queue = deque([((0, 0), "")])

    while queue:
        (x, y), path = queue.popleft()
        
        if x == rows - 1 and y == cols - 1:
            return path

        for dx, dy, direction in [(1, 0, "D"), (0, -1, "L"), (0, 1, "R"), (-1, 0, "U")]:
            new_x, new_y = x + dx, y + dy
            
            if 0 <= new_x < rows and 0 <= new_y < cols and not visited[new_x][new_y] and int_maze[new_x][new_y] == 0:
                visited[new_x][new_y] = True
                queue.append(((new_x, new_y), path + direction))

result = bfs(maze)
print(result)

2、日志统计

题目描述
小明维护着一个程序员论坛。现在他收集了一份"点赞"日志,日志共有 N 行。其中每一行的格式是:
ts id
表示在 ts 时刻编号 id 的帖子收到一个"赞"。

现在小明想统计有哪些帖子曾经是"热帖"。如果一个帖子曾在任意一个长度为 D 的时间段内收到不少于 K 个赞,小明就认为这个帖子曾是"热帖"。

具体来说,如果存在某个时刻 T 满足该帖在 [T,T+D) 这段时间内(注意是左闭右开区间)收到不少于 K 个赞,该帖就曾是"热帖"。

给定日志,请你帮助小明统计出所有曾是"热帖"的帖子编号。

输入描述
输入格式:

第一行包含三个整数 N,D,K。

以下 N 行每行一条日志,包含两个整数 ts 和 id。

其中,1≤K≤N≤10 ,0≤ts≤10 ,0≤id≤10 。

输出描述
按从小到大的顺序输出热帖 id。每个 id 一行。

def check(D,K,ts_):
    for x in ts_:
        count=0
        for y in ts_:
            if x<=y<x+D:
                count+=1
            if count>=K:
                return True
    return False
N,D,K=map(int,input().split())
d=dict()//因为是看单个post效果咋样,所以用字典的存储方式是更合适的,第一个id,第二存点赞列表
for _ in range(N):
    ts,ids=map(int,input().split())
    d[ids]=d.get(ids,[])+[ts]//收一个 存一个
ans=[]
for j in d.items()://items是以列表返回可遍历的(,) 元组数组
    ids,ts_=(i for i in j)
    if check(D,K,ts_):
        ans.append(ids)//符合就加进去
for i in sorted(ans):
    print(i)

GPT

def hot_posts(logs, N, D, K)://公正廉洁!
    logs.sort()  # Sort logs by timestamp
    post_likes = {
    
    }  # Store the number of likes for each post
    hot_post_ids = set()  # Store the ids of hot posts

    for i in range(N):
        ts, id = logs[i]
        if id not in post_likes:
            post_likes[id] = []

        post_likes[id].append(ts)

        # Remove likes that are out of the D time range
        while post_likes[id] and post_likes[id][0] <= ts - D:
            post_likes[id].pop(0)

        # If there are enough likes in the time range, add to hot_post_ids
        if len(post_likes[id]) >= K:
            hot_post_ids.add(id)

    return sorted(list(hot_post_ids))


# Read input
N, D, K = map(int, input().split())
logs = [tuple(map(int, input().split())) for _ in range(N)]

# Calculate and print hot posts
result = hot_posts(logs, N, D, K)
for post_id in result:
    print(post_id)

3、青蛙过河

问题描述
小青蛙住在一条河边, 它想到河对岸的学校去学习。小青蛙打算经过河里的石头跳到对岸。

河里的石头排成了一条直线, 小青蛙每次跳跃必须落在一块石头或者岸上。 不过, 每块石头有一个高度, 每次小青蛙从一块石头起跳, 这块石头的高度就会下降 1 , 当石头的高度下降到 0 时小青蛙不能再跳到这块石头上(某次跳跃后使石头高度下降到 0 是允许的)。

小青蛙一共需要去学校上 x 天课, 所以它需要往返 2x 次。当小青蛙具有 一个跳跃能力 y 时, 它能跳不超过 y 的距离。

请问小青蛙的跳跃能力至少是多少才能用这些石头上完 x 次课。

输入格式
输入的第一行包含两个整数 n,x, 分别表示河的宽度和小青蛙需要去学校 的天数。请注意
2x 才是实际过河的次数。
在这里插入图片描述
输出格式
输出一行, 包含一个整数, 表示小青蛙需要的最低跳跃能力。

import sys; readline = sys.stdin.readline

read = lambda: [int(x) for x in readline().split()]

n, m = read()

arr = read()
s = [0] * n
for i, x in enumerate(arr):
    s[i + 1] = s[i] + x
    
def check(y):
    for i in range(n - y):
        if s[i + y] - s[i] < 2 * m: return False
    return True

l = 0
r = n

while l < r:
    mid = l + r >> 1
    if check(mid): r = mid
    else: l = mid + 1

print(l)

结语

刚去吃了必胜客自助,智商税快跑,168块不如再开一个月GPT,别信网络谣言,pure智商税
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_53415043/article/details/129711167
今日推荐