Robot Grid Path Planning

AlgorithmAlgorithmBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this challenge, we will implement an algorithm to move a robot from the upper left corner to the bottom right corner of a rectangular grid. The robot can only move right and down, and some cells may be off-limits.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL algorithm(("`Algorithm`")) -.-> algorithm/BasicAlgorithmsGroup(["`Basic Algorithms`"]) python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) algorithm/BasicAlgorithmsGroup -.-> algorithm/recursion_dynamic("`Recursion Dynamic`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills algorithm/recursion_dynamic -.-> lab-268858{{"`Robot Grid Path Planning`"}} python/booleans -.-> lab-268858{{"`Robot Grid Path Planning`"}} python/conditional_statements -.-> lab-268858{{"`Robot Grid Path Planning`"}} python/lists -.-> lab-268858{{"`Robot Grid Path Planning`"}} python/tuples -.-> lab-268858{{"`Robot Grid Path Planning`"}} python/function_definition -.-> lab-268858{{"`Robot Grid Path Planning`"}} python/classes_objects -.-> lab-268858{{"`Robot Grid Path Planning`"}} python/encapsulation -.-> lab-268858{{"`Robot Grid Path Planning`"}} python/build_in_functions -.-> lab-268858{{"`Robot Grid Path Planning`"}} end

Grid Path

Problem

Given a rectangular grid with valid and invalid cells, implement a function to find a valid path for the robot to move from the upper left corner to the bottom right corner. If there is no valid path, return None. If the input is invalid or the matrix is empty, return None.

Requirements

The requirements for this algorithm are as follows:

  • The robot can only move right and down.
  • Some cells may be off-limits.
  • The grid is rectangular and not jagged.
  • There may not always be a valid path for the robot to reach the bottom right corner.
  • The input may not always be valid.
  • The algorithm should fit within memory constraints.

Example Usage

Consider the following grid:

o = valid cell
x = invalid cell

   0  1  2  3
0  o  o  o  o
1  o  x  o  o
2  o  o  x  o
3  x  o  o  o
4  o  o  x  o
5  o  o  o  x
6  o  x  o  x
7  o  x  o  o
  • General case:
expected = [(0, 0), (1, 0), (2, 0),
            (2, 1), (3, 1), (4, 1),
            (5, 1), (5, 2), (6, 2),
            (7, 2), (7, 3)]
  • No valid path: In the above example, row 7 col 2 is also invalid -> None
  • None input -> None
  • Empty matrix -> None

Summary

In this challenge, we implemented an algorithm to move a robot from the upper left corner to the bottom right corner of a rectangular grid. The robot can only move right and down, and some cells may be off-limits. We also defined the requirements for the algorithm and provided example usage scenarios.

Other Algorithm Tutorials you may like