栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

LeetCode(剑指 Offer)- 13. 机器人的运动范围

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

LeetCode(剑指 Offer)- 13. 机器人的运动范围

题目链接:点击打开链接

题目大意:略

解题思路

相关企业

  • Facebook

AC 代码

  • Java
// 解决方案(1)
class Solution {

    private int row, col, count, limit;

    private boolean[][] path, no;

    private int[] calc;

    public int movingCount(int m, int n, int k) {
        limit = k;
        count = 0;
        row = m;
        col = n;
        path = new boolean[m][n];
        no = new boolean[m][n];
        calc = new int[m * n];
        dfs(0, 0);
        return count;
    }

    private void dfs(int i, int j) {
        if (i < 0 || i >= row || j < 0 || j >= col || path[i][j]) {
            return;
        }

        path[i][j] = true;

        if (cal(i) + cal(j) <= limit) {
            count++;
        } else {
            no[i][j] = true;
            // 此时站在限制位置, 必须马上回退, 这个最容易忘记
            return;
        }

        // 接下来 4 个 if 为的是不进限制位置
        if (i+1 < row && !no[i+1][j]) {
            dfs(i + 1, j);
        }

        if (i-1 >= 0 && !no[i-1][j]) {
            dfs(i - 1, j);
        }

        if (j+1 < col && !no[i][j+1]) {
            dfs(i, j + 1);
        }

        if (j-1 >= 0 && !no[i][j-1]) {
            dfs(i, j - 1);
        }
    }

    private int cal(int num) {
        int tnum = num;
        if (calc[tnum] != 0) {
            return calc[tnum];
        }
        int sum = 0;
        while (num != 0) {
            sum += num % 10;
            num /= 10;
        }
        calc[tnum] = sum;
        return sum;
    }
}

// 解决方案(2)
class Solution {
    int m, n, k;
    boolean[][] visited;
    public int movingCount(int m, int n, int k) {
        this.m = m; this.n = n; this.k = k;
        this.visited = new boolean[m][n];
        return dfs(0, 0, 0, 0);
    }
    public int dfs(int i, int j, int si, int sj) {
        if(i >= m || j >= n || k < si + sj || visited[i][j]) return 0;
        visited[i][j] = true;
        return 1 + dfs(i + 1, j, (i + 1) % 10 != 0 ? si + 1 : si - 8, sj) + dfs(i, j + 1, si, (j + 1) % 10 != 0 ? sj + 1 : sj - 8);
    }
}

// 解决方案(3)
class Solution {
    public int movingCount(int m, int n, int k) {
        boolean[][] visited = new boolean[m][n];
        int res = 0;
        Queue queue= new LinkedList();
        queue.add(new int[] { 0, 0, 0, 0 });
        while(queue.size() > 0) {
            int[] x = queue.poll();
            int i = x[0], j = x[1], si = x[2], sj = x[3];
            if(i >= m || j >= n || k < si + sj || visited[i][j]) continue;
            visited[i][j] = true;
            res ++;
            queue.add(new int[] { i + 1, j, (i + 1) % 10 != 0 ? si + 1 : si - 8, sj });
            queue.add(new int[] { i, j + 1, si, (j + 1) % 10 != 0 ? sj + 1 : sj - 8 });
        }
        return res;
    }
}
  • C++
// 解决方案(1)
class Solution {
public:
    int movingCount(int m, int n, int k) {
        vector> visited(m, vector(n, 0));
        return dfs(0, 0, 0, 0, visited, m, n, k);
    }
private:
    int dfs(int i, int j, int si, int sj, vector> &visited, int m, int n, int k) {
        if(i >= m || j >= n || k < si + sj || visited[i][j]) return 0;
        visited[i][j] = true;
        return 1 + dfs(i + 1, j, (i + 1) % 10 != 0 ? si + 1 : si - 8, sj, visited, m, n, k) +
                   dfs(i, j + 1, si, (j + 1) % 10 != 0 ? sj + 1 : sj - 8, visited, m, n, k);
    }
};

// 解决方案(2)
class Solution {
public:
    int movingCount(int m, int n, int k) {
        vector> visited(m, vector(n, 0));
        int res = 0;
        queue> que;
        que.push({ 0, 0, 0, 0 });
        while(que.size() > 0) {
            vector x = que.front();
            que.pop();
            int i = x[0], j = x[1], si = x[2], sj = x[3];
            if(i >= m || j >= n || k < si + sj || visited[i][j]) continue;
            visited[i][j] = true;
            res++;
            que.push({ i + 1, j, (i + 1) % 10 != 0 ? si + 1 : si - 8, sj });
            que.push({ i, j + 1, si, (j + 1) % 10 != 0 ? sj + 1 : sj - 8 });
        }
        return res;
    }
};
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/820864.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号