ABDELRHMAN_SAEED007

(USACO) Lasers and Mirrors

Aug 23rd, 2025
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. // #define int long long
  5. #define ld long double
  6. #define F first
  7. #define S second
  8. #define el '\n'
  9. #define cout(x) for(auto v:x)cout<<v<<' ';cout<<el
  10. #define coutp(x) for(auto v:x)cout<<v.F<<' '<<v.S<<el
  11. #define cin(x) for(auto &v:x)cin>>v;
  12. #define all(x)  x.begin(),x.end()
  13. #define ll long long
  14. #define sz(x)  (int)x.size()
  15. #define pi pair<ll,ll>
  16. using ull = unsigned long long;
  17.  
  18. void solve(){
  19. ll n;
  20.     cin>>n;
  21.  
  22.     vector<ll>dis(n+3,-1);
  23.     vector<pi>v(n+3);
  24.     unordered_map<ll,vector<ll>>mp[2];
  25.     for (int i=0;i<n+2;i++)
  26.     {
  27.         cin>>v[i].first>>v[i].second;
  28.         mp[0][v[i].first].push_back(i);
  29.         mp[1][v[i].second].push_back(i);
  30.     }
  31.     queue<pair<ll,bool>>q;
  32.     dis[0]=0;
  33.     q.push({0,0});
  34.     q.push({0,1});
  35.     while (!q.empty())
  36.     {
  37.  
  38.        ll  u=q.front().first;
  39.         ll dir=q.front().second;
  40.         ll val=dir ? v[u].second : v[u].first;
  41.          q.pop();
  42.         for (auto it:mp[dir][val])
  43.         {
  44.             if (dis[it]==-1)q.push({it,!dir}),dis[it]=dis[u]+1;
  45.         }
  46.     }
  47.  
  48.     if (dis[1]==-1)cout<<-1;
  49.     else cout<<dis[1]-1;
  50.  
  51. }
  52.  
  53. int32_t main()
  54. {
  55.  
  56. // #ifndef ONLINE_JUDGE
  57. //      freopen("in.txt", "r", stdin);
  58. //    // freopen("output.txt", "w", stdout);
  59. // #endif
  60.  
  61.     freopen("lasers.in","r",stdin);
  62.     freopen("lasers.out","w",stdout);
  63.  
  64.     ios_base::sync_with_stdio(false);
  65.     cin.tie(NULL);
  66.     int tc = 1;
  67.     //cin >> tc;
  68.     for (int i = 1; i <= tc; i++)solve();
  69.     return 0;
  70. }
  71. /*
  72.  
  73.  */
  74.  
Advertisement
Add Comment
Please, Sign In to add comment