腾讯2022校招第二批代码,求指教!!!

def f(nums, x, y):
    count = 1
    queue = [[x, y]]
    visited = [[x, y]]
    d = [[-2, -1], [-2, 1], [-1, -2], [-1, 2], [1, -2], [1, 2], [2, -1], [2, 1]]

    n, m = len(nums), len(nums[0])
    while queue:
        x, y = queue.pop()
        for dx, dy in d:
            x1, y1 = x + dx, y + dy
            if 0 <= x1 < n and 0 <= y1 < m and nums[x1][y1] != nums[x][y] and [x1, y1] not in visited:
                count += 1
                visited.append([x1, y1])
                queue.append([x1, y1])
    return count


if __name__ == '__main__':
    data = list(map(int, input().strip().split()))
    n, m = data[0], data[1]
    nums = [[0] * m for _ in range(n)]
    for i in range(n):
        data = list(input().strip())
        for j in range(m):
            nums[i][j] = 0 if data[j] == 'b' else 1
    data = list(map(int, input().strip().split()))
    x, y = data[0], data[1]
    res = f(nums, x - 1, y - 1)
    print(res)

#笔试题目##腾讯#
全部评论
这一题我用Python 写一定会超时(40%),牛客对Python的支持不好,但是C++能过。
1 回复 分享
发布于 2021-09-06 23:39
同python,只过40%
点赞 回复 分享
发布于 2021-09-07 10:54

相关推荐

暴杀流调参工作者:春招又试了一些岗位,现在投递很有意思,不仅要精心准备简历,投递官网还得把自己写的东西一条一条复制上去,阿里更是各个bu都有自己的官网,重复操作无数次,投完简历卡完学历了,又该写性格测评、能力测评,写完了又要写专业笔试,最近还有些公司搞了AI辅助编程笔试,有些还有AI面试,对着机器人话也听不明白录屏硬说,终于到了人工面试又要一二三四面,小组成员面主管面部门主管面hr面,次次都没出错机会,稍有不慎就是挂。 卡学历卡项目卡论文卡实习什么都卡,没有不卡的😂
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务