B. Sail

Source: Internet
Author: User
Time limit per test

1 second

Memory limit per test

256 megabytes

Input

Standard Input

Output

Standard output

The polar bears are going fishing. They plan to sail from (SX, Bytes,SY) To(EX, Bytes,EY) .
However, the boat can only sail by wind. At each second, the wind blows in one of these directions ctions: east, south, west or north. Assume the boat is currently (X, Bytes,Y) .

  • If the wind blows to the east, the boat will move(XLatency + latency 1, latency,Y).
  • If the wind blows to the south, the boat will move(X, Bytes,YExecutor-cores 1).
  • If the wind blows to the west, the boat will move(XAccept-limit 1, limit,Y).
  • If the wind blows to the north, the boat will move(X, Bytes,YLimit + limit 1).

Alternatively, they can hold the boat by the anchor. In this case, the boat stays (X, Bytes,Y) . Given the wind direction T Seconds,
What is the earliest time they sail(EX, Bytes,EY) ?

Input

The first line contains five IntegersT, Bytes,SX, Bytes,SY, Bytes,EX, Bytes,EY (1 digit ≤ Digit T Limit ≤ limit 105 , Too many-else 10 9 Limit ≤ limit S X , Bytes, S Y , Bytes, E X , Bytes,E Y Limit ≤ limit 10 9 ).
The starting location and the ending location will be different.

The second line containsTCharacters,I-Th
Character is the wind blowing direction atI-Th second. It will be one of the four possibilities: "E" (East), "S" (south), "W" (West)
And "N" (North ).

Output

If they can reach(EX, Bytes,EY)WithinTSeconds,
Print the earliest time they can achieve it. Otherwise, print"-1"(Without quotes ).

Sample test (s) Input
5 0 0 1 1 sesnw
Output
 
4
Input
10 5 3 3 6 nenswesnee
Output
 
-1
Note

In the first sample, they can stay at seconds1,3,
And move at seconds2,4.

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

Solution Description: this problem is simulated according to the requirements of the question. Each time the wind blows, the system determines whether to change the position based on the wind direction until the specified position or time ends.

 # include 
  
    # include 
    # include 
    
      # include 
     
       # include 
      
        # include 
       
         using namespace STD; int main () {int t, a, B, x, y, I; int flag; char C; scanf ("% d ", & T, & A, & B, & X, & Y); flag = 0; for (I = 1; I <= T; I ++) {CIN> C; If (C = 'n' & B 
        
          Y) {B --;} else if (C = 'E' & A 
         
           X) {A --;} if (a = x & B = y) {printf ("% d \ n", I); flag = 1; break ;}} if (flag = 0) {printf ("-1 \ n") ;}
         
        
       
      
     
    
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.