查看: 890| 回复: 1
跳转到指定楼层
上一主题 下一主题
收起左侧

[树/链表/图] 请问我这个the Maze ii的解法错在哪儿了

全局:
1小米
https://leetcode.com/problems/the-maze-ii

提交有错,出错的test case的input array超大,可是自己却没有找到能出错的input array比较小的test case来debug,大牛们给看看?

class Solution {
    public int shortestDistance(int[][] maze, int[] start, int[] destination) {
        Map<Integer, Integer> memo = new HashMap<>();
        Set<Integer> path = new HashSet<>();
        int shortest = dfsShortest(maze, start, destination, memo, path);
        return shortest == Integer.MAX_VALUE ? -1 : shortest;
    }

    private int dfsShortest(int[][] maze, int[] start, int[] dest, Map<Integer, Integer> memo, Set<Integer> path) {
        if (start[0] == dest[0] && start[1] == dest[1]) return 0;
        int startIdx = start[0]*maze[0].length+start[1];
        if (memo.get(startIdx) != null) return memo.get(startIdx);
        path.add(startIdx);
        int res = Integer.MAX_VALUE;
        for (int[] next : findNexts(maze, start)) {
            int nextIdx = next[0]*maze[0].length+next[1];
            if (!path.contains(nextIdx)) {
                int stepSize = Math.abs(next[0]-start[0]) + Math.abs(next[1]-start[1]);
                int nextToDest = dfsShortest(maze, next, dest, memo, path);
                if (nextToDest != Integer.MAX_VALUE) {
                    res = Math.min(res, nextToDest+stepSize);
                }               
            }
        }
        path.remove(startIdx);
        memo.put(startIdx, res);
        return res;
    }

    private List<int[]> findNexts(int[][] maze, int[] pos) {
        List<int[]> res = new ArrayList<>();
        int posX = pos[0];
        while (posX-1 >= 0 && maze[posX-1][pos[1]] == 0) posX--;
        if (posX < pos[0] && posX >= 0) res.add(new int[]{posX, pos[1]});
        posX = pos[0];
        while (posX+1 < maze.length && maze[posX+1][pos[1]] == 0) posX++;
        if (posX > pos[0] && posX < maze.length) res.add(new int[]{posX, pos[1]});
        int posY = pos[1];
        while (posY-1 >= 0 && maze[pos[0]][posY-1] == 0) posY--;
        if (posY < pos[1] && posY >= 0) res.add(new int[]{pos[0], posY});
        posY = pos[1];
        while (posY+1 < maze[0].length && maze[pos[0]][posY+1] == 0) posY++;
        if (posY > pos[1] && posY < maze[0].length) res.add(new int[]{pos[0], posY});
        return res;
    }
}


上一篇:大清早发现个Leetcode bug
下一篇:求助各位前辈!!!
🔗
 楼主| casunshine 2021-5-22 23:46:46 | 只看该作者
全局:
  1. class Solution {
  2.     public int shortestDistance(int[][] maze, int[] start, int[] destination) {
  3.         Map<Integer, Integer> memo = new HashMap<>();
  4.         Set<Integer> path = new HashSet<>();
  5.         int shortest = dfsShortest(maze, start, destination, memo, path);
  6.         return shortest == Integer.MAX_VALUE ? -1 : shortest;
  7.     }

  8.     private int dfsShortest(int[][] maze, int[] start, int[] dest, Map<Integer, Integer> memo, Set<Integer> path) {
  9.         if (start[0] == dest[0] && start[1] == dest[1]) return 0;
  10.         int startIdx = start[0]*maze[0].length+start[1];
  11.         if (memo.get(startIdx) != null) return memo.get(startIdx);
  12.         path.add(startIdx);
  13.         int res = Integer.MAX_VALUE;
  14.         for (int[] next : findNexts(maze, start)) {
  15.             int nextIdx = next[0]*maze[0].length+next[1];
  16.             if (!path.contains(nextIdx)) {
  17.                 int stepSize = Math.abs(next[0]-start[0]) + Math.abs(next[1]-start[1]);
  18.                 int nextToDest = dfsShortest(maze, next, dest, memo, path);
  19.                 if (nextToDest != Integer.MAX_VALUE) {
  20.                     res = Math.min(res, nextToDest+stepSize);
  21.                 }               
  22.             }
  23.         }
  24.         path.remove(startIdx);
  25.         memo.put(startIdx, res);
  26.         return res;
  27.     }

  28.     private List<int[]> findNexts(int[][] maze, int[] pos) {
  29.         List<int[]> res = new ArrayList<>();
  30.         int posX = pos[0];
  31.         while (posX-1 >= 0 && maze[posX-1][pos[1]] == 0) posX--;
  32.         if (posX < pos[0] && posX >= 0) res.add(new int[]{posX, pos[1]});
  33.         posX = pos[0];
  34.         while (posX+1 < maze.length && maze[posX+1][pos[1]] == 0) posX++;
  35.         if (posX > pos[0] && posX < maze.length) res.add(new int[]{posX, pos[1]});
  36.         int posY = pos[1];
  37.         while (posY-1 >= 0 && maze[pos[0]][posY-1] == 0) posY--;
  38.         if (posY < pos[1] && posY >= 0) res.add(new int[]{pos[0], posY});
  39.         posY = pos[1];
  40.         while (posY+1 < maze[0].length && maze[pos[0]][posY+1] == 0) posY++;
  41.         if (posY > pos[1] && posY < maze[0].length) res.add(new int[]{pos[0], posY});
  42.         return res;
  43.     }
  44. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表