头像

zhincra




离线:47分钟前


最近来访(332)
用户头像
Andy2035
用户头像
openallzzz
用户头像
我Justsmile一下
用户头像
Dear.隆冬
用户头像
泛滥的可爱物种不会RE
用户头像
fangzichang
用户头像
胡想
用户头像
say774
用户头像
zinfa
用户头像
yxc的小迷妹
用户头像
冰中月
用户头像
不高兴的兽奶
用户头像
RyanRyan
用户头像
RwChen
用户头像
wwwzz
用户头像
navystar
用户头像
for循环
用户头像
acwing_xyy
用户头像
xhalz_cxk
用户头像
Ethanyyc

新鲜事 原文

zhincra
48分钟前
蓝桥杯5分钟倒计时
图片


新鲜事 原文

zhincra
1小时前
蓝桥国赛马上就要开始了,我们浙江一共十个,我竟然是其中一个


新鲜事 原文

zhincra
3小时前
来感受一下蓝桥杯的压迫感吧:)
图片


新鲜事 原文

zhincra
7小时前
今天下午就蓝桥杯了啊:)


新鲜事 原文

zhincra
21小时前
论我同学incra的同桌
图片 图片


新鲜事 原文

zhincra
1天前
图片



zhincra
1天前

首先,机器人的移动只能是横纵坐标加减1,这提示我们使用 BFS 算法求解最大可达到格子数。

其次,需要注意不能进入行坐标和列坐标的数位之和大于 k 的格子。可以写一个函数 getSum 来计算一个数的所有数位之和,并在 BFS 过程中判断当前格子对应的坐标的数位之和是否超过了 k。

最后,遍历完后,通过记录访问过的坐标数量返回最大可达到的格子数。

Python 代码实现:

python
import queue

# 计算一个数的各个数字之和
def getSum(num):
    s = 0
    while num > 0:
        s += num % 10
        num //= 10
    return s

def movingCount(k: int, m: int, n: int) -> int:
    visited = set()     # 存储访问过的坐标
    q = queue.Queue()   # 队列保存待访问的坐标
    q.put((0, 0))       # 将起点添加到队列中
    visited.add((0, 0)) # 标记起点为已访问

    cnt = 1             # 统计可以到达的格子数,起点已经访问过了
    dx = [0, 1, 0, -1]  # 横向移动的方向数组
    dy = [1, 0, -1, 0]  # 纵向移动的方向数组

    while not q.empty():
        x, y = q.get()
        for i in range(4):
            newx, newy = x + dx[i], y + dy[i]
            if 0 <= newx < m and 0 <= newy < n and (newx, newy) not in visited and getSum(newx) + getSum(newy) <= k:
                q.put((newx, newy))
                visited.add((newx, newy))
                cnt += 1

    return cnt

其中 visited 集合用于标记已经访问过的坐标,避免重复访问。




zhincra
2天前
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def buildTree(self, preorder, inorder):
        """
        :type preorder: List[int]
        :type inorder: List[int]
        :rtype: TreeNode
        """
        if not inorder: return None   # 如果中序遍历为空,则返回空节点

        root_val = preorder.pop(0)      # 用 pop 方法弹出前序遍历的第一个节点;
        root = TreeNode(root_val)       # 对根节点实例化,并初始化值;

        idx = inorder.index(root_val)     # 在中序遍历列表中查找根节点,得到下标位置;

        # 递归构建左右子树及其关系;
        root.left = self.buildTree(preorder, inorder[:idx])
        root.right = self.buildTree(preorder, inorder[idx+1:])

        return root    # 返回构建好的二叉树。


新鲜事 原文

zhincra
12天前
e搞代码 https://www.luogu.com.cn/paste/3uqng5l3 复制到ide py


新鲜事 原文

zhincra
1个月前
再次声明incra不是我大号是我同学