Advertisement
tepyotin2

Bessie’s Question

Jan 4th, 2024
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int n;
  6. int matrix[2001][2001];
  7. // memset(matrix, -1, sizeof(matrix));
  8.  
  9. int main(){
  10.     ios_base::sync_with_stdio(0), cin.tie(0);
  11.     // freopen("question.in", "r", stdin);
  12.     cin >> n;
  13.     int row = 1000;
  14.     int col = 1000;
  15.     int ans = INT_MAX;
  16.     char dir;
  17.     int steps;
  18.     // for(int i=1; i<=10; i++){
  19.     //     for(int j=1; j<=10; j++){
  20.     //         cout << matrix[i][j] << " ";
  21.     //     }
  22.     //     cout << '\n';
  23.     // }
  24.     int curtime = 0;
  25.     int revisit = 0;
  26.     for(int i=1; i<=n; i++){
  27.         cin >> dir >> steps;
  28.         for(int j=1; j<=steps; j++){
  29.             curtime++;
  30.             if(dir == 'N'){
  31.                 row++;
  32.             }else if(dir == 'E'){
  33.                 col++;
  34.             }else if(dir == 'S'){
  35.                 row--;
  36.             }else if(dir == 'W'){
  37.                 col--;
  38.             }
  39.             if(matrix[row][col] != 0){
  40.                 revisit = curtime-matrix[row][col];
  41.                 ans = min(revisit, ans);
  42.             }
  43.             matrix[row][col] = curtime;
  44.         }
  45.     }
  46.     if(ans == INT_MAX){
  47.         ans = -1;
  48.     }
  49.     cout << ans << '\n';
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement