298B - Sail - CodeForces Solution


brute force greedy implementation *1200

Please click on ads to support us..

Python Code:

a = list(map(int, input().split()))[1:]
s = input()

x = a[2] - a[0]
y = a[3] - a[1]

if x>0: cx = 'E'
else: cx = 'W'

if y>0: cy = 'N'
else: cy = 'S'


x = abs(x)
y = abs(y)

for i in range(len(s)):

    if s[i] == cx:
        x-=1
    if s[i] == cy:
        y-=1

    if x<=0 and y<=0:
        print(i+1)
        exit()

print(-1)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,i,j,k=0,l=0,x,y,sx,sy,ex,ey,e=0,so=0,w=0,no=0,ans=0;
    cin>>n;
    cin>>sx>>sy>>ex>>ey;
    string s;
    cin>>s;
    x=sx-ex;
    y=sy-ey;
    if(x<0)
    {
        e=(x);
        for(i=0; i<s.size(); i++)
        {
            if(s[i]=='E')
                e++;
            if(e==0)
            {
                ans=max(ans,i);
                break;
            }
        }
    }
    else if(x>0)
    {
        w=(x);
        for(i=0; i<s.size(); i++)
        {
            if(s[i]=='W')
                w--;
            if(w==0)
            {
                ans=max(ans,i);
                break;
            }
        }
    }
    if(y<0)
    {
        no=(y);
        for(i=0; i<s.size(); i++)
        {
            if(s[i]=='N')
                no++;
            if(no==0)
            {
                ans=max(ans,i);
                break;
            }
        }
    }
    else if(y>0)
    {
        so=(y);
        for(i=0; i<s.size(); i++)
        {
            if(s[i]=='S')
                so--;
            if(so==0)
            {
                ans=max(ans,i);
                break;
            }
        }
    }
    if(so==0 && no==0 && w==0 && e==0)
    {
        cout<<ans+1<<endl;
    }
    else
        cout<<"-1"<<endl;
}


Comments

Submit
0 Comments
More Questions

1176A - Divide it
1527A - And Then There Were K
1618E - Singers' Tour
1560B - Who's Opposite
182B - Vasya's Calendar
934A - A Compatible Pair
1618F - Reverse
1684C - Column Swapping
57C - Array
1713D - Tournament Countdown
33A - What is for dinner
810A - Straight A
1433C - Dominant Piranha
633A - Ebony and Ivory
1196A - Three Piles of Candies
299A - Ksusha and Array
448B - Suffix Structures
1092B - Teams Forming
1166C - A Tale of Two Lands
544B - Sea and Islands
152B - Steps
1174D - Ehab and the Expected XOR Problem
1511A - Review Site
1316A - Grade Allocation
838A - Binary Blocks
1515D - Phoenix and Socks
1624D - Palindromes Coloring
1552F - Telepanting
1692G - 2Sort
1191A - Tokitsukaze and Enhancement