パナソニックグループ プログラミングコンテスト2025(AtCoder Beginner Contest 427)
E
Wind Cleaning
AtCoder
⌀ 1734
原题
←F →
翻译中…

Problem Statement

翻译中…

There is a grid with $H$ rows and $W$ columns. Let $(i, j)$ denote the cell at the $i$-th row from the top and $j$-th column from the left.

翻译中…

Takahashi is on one of the cells in this grid, and there is trash on some cells of the grid.

翻译中…

The states of the cells are given by $H$ strings $S_1, S_2, \ldots, S_H$ of length $W$, where $S_{i, j}$ denotes the $j$-th character of $S_i$ and represents the following state:

翻译中…
  • If $S_{i, j} =$ T, cell $(i, j)$ is the cell where Takahashi is.
  • If $S_{i, j} =$ #, cell $(i, j)$ is a cell with trash.
  • If $S_{i, j} =$ ., cell $(i, j)$ is an empty cell without trash.
翻译中…

There is no trash on the cell where Takahashi is.

翻译中…

He can repeatedly perform the following operation:

翻译中…
  • Choose one of the four directions (up, down, left, or right), and move all trash simultaneously one cell in that direction. Here, if trash goes outside the grid, the trash disappears, and if trash moves to the cell where he is, he gets dirty.
翻译中…

Determine whether it is possible for him to make all trash disappear without getting dirty, and if possible, find the minimum possible number of operations.

翻译中…

Constraints

翻译中…
  • $2 \leq H, W \leq 12$
  • $S_i$ is a string of length $W$ consisting of T, #, and ..
  • There is exactly one $(i, j)$ such that $S_{i, j} =$ T.
  • There is at least one $(i, j)$ such that $S_{i, j} =$ #.
翻译中…

Input

翻译中…

The input is given from Standard Input in the following format:

翻译中…

H W
S_1
S_2
\vdots
S_H

翻译中…

Output

翻译中…

If it is possible for Takahashi to make all trash disappear without getting dirty, print the minimum possible number of operations. Otherwise, print -1.

翻译中…

样例

Input #1
3 4
###.
#.T.
...#
Output #1
3
Takahashi can make all trash disappear in three operations as follows: - Choose up. After this operation, trash is on cells $(1, 1)$ and $(2, 4)$. - Choose right. After this operation, trash is on cell $(1, 2)$. - Choose up. After this operation, all trash has disappeared.
Input #2
3 3
###
#T#
###
Output #2
-1
Input #3
5 5
###..
#T...
..##.
##..#
#..#.
Output #3
5
题目信息

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。