298B.md (1835B)
1 Source: https://codeforces.com/problemset/problem/298/B 2 3 B. Sail 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 stdin 16 17 output 18 19 stdout 20 21 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*). 22 23 * If the wind blows to the east, the boat will move to (*x* + 1, *y*). 24 * If the wind blows to the south, the boat will move to (*x*, *y* - 1). 25 * If the wind blows to the west, the boat will move to (*x* - 1, *y*). 26 * If the wind blows to the north, the boat will move to (*x*, *y* + 1). 27 28 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*)? 29 30 Input 31 32 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. 33 34 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). 35 36 Output 37 38 If they can reach (*e**x*, *e**y*) within *t* seconds, print the earliest time they can achieve it. Otherwise, print "-1" (without quotes). 39 40 Examples 41 42 Input 43 44 ``` 45 5 0 0 1 1 46 SESNW 47 ``` 48 49 Output 50 51 ``` 52 4 53 ``` 54 55 Input 56 57 ``` 58 10 5 3 3 6 59 NENSWESNEE 60 ``` 61 62 Output 63 64 ``` 65 -1 66 ``` 67 68 Note 69 70 In the first sample, they can stay at seconds 1, 3, and move at seconds 2, 4. 71 72 In the second sample, they cannot sail to the destination.