Suki's Magical Maze
Suki is exploring a mysterious maze where certain cells have magical properties. She starts at the entrance and wants to know how many cells she can reach.
The maze contains special cells with unique behaviors:
- Teleport cells (
T): From a teleport cell, Suki can move to the other, paired teleport cell in one step. - Directional cells (
^,v,<,>): When Suki enters one of these cells, she can only exit in the indicated direction (up, down, left, or right). - Diagonal cells (
*): From these cells, Suki can move in 8 directions (including diagonals) instead of just 4. - Normal cells (
.): Suki can move in 4 orthogonal directions. - Walls (
#): Impassable.
Starting from the entrance, count how many distinct cells Suki can reach by moving through the maze. She can enter any reachable cell from an adjacent reachable cell, following the movement rules of the current cell.
Input
The first line contains n and m (1 ≤ n, m ≤ 500), the dimensions of the maze.
The next n lines contain the maze, each with m characters. The maze contains:
.for normal cells#for walls^,v,<,>for directional cells*for diagonal cellsTfor teleport cells (the maze contains either noTor exactly two, which form a pair)Sfor the starting position
The starting position S behaves as a normal cell.
Output
Print the number of distinct cells reachable from the starting position S.
Notes
- Suki starts her journey at
Sand can move to adjacent cells following the movement rules. - When Suki reaches a directional cell, she can only move in that one direction from it (if that direction leads to a valid cell, otherwise she is stuck there).
- A
Tcell otherwise behaves as a normal cell: from eitherTSuki can move in the 4 orthogonal directions, or jump to the otherT(the pair works both ways). Jumping is optional, and everyTshe steps on or jumps to counts as reached. - When Suki reaches a diagonal cell (
*), she can choose to move in any of the 8 directions (including diagonals). - Walls (
#) cannot be entered. - The answer includes the starting cell
S.