Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main()
- {
- //freopen("mowing.in","r",stdin);
- //freopen("mowing.out","w",stdout);
- int n;
- cin>>n;
- pair<char,int> niza[n];
- for(int i=0; i<n; i++)
- {
- cin>>niza[i].first>>niza[i].second;
- }
- int x=350;
- int y=350;
- int vis[700][700];
- memset(vis,-1,sizeof(vis));
- vis[x][y]=0;
- int currstep=1;
- int MAX=0;
- for(int i=0; i<n; i++)
- {
- char direction=niza[i].first;
- int steps=niza[i].second;
- for(int q=0; q<steps; q++)
- {
- if(direction=='N')
- {
- y--;
- if(vis[x][y]!=-1)
- {
- cout<<currstep-vis[x][y]<<endl;
- return 0;
- }
- vis[x][y]=currstep;
- currstep++;
- }
- else if(direction=='S')
- {
- y++;
- if(vis[x][y]!=-1)
- {
- cout<<currstep-vis[x][y]<<endl;
- return 0;
- }
- vis[x][y]=currstep;
- currstep++;
- }
- else if(direction=='W')
- {
- x--;
- if(vis[x][y]!=-1)
- {
- cout<<currstep-vis[x][y]<<endl;
- return 0;
- }
- vis[x][y]=currstep;
- currstep++;
- }
- else
- {
- x++;
- if(vis[x][y]!=-1)
- {
- cout<<currstep-vis[x][y]<<endl;
- return 0;
- }
- vis[x][y]=currstep;
- currstep++;
- }
- }
- }
- cout<<-1<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement