Leetcode — 980. Unique Paths III

The link to the problem: https://leetcode.com/problems/unique-paths-iii/

On a 2-dimensional grid, there are 4 types of squares:

  • 1 represents the starting square. There is exactly one starting square.
  • 2 represents the ending square. There is exactly one ending square.
  • 0 represents empty squares we can walk over.
  • -1 represents obstacles that we cannot walk over.

Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.


If you have solved 62. Unique Paths and 63. Unique Paths II on Leetcode, you might find that both of these questions can use dynamic programming to solve it. Since there are restrictions on our moving directions and we always start from top left to bottom right in problem 62 and 63, we can use a 2D array to store the number of paths so that the number of paths to the position (i, j) will always be the aggregation of the number of paths to its top cell and its left cell.

However, you will notice that this problem is totally different than the previous two problems. Now the starting and ending positions vary in every test case, we can forward all 4 directions in each cell, and we have to walk through each cell exactly one time. It is impossible for us to use dynamic programming to solve this problem since we need to keep track of the cells that we have walked through.

Since we will always have 4 directions to head to while we are on the current cell, these 4 cells are possible to be our next step. We will try to head to all these possible cells from the current position to see if we can create new possible paths based on the current choices. And yes, this is what backtracking does — we make a choice and keep making other choices based on the choice till we cannot continue the choices, then we redo the previous choices and make another choice.

Code Logic:

Before we start to make any choices (i.e., decide which cell we are going to step on next), we should first find the starting position and the total number of cells we should traverse.

Since we need to know which cells we have visited, I will use a List to record all the visited paths. If we found the current cell is invalid, has an obstacle, or has been visited, then we should redo the choice and choose another valid cell. We will continue traversing the next possible cells until we have traversed all the cells for the current path.

Below is the 20ms code that beats 5% —

Since we are allowed to change the value of the given 2D array, we can also record the cells that we have visited in the 2D array.

After we visited the cells, we can put a stone (some obstacles) on the cell so that we won’t accidentally revisit them later. Hence, we will just set grid[i][j] to -1 and remove the stone (i.e., set grid[i][j] to 0) after we traversed all the possible paths based on the current cell.

Below is the 0ms code that beats 100% —

Click here to view all the stories posted on this blog: https://anj910.medium.com/bookmark-c95e4c52cf44