All problems
0993HardArrayBacktrackingBit ManipulationMatrixHamiltonian Path

Sweeping Every Open Bay Once

Tracked in this browser only
Write code

Trains the technique from

LeetCode 980Unique Paths III

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A warehouse floor is given as the grid floor, where each cell carries one of four markings:

  • 1 marks the single starting bay,
  • 2 marks the single finishing bay,
  • 0 marks an open bay that has to be swept,
  • -1 marks a blocked bay that cannot be entered.

A sweep begins on the starting bay and ends on the finishing bay, moving each time to a bay that shares an edge with the current one. It has to cover every open bay, and no bay may be stood on more than once. The starting and finishing bays are not open bays, and each of them is stood on exactly once, as the two ends of the sweep.

Return how many different sweeps the floor allows. Two sweeps differ when the order of bays they stand on differs.

Examples

Example 1

Input
floor = [[1, 0, 0], [0, 0, 0], [0, 0, 2]]
Output
2

Every bay is open, so all nine have to be stood on with the last one being the far corner. Going down the left column, one step right, up the middle column, one step right, then down the right column is one way. Going along the top row, one step down, back along the middle row, one step down, then along the bottom row is the other. Nothing else covers the floor.

Example 2

Input
floor = [[1, 0], [0, 2]]
Output
0

All four bays have to be stood on, which takes three steps from the starting bay. Three steps around a square of four bays always end up next door to where they began, never diagonally across, so no sweep works.

Example 3

Input
floor = [[1, -1], [0, 2]]
Output
1

The blocked bay leaves one route: down, then across.

Constraints

  • 1 <= floor.length <= 20
  • 1 <= floor[i].length <= 20
  • The grid holds at most 20 bays in all.
  • -1 <= floor[i][j] <= 2
  • There is exactly one starting bay and exactly one finishing bay.

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def unique_paths_i_i_i(floor: list[list[int]]) -> int:
Java
public int uniquePathsIII(int[][] floor)
September 7
Apply