Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int n;
- int matrix[2001][2001];
- // memset(matrix, -1, sizeof(matrix));
- int main(){
- ios_base::sync_with_stdio(0), cin.tie(0);
- // freopen("question.in", "r", stdin);
- cin >> n;
- int row = 1000;
- int col = 1000;
- int ans = INT_MAX;
- char dir;
- int steps;
- // for(int i=1; i<=10; i++){
- // for(int j=1; j<=10; j++){
- // cout << matrix[i][j] << " ";
- // }
- // cout << '\n';
- // }
- int curtime = 0;
- int revisit = 0;
- for(int i=1; i<=n; i++){
- cin >> dir >> steps;
- for(int j=1; j<=steps; j++){
- curtime++;
- if(dir == 'N'){
- row++;
- }else if(dir == 'E'){
- col++;
- }else if(dir == 'S'){
- row--;
- }else if(dir == 'W'){
- col--;
- }
- if(matrix[row][col] != 0){
- revisit = curtime-matrix[row][col];
- ans = min(revisit, ans);
- }
- matrix[row][col] = curtime;
- }
- }
- if(ans == INT_MAX){
- ans = -1;
- }
- cout << ans << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement