首页 > 其他分享 >LeetCode 1730. Shortest Path to Get Food

LeetCode 1730. Shortest Path to Get Food

时间:2022-10-23 06:44:05浏览次数:73  
标签:Get Food 1730 int length grid food new que

原题链接在这里:https://leetcode.com/problems/shortest-path-to-get-food/

题目:

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.

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 200
  • grid[row][col] is '*''X''O', or '#'.
  • The grid contains exactly one '*'.

题解:

Perform BFS from the start and eventually find the food.

Time Complexity: O(m*n). m = grid.length. n = grid[0].length.

Space: O(m*n).

AC Java:

 1 class Solution {
 2     int [][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
 3     public int getFood(char[][] grid) {
 4         int m = grid.length;
 5         int n = grid[0].length;
 6         LinkedList<int[]> que = new LinkedList<>();
 7         que.add(findStart(grid));
 8         boolean[][] visited = new boolean[m][n];
 9         
10         int level = 0;
11         while(!que.isEmpty()){
12             int size = que.size();
13             while(size-- > 0){
14                 int [] cur = que.poll();
15                 if(grid[cur[0]][cur[1]] == '#'){
16                     return level;
17                 }
18                 
19                 for(int [] dir : dirs){
20                     int x = cur[0] + dir[0];
21                     int y = cur[1] + dir[1];
22                     if(x < 0 || x >= m || y < 0 || y >= n || grid[x][y] == 'X' || visited[x][y]){
23                         continue;
24                     }
25                     
26                     visited[x][y] = true;
27                     que.add(new int[]{x, y});
28                 }
29             }
30             
31             level++;
32         }
33         
34         return -1;
35     }
36     
37     private int[] findStart(char [][] grid){
38         for(int i = 0; i < grid.length; i++){
39             for(int j = 0; j < grid[0].length; j++){
40                 if(grid[i][j] == '*'){
41                     return new int[]{i, j};
42                 }
43             }
44         }
45         
46         return new int[0];
47     }
48 }

类似01 Matrix.

标签:Get,Food,1730,int,length,grid,food,new,que
From: https://www.cnblogs.com/Dylan-Java-NYC/p/16817822.html

相关文章

  • Linux热门命令,你get到多少?
    大家应该都知道Linux操作系统,就算不熟悉至少也听说过吧,由于它是开放性的,也是免费的,且安全性比较高等,近些年受到越来越多人的喜欢,不少公司也都是使用Linux系统办公。Linux中......
  • 什么,你不知道wget可以这样用?
    导读本文将介绍wget的基本使用方法,和一些高级用法,比如递归下载等。对于经常在FTP网页下载数据的读者来说,可以说是必备的技能之一。1.介绍Wget是由GNU项目创建的计算......
  • python内置方法__getitem__,__delitem__,__setitem__
    classFoo:def__init__(self,name):self.name=namedef__getitem__(self,item):print('getitem')print(item)returns......
  • linux下使用gcc编译含gets()函数的程序
    网上有很多关于gets()会导致栈溢出之类的废话也许会有初学者望着千篇一律的回答茫然无错,以为真的就只能使用fgets()了 首先你要了解gets()函数有极大的风险其次,在gcc......
  • C# HTTP POST AND GET json or xml
    usingSystem.Net;usingSystem.Net.Cache;usingSystem.IO;stringHttpPost(stringstrUrl,stringstrPostData){s......
  • How to get the ASCII value of a character Python
    HowtogettheASCIIvalueofacharacter 回答1Fromhere:Thefunctionord()getstheintvalueofthechar.Andincaseyouwanttoconvertbackafterp......
  • Android USB之复合设备(gadget)详解
    一.USBgadgetdriverUSBgadget驱动描述了USB设备控制器的硬件操作方法,不同的USB控制器实现不同。有的USB控制器只能作为设备控制器,如ompa、pxa2等USB设备控制器,其......
  • Iframe - Target for a Link
    Iframe-TargetforaLink通过超链接来切换iframe里面的内容 <!DOCTYPEhtml><html><body><h2>Iframe-TargetforaLink</h2><iframesrc="demo_iframe.htm......
  • C语言中的getchar、putchar函数
    getchar可以接受键盘上打印的字符,puchar可以进行输出字符比如:#include<stdio.h>intmain(){intch=getchar();putchar(ch);printf("%c\n",ch);return0;}运......
  • get pull报错 Please commit your changes or stash them before you merge
    当本地分支和远程修改了同一个文件代码,pull远程分支的代码的时候会出现文件冲突出现这个错误Pleasecommityourchangesorstashthembeforeyoumerge.可以先将当前......