力扣面试题13.机器人的运动范围

力扣面试题13.机器人的运动范围

class Solution {
    public:
    int get(int x)
    {
        int res = 0;
        while (x != 0)
        {
            res += x % 10;
            x /= 10;
        }
        return res;
    }
    // 机器人的运动范围
    int movingCount(int m, int n, int k)
    {
        if (!k) return 1;
        vector<vector<int>> temp(m, vector<int>(n, 0));
        temp[0][0] = 1;
        int res = 1;
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                if ((i == 0 && j == 0) || get(i) + get(j) > k) continue;
                if (i - 1 >= 0)
                    temp[i][j] |= temp[i - 1][j];
                if (j - 1 >= 0)
                    temp[i][j] |= temp[i][j - 1];
                res += temp[i][j];
            }
        }
        return res;
    }
};

posted on 2020-04-09 22:54  woodjay  阅读(173)  评论(0编辑  收藏  举报

导航