Find the Fewest Tile Rotations That Connect Start to Finish on a Pipe Grid

Quick Overview

Find the fewest 90-degree tile rotations that connect a start cell to a finish cell on a grid of straight and L-shaped tiles. Tests modeling tile orientations as search states, shortest-path costs for rotations, and handling the fact that each tile can hold only one orientation along a path.

Find the Fewest Tile Rotations That Connect Start to Finish on a Pipe Grid

Company: Accenture

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

You are playing a pathfinding puzzle on a grid of size `rows x cols`. Each cell holds a tile encoded as an integer: - `0`: empty cell (no path through it). - `1`: horizontal straight (connects left and right). - `2`: vertical straight (connects up and down). - `3`: L-bend (connects up and right). - `4`: L-bend (connects right and down). - `5`: L-bend (connects down and left). You may rotate any tile by 90 degrees, clockwise or counter-clockwise. Each 90-degree rotation costs one move. Empty cells cannot be rotated or traversed. Rotating a straight tile once toggles it between horizontal and vertical. Rotating an L-bend cycles it through its four possible orientations. Given `grid`, the `start` cell `[r1, c1]` and the `finish` cell `[r2, c2]`, return the minimum number of rotations needed so that there is a connected path of tiles from `start` to `finish` in which adjacent tiles on the path connect to each other through their openings. If it is impossible, return `-1`. ### Function Signature ```python def min_rotations(grid: list[list[int]], start: list[int], finish: list[int]) -> int: ``` ### Rules - The four sides of a cell are up, right, down and left. Two side-adjacent cells connect when each tile has an opening on the side that faces the other. - The cost of a tile is the fewest quarter turns that take it from its given orientation to the orientation it has in the final grid. A straight tile costs 0 or 1. An L-bend costs 0, 1 or 2 (three turns one way equal one turn the other way). - A path is a sequence of distinct non-empty cells that begins at `start`, ends at `finish`, and in which each cell is side-adjacent to the next one and connects to it. Every tile has exactly one orientation, so no cell appears twice on a path. - Only the openings the path uses matter. The second opening of the `start` tile and of the `finish` tile may point at the grid edge, an empty cell or any other tile. Tiles off the path never need rotating. - Return the minimum total cost over all paths. If `start` or `finish` is an empty cell, return `-1`. If `start` equals `finish` and that cell is not empty, return `0`. ### Constraints - `1 <= rows, cols <= 10` - Each `grid[r][c]` is an integer from `0` to `5`. - `0 <= start[0], finish[0] < rows` - `0 <= start[1], finish[1] < cols` ### Examples Example 1 ```text Input: grid = [[1,1,1]], start = [0,0], finish = [0,2] Output: 0 ``` All three tiles are already horizontal straights, so the row is a connected path from (0,0) to (0,2) without any rotation. Example 2 ```text Input: grid = [[3,5],[4,3]], start = [0,0], finish = [1,1] Output: 0 ``` Tile (0,0) opens up and right, tile (0,1) opens down and left, and tile (1,1) opens up and right. So (0,0) connects to (0,1), (0,1) connects down to (1,1), and no tile has to turn. Example 3 ```text Input: grid = [[0,4,5],[1,1,5],[5,2,0]], start = [1,0], finish = [2,1] Output: 4 ``` The only valid path is (1,0), (2,0), (2,1): turning (1,0) vertical costs 1, turning (2,0) to open up and right costs 2, and turning (2,1) horizontal costs 1. A route of cost 2 goes right through (1,1), loops through (1,2), (0,2) and (0,1), and comes back down through (1,1) to the finish, but it needs (1,1) horizontal on the first pass and vertical on the second, which one tile cannot be.

Overview: Find the fewest 90-degree tile rotations that connect a start cell to a finish cell on a grid of straight and L-shaped tiles. Tests modeling tile orientations as search states, shortest-path costs for rotations, and handling the fact that each tile can hold only one orientation along a path.

Accenture logo
Accenture
Nov 25, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

You are playing a pathfinding puzzle on a grid of size rows x cols. Each cell holds a tile encoded as an integer:

  • 0 : empty cell (no path through it).
  • 1 : horizontal straight (connects left and right).
  • 2 : vertical straight (connects up and down).
  • 3 : L-bend (connects up and right).
  • 4 : L-bend (connects right and down).
  • 5 : L-bend (connects down and left).

You may rotate any tile by 90 degrees, clockwise or counter-clockwise. Each 90-degree rotation costs one move. Empty cells cannot be rotated or traversed. Rotating a straight tile once toggles it between horizontal and vertical. Rotating an L-bend cycles it through its four possible orientations.

Given grid, the start cell [r1, c1] and the finish cell [r2, c2], return the minimum number of rotations needed so that there is a connected path of tiles from start to finish in which adjacent tiles on the path connect to each other through their openings. If it is impossible, return -1.

Function Signature

def min_rotations(grid: list[list[int]], start: list[int], finish: list[int]) -> int:

Rules

  • The four sides of a cell are up, right, down and left. Two side-adjacent cells connect when each tile has an opening on the side that faces the other.
  • The cost of a tile is the fewest quarter turns that take it from its given orientation to the orientation it has in the final grid. A straight tile costs 0 or 1. An L-bend costs 0, 1 or 2 (three turns one way equal one turn the other way).
  • A path is a sequence of distinct non-empty cells that begins at start , ends at finish , and in which each cell is side-adjacent to the next one and connects to it. Every tile has exactly one orientation, so no cell appears twice on a path.
  • Only the openings the path uses matter. The second opening of the start tile and of the finish tile may point at the grid edge, an empty cell or any other tile. Tiles off the path never need rotating.
  • Return the minimum total cost over all paths. If start or finish is an empty cell, return -1 . If start equals finish and that cell is not empty, return 0 .

Constraints

  • 1 <= rows, cols <= 10
  • Each grid[r][c] is an integer from 0 to 5 .
  • 0 <= start[0], finish[0] < rows
  • 0 <= start[1], finish[1] < cols

Examples

Example 1

Input: grid = [[1,1,1]], start = [0,0], finish = [0,2]
Output: 0

All three tiles are already horizontal straights, so the row is a connected path from (0,0) to (0,2) without any rotation.

Example 2

Input: grid = [[3,5],[4,3]], start = [0,0], finish = [1,1]
Output: 0

Tile (0,0) opens up and right, tile (0,1) opens down and left, and tile (1,1) opens up and right. So (0,0) connects to (0,1), (0,1) connects down to (1,1), and no tile has to turn.

Example 3

Input: grid = [[0,4,5],[1,1,5],[5,2,0]], start = [1,0], finish = [2,1]
Output: 4

The only valid path is (1,0), (2,0), (2,1): turning (1,0) vertical costs 1, turning (2,0) to open up and right costs 2, and turning (2,1) horizontal costs 1. A route of cost 2 goes right through (1,1), loops through (1,2), (0,2) and (0,1), and comes back down through (1,1) to the finish, but it needs (1,1) horizontal on the first pass and vertical on the second, which one tile cannot be.