剑指offer:机器人的运动范围

本文讨论了一位程序员在实现移动计数算法时遇到的问题,由于将`++hh`错误地写成了`hh++`,导致算法不正确。作者通过修复这个迭代器使用错误,实现了正确的功能。代码涉及到了队列、二维数组和广度优先搜索(BFS)策略。
摘要由CSDN通过智能技术生成

这道题我太傻逼了,我入队的时候,++ hh 写成 hh ++ 了。。
原题链接:https://www.acwing.com/problem/content/description/22/
在这里插入图片描述

class Solution {
public:
    
    int get_sum(int x, int y)
    {
        int sum = 0;
        while(x)
        {
            sum += x % 10;
            x /= 10;
        }
        
        while(y)
        {
            sum += y % 10;
            y /= 10;
        }
        
        return sum;
    }
    
    int movingCount(int threshold, int rows, int cols)
    {
        if (!rows || !cols) return 0;
        int sum = 0;
        bool st[rows][cols];
        memset(st, 0, sizeof st);
        pair<int, int> q[2510];
        int hh = 0;
        q[hh] = {0, 0};
        st[0][0] = true;
        sum ++;
        
        int dx[] = {0, -1, 0, 1}, dy[] = {-1, 0, 1, 0};
        // int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
        
        while(hh != -1)
        {
            int tx = q[hh].first, ty = q[hh].second;
            hh --;
            // printf("出栈的是%d, %d\n", tx, ty);
            
            for(int i = 0; i < 4; i ++)
            {
                int x = tx + dx[i], y = ty + dy[i];
                if(x >= 0 && x < rows && y >= 0 && 
                   y < cols && get_sum(x, y) <= threshold && !st[x][y])
                {
                    st[x][y] = true;
                    // 我是真的傻逼,这里++hh写成hh++了。
                    q[++hh] = {x, y};
                    // printf("%d %d\n", x, y);
                    sum ++;
                }
            }
        }
        
        // cout << get_sum(14, 10) << endl;
        return sum;
    }
};
from collections import deque

class Solution(object):
    
    def movingCount(self, threshold, rows, cols):
        """
        :type threshold: int
        :type rows: int
        :type cols: int
        :rtype: int
        """
        queue = deque()
        queue.append((0, 0))
        st = [[False] * cols for _ in range(rows)]
        dx = [0, 1, 0, -1]
        dy = [1, 0, -1, 0]
        res = 0
        while queue:
            x, y = queue.popleft()
            if x >= 0 and x < rows and y >= 0 \
                and y < cols and not st[x][y] and \
                self.getSum(x, y) <= threshold:
                    res += 1
                    st[x][y] = True
                    for i in range(4):
                        nx = x + dx[i]
                        ny = y + dy[i]
                        queue.append((nx, ny))
        
        return res
        
    def getSum(self, x, y):
        sum = 0
        while x:
            sum += x % 10
            x //= 10
        while y:
            sum += y % 10
            y //= 10
        return sum
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值