CF793B.Igor and his way to work

普及/提高-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Woken up by the alarm clock Igor the financial analyst hurried up to the work. He ate his breakfast and sat in his car. Sadly, when he opened his GPS navigator, he found that some of the roads in Bankopolis, the city where he lives, are closed due to road works. Moreover, Igor has some problems with the steering wheel, so he can make no more than two turns on his way to his office in bank.

Bankopolis looks like a grid of n rows and m columns. Igor should find a way from his home to the bank that has no more than two turns and doesn't contain cells with road works, or determine that it is impossible and he should work from home. A turn is a change in movement direction. Igor's car can only move to the left, to the right, upwards and downwards. Initially Igor can choose any direction. Igor is still sleepy, so you should help him.

被闹钟惊醒的金融分析师伊戈尔匆匆赶去上班。他吃完早餐,坐进自己的汽车。然而,当他打开GPS导航仪时,却发现他所居住的城市——银行城(Bankopolis)中,部分道路因施工而关闭。此外,伊戈尔的汽车方向盘有些问题,因此他在前往银行办公室的途中最多只能转弯两次。

银行城的布局是一个 nn 行 mm 列的网格。伊戈尔需要找到一条从家到银行的路径,该路径至多包含两次转弯,且不经过任何正在施工的道路单元格;若不存在这样的路径,则判定为不可达,他只能在家办公。一次“转弯”指行进方向的改变。伊戈尔的汽车仅能向左、向右、向上或向下移动。初始时,伊戈尔可任选一个方向出发。此时伊戈尔仍处于困倦状态,因此请你帮助他。

输入格式

The first line contains two integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the grid.

Each of the next n lines contains m characters denoting the corresponding row of the grid. The following characters can occur:

  • "." — an empty cell;
  • "*" — a cell with road works;
  • "S" — the cell where Igor's home is located;
  • "T" — the cell where Igor's office is located.

It is guaranteed that "S" and "T" appear exactly once each.

第一行包含两个整数 nn 和 mm(1≤n,m≤10001 \leq n, m \leq 1000),分别表示网格的行数和列数。

接下来的 nn 行,每行包含 mm 个字符,表示网格的对应行。可能出现的字符如下:

  • . — 空单元格;
  • * — 正在进行道路施工的单元格;
  • S — Igor 家所在单元格;
  • T — Igor 办公室所在单元格。

保证字符 S 和 T 各恰好出现一次。

输出格式

In the only line print "YES" if there is a path between Igor's home and Igor's office with no more than two turns, and "NO" otherwise.

在唯一的一行中,如果 Igor 的家与 Igor 的办公室之间存在一条至多包含两次转向的路径,则输出 "YES";否则输出 "NO"。

输入输出样例

  • 输入#1

    5 5
    ..S..
    ****.
    T....
    ****.
    .....

    输出#1

    YES
  • 输入#2

    5 5
    S....
    ****.
    .....
    .****
    ..T..

    输出#2

    NO

说明/提示

The first sample is shown on the following picture:

In the second sample it is impossible to reach Igor's office using less that 4 turns, thus there exists no path using no more than 2 turns. The path using exactly 4 turns is shown on this picture:

第一个样例如下图所示:

在第二个样例中,无法用少于 4 次转弯到达 Igor 的办公室,因此不存在转弯次数不超过 2 次的路径。恰好使用 4 次转弯的路径如下图所示:

输入解题思路,AI测评打分。不知道怎么写?

首页