Treasure Map
Medium
bfs
shortest-path
grid
pathfinding
Problem Description
Find the shortest path from start (S) to treasure (T) in a grid map. The grid contains walls (#) that block movement, open paths (.), start position (S), and treasure position (T). You can move in 4 directions: up, down, left, right.
Input Format
First line contains m and n (grid dimensions). Next m lines contain the grid with characters: S (start), T (treasure), # (wall), . (path).
Output Format
Minimum steps to reach treasure from start, or -1 if impossible.
Constraints
• 1 ≤ m, n ≤ 100
• Exactly one S and one T in the grid
• Movement in 4 directions only
• Cannot move through walls (#)
• Each step counts as 1 unit
Sample Input/Output
Input:
4 4 S... .##. .... ...T
Output:
6
Explanation
Shortest path from S to T: S→right→right→right→down→down→down→T. Total steps: 6.
Solution Hints
95
Points
Points
2500ms
Time Limit
Time Limit
256MB
Memory Limit
Memory Limit
Code Editor
Register to Access Code Editor
Create a free account to solve coding problems and track your progress.
Register Now