13届蓝桥杯省赛PythonB组真题-蜂巢

蜂巢题目本身难度并不大,使用枚举方法就可以求解,但在编程中还是有坑的(文末最后提到)。解题思路:

1.先把蜂巢坐标系(d, p, q)转换为直角坐标系(x, y),不妨把正六边形中心到各边的距离记作1.

则沿6各方向移动一步的坐标变换是:

direction = [(-2, 0), (-1, a), (1, a), (2, 0), (1, -a), (-1, -a)],其中a=math.sqrt(3)

所以有坐标转换公式:

x = direction[d][0]*p + direction[(d+2)%6][0]*q
y = direction[d][1]*p + direction[(d+2)%6][1]*q

2.B(x1, y1)与C(x2, y2)的关系分为5种:

1)B、C在同一行:y1=y2

2)B在C的左上:y1>y2且x1<=x2

3)B在C的左下:y1<y2且x1<=x2

4)B在C的右上:y1>y2且x1>x2

5)B在C的右下:y1<y2且x1>x2

对情形1)最简单,沿方向0或3移动即可,移动步数:|x2-x1|//2

对情形2),先把B沿方向4移动到B‘,使得B’与C同行,再把B‘沿方向3移动到C;但如果B’到了C点右边,则会使得移动的步数不是最少,此时要先把B沿方向4移动到C点正上方的B‘,再把B’移动到C。

其它3种情况,类似情况2)。具体代码如下,视频讲解可见: 蜂巢_哔哩哔哩_bilibili

import math
a = math.sqrt(3)
direction = [(-2, 0), (-1, a), (1, a), (2, 0), (1, -a), (-1, -a)]
def transform(d,p,q):#把坐标转为直角坐标系下的值
    #设蜂巢正六边形中心到各边的距离=1
    x = direction[d][0]*p + direction[(d+2)%6][0]*q
    y = direction[d][1]*p + direction[(d+2)%6][1]*q
    return x, y

def count_step(d1, p1, q1, d2, p2, q2):
    x1, y1 = transform(d1, p1, q1) # B点坐标
    x2, y2 = transform(d2, p2, q2) # C点坐标

    y_steps = round(math.fabs(y1 - y2) / a)
    # 1. B点在C点左上,从B点出发先沿方向4走到与C同行,再沿方向3到C点
    if y1 > y2 and x1 <= x2:
        x1 = x1 + direction[4][0] * y_steps
        if x1 <= x2:# B点到与C点同一行的位置
            x_steps = math.fabs(x2 - x1) // 2
            return round(y_steps + x_steps)
        else:
            # 先沿方向4走(x2-x1)/direction[4][0]步,走到与C点横坐标相等的位置
            y_steps_1 = round((x2 - x1)/direction[4][0])
            # 此时在纵轴,走了 y_steps1 * direction[4][1]
            y1 = y1 + direction[4][1] * y_steps_1
            # 还需要从C点正上方走到C点
            y_steps_2 = round((y1 - y2)/a)
            return y_steps_1 + y_steps_2

    # 2. B点在C点同一行的左边或右边
    if y1 == y2:
        return int( math.fabs(x2 - x1) // 2)

    # 3. B点在C点左下,从B点出发先沿方向2走到与C同行,再沿方向3到C点
    if y1 < y2 and x1 <= x2:
        x1 = x1 + direction[2][0] * y_steps
        if x1 <= x2:
            x_steps = math.fabs(x2 - x1) // 2
            return round(y_steps + x_steps)
        else:
            y_steps_1 = round((x2 - x1) / direction[2][0])
            y1 = y1 + direction[2][1] * y_steps_1
            y_steps_2 = round((y2 - y1) / a)
            return y_steps_1 + y_steps_2

    # 4. B点在C点右上,从B点出发先沿方向5走到与C同行,再沿方向0到C点
    if y1 > y2 and x1 >= x2:
        x1 = x1 + direction[5][0] * y_steps
        if x1 >= x2:
            x_steps = math.fabs(x1 - x2) // 2
            return round(y_steps + x_steps)
        else:
            y_steps_1 = round((x2 - x1) / direction[5][0])
            y1 = y1 + direction[5][1] * y_steps_1
            y_steps_2 = round((y1 - y2) / a)
            return y_steps_1 + y_steps_2

    # 5. B点在C点右下,从B点出发先沿方向1走到与C同行,再沿方向0到C点
    if y1 < y2 and x1 >= x2:
        x1 = x1 + direction[1][0] * y_steps
        if x1 >= x2:
            x_steps = math.fabs(x1 - x2) // 2
            return round(y_steps + x_steps)
        else:
            y_steps_1 = round((x2 - x1) / direction[1][0])
            y1 = y1 + direction[1][1] * y_steps_1
            y_steps_2 = round((y2 - y1) / a)
            return y_steps_1 + y_steps_2

inp = tuple(map(int, input().strip().split()))
print(count_step(*inp))

特别注意,取整使用round, 博主开始使用int取整,结果有几条测试数据的输出比标准输出小1,耗费我不少时间才发现是取整的原因。

### 历蓝桥杯Python B考试真题概述 蓝桥杯全国软件和信息技术专业人才大是一项面向大学生的科技竞活动。对于Python编程语言,该事提供了多个不同难度级别的题目供参者挑战。以下是部分年份中Python B的一些典型试题及其解答思路。 #### 2021年第十二蓝桥杯Python B真题分析 一道关于字符串处理的问题被提出,在这个问题里需要利用`find()`方法去定位特定字符的位置并据此完成相应操作[^2]。此类型的题目旨在考察选手们对基础数据结构以及常用算法的理解程度。 ```python def string_processing(s, target_char): index = s.find(target_char) if index != -1: result = "Character found at position:" + str(index) else: result = "Target character not present" return result ``` #### 十三蓝桥杯Python B第五题:“蜂巢” 本题描述了一个由六边形成的网格模型,并要求编写程序模拟蜜蜂在其中移动的过程。为了实现这一目标,可以采用广度优先搜索(BFS)或深度优先搜索(DFS)的方法来进行路径探索[^3]。 ```python from collections import deque directions = [(1,-1),(1,0),(-1,1),(-1,0),(0,1),(0,-1)] def bfs(start_x,start_y,end_x,end_y,matrix): queue = deque([(start_x, start_y)]) visited = set() while queue: current_pos = queue.popleft() if current_pos==(end_x,end_y): return True for dx,dy in directions: next_pos=(current_pos[0]+dx,current_pos[1]+dy) if (next_pos not in visited and within_bounds(next_pos)): visited.add(current_pos) queue.append(next_pos) return False def within_bounds(position): # Implement boundary checking logic here. pass ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值