当前位置:网站首页>Interview question 08.02 Lost robot
Interview question 08.02 Lost robot
2022-06-10 04:50:00 【Zi Yan Mu Yu】
topic
Imagine a robot sitting in the upper left corner of a grid , grid r That's ok c Column . The robot can only move down or right , But you can't go to some forbidden grid ( There are obstacles ). Design an algorithm , Find the path for the robot to move from the upper left corner to the lower right corner .
Obstacles and empty positions in the grid are used respectively 1 and 0 To express .
Return to a feasible path , The path consists of the row and column numbers of the passing grid . The upper left corner of 0 That's ok 0 Column . If there is no feasible path , Return an empty array .
Example 1:
Input :
[
[0,0,0],
[0,1,0],
[0,0,0]
]
Output : [[0,0],[0,1],[0,2],[1,2],[2,2]]
explain :
The position marked with bold in the input is the path represented by the output , namely
0 That's ok 0 Column ( top left corner ) -> 0 That's ok 1 Column -> 0 That's ok 2 Column -> 1 That's ok 2 Column -> 2 That's ok 2 Column ( The lower right corner )
source : Power button (LeetCode)
link :https://leetcode.cn/problems/robot-in-a-grid-lcci
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking
It depends on the question Can only go down and right !!!!! dfs Can be
Code
class Solution:
def __init__(self):
self.r = 0
self.c = 0
self.ans = []
def pathWithObstacles(self, obstacleGrid: List[List[int]]) -> List[List[int]]:
self.r = len(obstacleGrid)
self.c = len(obstacleGrid[0])
self.findPath(0,0,obstacleGrid)
return self.ans
def findPath(self, x: int, y: int, obstacleGrid: List[List[int]]) -> bool:
if x < 0 or x >= self.r or y < 0 or y >= self.c or obstacleGrid[x][y] == 1:
return False
self.ans.append([x, y])
if x == self.r - 1 and y == self.c - 1:
return True
obstacleGrid[x][y] = 1
ok = self.findPath(x + 1, y, obstacleGrid) or self.findPath(x, y + 1, obstacleGrid)
if not ok:
self.ans.pop()
return ok
边栏推荐
- Detailed explanation of thread pool creation method
- In the introduction to beginners in the mindpole official website tutorial, I don't know much about map mapping when importing data
- 【Linux篇<Day20>】——一文入门 数据库 和 容器技术
- Mindscore1.6conda installation GPU version verification failed
- Unit test with%unittest
- S系列·删除文件夹的几种姿势
- Deep learning and CV tutorial (13) | target detection (SSD, Yolo Series)
- [depth first search] maximum product: arrangement
- 25. BOM事件
- MindSpore缺失与torch.nn.Maxpool3d和torch.nn.Avgpool3d对标的算子
猜你喜欢

Pysimplegui classic practice: how to read this Chinese character?

Gevent | use it asynchronously!

Error occurs when the mindspire tutorial is run online

25. BOM事件

2022 refrigeration and air conditioning equipment operation special operation certificate examination question bank simulated examination platform operation

Jenkinsclient | easy to use Jenkins client

在MindSpore官网容器内安装MindInsight不能在本地正常工作

得物登录组件重构

如何用全国天气预报API接口进行快速开发

js微信小游戏之打蚊子
随机推荐
Yyds dry goods inventory solution sword finger offer: rectangular coverage
OpenJudge NOI 1.13 13:人民币支付
2022.6.1-----leetcode. four hundred and seventy-three
Mindscore1.6conda installation GPU version verification failed
2022.6.7-----leetcode. eight hundred and seventy-five
使用 Locust 进行 Kubernetes 分布式性能测试
2022.5.26-----leetcode. six hundred and ninety-nine
2022 mobile crane driver examination questions and online simulation examination
Read leastereo:hierarchical neural architecture search for deep stereo matching
Apispace sunrise sunset API interface is free and easy to use
【Linux篇<Day20>】——一文入门 数据库 和 容器技术
mindconvert模型转换中clip算子报错
【UE4自动地形材质】
[joint search set] sympodial plants (number of connected blocks)
2022.5.27-----leetcode. Interview 17.11
OpenJudge NOI 1.13 14:求满足条件的3位数
Softing为艾默生提供AMS设备管理系统的连接解决方案
Openjudge noi 1.13 14: find the 3 digits that meet the conditions
2022.5.30-----leetcode. one thousand and twenty-two
如何用全国天气预报API接口进行快速开发