CF298B.Sail

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

The polar bears are going fishing. They plan to sail from (s__x, s__y) to (e__x, e__y). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently at (x, y).

  • If the wind blows to the east, the boat will move to (x + 1, y).
  • If the wind blows to the south, the boat will move to (x, y - 1).
  • If the wind blows to the west, the boat will move to (x - 1, y).
  • If the wind blows to the north, the boat will move to (x, y + 1).

Alternatively, they can hold the boat by the anchor. In this case, the boat stays at (x, y). Given the wind direction for t seconds, what is the earliest time they sail to (e__x, e__y)?

北极熊要去钓鱼。他们计划从 $ (s_x, s_y) $ 航行到 $ (e_x, e_y) $。然而,船只能依靠风力航行。每一秒,风会朝以下四个方向之一吹:东、南、西或北。假设船当前位于 $ (x, y) $。

  • 如果风向东吹,船将移动到 $ (x + 1, y) $。
  • 如果风向南吹,船将移动到 $ (x, y - 1) $。
  • 如果风向西吹,船将移动到 $ (x - 1, y) $。
  • 如果风向北吹,船将移动到 $ (x, y + 1) $。

此外,他们也可以抛锚固定船只。此时,船保持在 $ (x, y) $ 不动。已知接下来 $ t $ 秒内的风向,问他们最早能在第几秒到达 $ (e_x, e_y) $?

输入格式

The first line contains five integers t, s__x, s__y, e__x, e__y (1 ≤ t ≤ 105,  - 109 ≤ s__x, s__y, e__x, e__y ≤ 109). The starting location and the ending location will be different.

The second line contains t characters, the i-th character is the wind blowing direction at the i-th second. It will be one of the four possibilities: "E" (east), "S" (south), "W" (west) and "N" (north).

第一行包含五个整数 tt、sxs_x、sys_y、exe_x、eye_y(1 ≤ t ≤ 1051 \le t \le 10^5,−109 ≤ sx, sy, ex, ey ≤ 109-10^9 \le s_x, s_y, e_x, e_y \le 10^9)。起点与终点不相同。

第二行包含 tt 个字符,其中第 ii 个字符表示第 ii 秒的风向。风向为以下四种之一:“E”(东)、“S”(南)、“W”(西)和“N”(北)。

输出格式

If they can reach (e__x, e__y) within t seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes).

如果他们能在 tt 秒内到达 (ex, ey)(e_x,\,e_y),则输出其能够到达的最早时间;否则,输出 -1(不带引号)。

输入输出样例

  • 输入#1

    5 0 0 1 1
    SESNW

    输出#1

    4
  • 输入#2

    10 5 3 3 6
    NENSWESNEE

    输出#2

    -1

说明/提示

In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4.

In the second sample, they cannot sail to the destination.

在第一个样例中,他们可以在第 1 秒和第 3 秒停留,在第 2 秒和第 4 秒移动。

在第二个样例中,他们无法航行至目的地。

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

首页