← 返回 meta 的题目列表Mouse and Cheese
类型:online_judge
Given a maze (grid) and a mouse starting from the start point to find the cheese. Each position in the maze can be either empty or a wall. The mouse can only move on empty positions. You need to implement two functions: move() to move the mouse to an adjacent empty position and canMove() to check if the mouse can move in a given direction. Find the shortest path for the mouse to find the cheese.
Input:
A 2D character array specifying the maze, where 'S' is the start, 'C' is the cheese, '#' is wall, and '.' is an empty position.
The start and cheese positions are marked in the maze.
Output:
An integer representing the shortest number of steps for the mouse to reach the cheese. Return -1 if not reachable.
Example 1: Input: maze = [['S', '.', '.', '#'], ['.', '#', '.', 'C']] Output: 5
Data Constraints:
The number of rows and columns in the maze is n, where 1 <= n <= 100.
Test Cases:
[['S', '.', '.', '#'], ['.', '#', '.', 'C']]
[['S', '#', '#', '#'], ['.', '.', '.', 'C']]
[['S', '.', 'C', '#'], ['.', '#', '#', '#']]
[['S', '.', '#', 'C']]
[['S', 'C']]
Example
Input
[['S', '.', '.', '#'], ['.', '#', '.', 'C']]