zoukankan      html  css  js  c++  java
  • 1730. Shortest Path to Get Food 获得食物的最短(所以用bfs)路径

    You are starving and you want to eat food as quickly as possible. You want to find the shortest path to arrive at any food cell.

    You are given an m x n character matrix, grid, of these different types of cells:

    • '*' is your location. There is exactly one '*' cell.
    • '#' is a food cell. There may be multiple food cells.
    • 'O' is free space, and you can travel through these cells.
    • 'X' is an obstacle, and you cannot travel through these cells.

    You can travel to any adjacent cell north, east, south, or west of your current location if there is not an obstacle.

    Return the length of the shortest path for you to reach any food cell. If there is no path for you to reach food, return -1.

     

    Example 1:

    Input: grid = [["X","X","X","X","X","X"],["X","*","O","O","O","X"],["X","O","O","#","O","X"],["X","X","X","X","X","X"]]
    Output: 3
    Explanation: It takes 3 steps to reach the food.
    

    Example 2:

    Input: grid = [["X","X","X","X","X"],["X","*","X","O","X"],["X","O","X","#","X"],["X","X","X","X","X"]]
    Output: -1
    Explanation: It is not possible to reach the food.
    

    Example 3:

    Input: grid = [["X","X","X","X","X","X","X","X"],["X","*","O","X","O","#","O","X"],["X","O","O","X","O","O","X","X"],["X","O","O","O","O","#","O","X"],["X","X","X","X","X","X","X","X"]]
    Output: 6
    Explanation: There can be multiple food cells. It only takes 6 steps to reach the bottom food.

    Example 4:

    Input: grid = [["O","*"],["#","O"]]
    Output: 2
    

    Example 5:

    Input: grid = [["X","*"],["#","X"]]
    Output: -1

    抓典型吧,挺好的题

    int[][] dirs = new int[][]{{1,0},{0,1},{-1,0},{0,-1}};
    
    public int getFood(char[][] grid) {
    
        int m = grid.length;
        int n = grid[0].length;
    
        Queue<int[]> q = new LinkedList<>();
        q.add(findStart(grid));
    
        boolean[][] visited = new boolean[m][n];
    
        int step=0;
        while(!q.isEmpty()){
            int len = q.size();
            for(int i=0; i < len; i++){
                int[] pos = q.poll();
    
                int x = pos[0];
                int y = pos[1];
    
                if(grid[x][y] == '#') return step;
    
                for(int[] dir: dirs){
                    int newX = x + dir[0];
                    int newY = y + dir[1];
    
                    if(isValid(grid, newX, newY) && !visited[newX][newY]){
                        visited[newX][newY] = true;
                        q.offer(new int[]{newX, newY});
                    }
                }
            }
            step++;
        }
    
        return -1;
    }
    
    private int[] findStart(char[][] grid){
        for(int i=0; i < grid.length; i++){
            for(int j=0; j < grid[0].length; j++){
                if(grid[i][j] == '*'){
                    return new int[]{i, j};
                }
            }
        }
        throw new RuntimeException();
    }
    
    private boolean isValid(char[][] grid, int i, int j){
        return i >= 0 && i < grid.length && j >= 0 && j < grid[0].length && grid[i][j] != 'X';
    }



  • 相关阅读:
    手把手教你进行R语言的安装及安装过程中相关问题解决方案
    一篇文章助你理解Python3中字符串编码问题
    一篇文章助你理解Python2中字符串编码问题
    浅谈unicode编码和utf-8编码的关系
    网络爬虫过程中5种网页去重方法简要介绍
    手把手教你如何安装水晶易表——靠谱的安装教程
    在Windows上如何安装和彻底卸载Adobe Flash Player教程
    java 类的执行顺序
    bean的二次加工-Spring5.X后置处理器BeanPostProcessor
    Spring里面bean的生命周期里面的init和destroy方法
  • 原文地址:https://www.cnblogs.com/immiao0319/p/15201941.html
Copyright © 2011-2022 走看看