Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Twisted Mirror Path Count

XPChallenge Points: 20
levelLevel: Medium

You are given a 2D grid of size m x n, where each cell either contains:

  • 0 → an open space

  • 1 → a mirror tile

A robot starts at position (0, 0) and must reach (m-1, n-1). It may only move right or down.

However, when the robot tries to move into a mirror cell, it does not enter the mirror directly. Instead, it gets redirected:

  • If it was moving right, it is redirected down.

  • If it was moving down, it is redirected right.

This redirection continues if the robot is redirected into another mirror cell, meaning mirror reflections can chain repeatedly.

If at any point the redirection would move the robot outside the grid, then that path is considered invalid.

Your task is to compute how many distinct valid paths lead from the start to the goal cell.
Return the result modulo 10⁹ + 7.

Example 1:

Input: grid = [[0,0,1], [1,0,0], [0,1,0]]

Output: 1

Explanation:

There are 1 valid reflected paths that successfully reach the bottom-right cell.

Example 2:

Input: grid = [[1,1],[1,1]]

Output: 0

Explanation:

Every move leads to reflection loops or out-of-bounds.

Example 3:

Input: grid = [[1,0],[0,0]]

Output: 0

Explanation:

Only one possible path avoids going out of bounds after reflections.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result