problems / 189

Suki's Magical Maze

easy bfs dfs matrix simulation 2000 ms · 256 MB

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 cells
  • T for teleport cells (the maze contains either no T or exactly two, which form a pair)
  • S for 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 S and 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 T cell otherwise behaves as a normal cell: from either T Suki can move in the 4 orthogonal directions, or jump to the other T (the pair works both ways). Jumping is optional, and every T she 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.

Example 1

Input

3 3
S..
...
...

Output

9

Example 2

Input

4 5
S.#.*
.#...
..T#.
T...#

Output

16

Example 3

Input

4 5
S.#..
.#*#.
.#.#.
.....

Output

15

Hints · reveal one at a time if you're stuck

Hint 1

Treat each cell as a vertex and the allowed moves as edges. What decides the moves out of a cell, and is a move from one cell always matched by a move back?

Hint 2

Search from S with BFS or DFS, adding each cell the first time it is reached. The rules belong to the cell you are leaving: a directional cell has one exit, and a diagonal cell has eight.

Hint 3

The two teleport cells add one extra move each, to the other cell of the pair, on top of their four orthogonal moves. Make sure taking the jump does not stop you from also trying the orthogonal moves.

report an issue Prefer your own editor? sooks prep 189 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.