Advertisement
Guest User

Untitled

a guest
Mar 6th, 2023
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. //freopen("mowing.in","r",stdin);
  8. //freopen("mowing.out","w",stdout);
  9. int n;
  10. cin>>n;
  11. pair<char,int> niza[n];
  12. for(int i=0; i<n; i++)
  13. {
  14. cin>>niza[i].first>>niza[i].second;
  15. }
  16. int x=350;
  17. int y=350;
  18. int vis[700][700];
  19. memset(vis,-1,sizeof(vis));
  20. vis[x][y]=0;
  21. int currstep=1;
  22. int MAX=0;
  23. for(int i=0; i<n; i++)
  24. {
  25. char direction=niza[i].first;
  26. int steps=niza[i].second;
  27. for(int q=0; q<steps; q++)
  28. {
  29. if(direction=='N')
  30. {
  31. y--;
  32. if(vis[x][y]!=-1)
  33. {
  34. cout<<currstep-vis[x][y]<<endl;
  35. return 0;
  36. }
  37. vis[x][y]=currstep;
  38. currstep++;
  39. }
  40. else if(direction=='S')
  41. {
  42. y++;
  43. if(vis[x][y]!=-1)
  44. {
  45. cout<<currstep-vis[x][y]<<endl;
  46. return 0;
  47. }
  48. vis[x][y]=currstep;
  49. currstep++;
  50. }
  51. else if(direction=='W')
  52. {
  53. x--;
  54. if(vis[x][y]!=-1)
  55. {
  56. cout<<currstep-vis[x][y]<<endl;
  57. return 0;
  58. }
  59. vis[x][y]=currstep;
  60. currstep++;
  61. }
  62. else
  63. {
  64. x++;
  65. if(vis[x][y]!=-1)
  66. {
  67. cout<<currstep-vis[x][y]<<endl;
  68. return 0;
  69. }
  70. vis[x][y]=currstep;
  71. currstep++;
  72. }
  73. }
  74. }
  75. cout<<-1<<endl;
  76. return 0;
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement