Advertisement
Zeinab_Hamdy

edited

Aug 31st, 2024 (edited)
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. //  #### Zeinab
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. #define nl "\n"
  5. #define fi first
  6. #define se second
  7. #define pb push_back
  8. #define ll long long
  9. #define RV return void
  10. #define sz(x) int(x.size())
  11. #define all(v) v.begin(), v.end()
  12. #define rall(v) v.rbegin(), v.rend()
  13. #define cin(v) for(auto&x:v) cin >> x;
  14. #define cout(v) for(auto&x:v) cout << x << " ";
  15.  
  16. int n , m , X0 , Y0 , X1 , Y1;
  17. string str;
  18.         // w , e , n , s
  19. int dx[]= {0 , 0 ,-1 , 1};
  20. int dy[]= {-1 , 1 , 0 , 0};
  21. char turn[]={'w' , 'e' , 'n' , 's'};
  22.  
  23. vector < vector < int > > grid;
  24.  
  25. bool valid(int i , int j){
  26.     return i >=0 and j >=0 and i < n and j < m and !grid[i][j]  ;
  27. }
  28.  
  29.  
  30. int ret ;
  31.  
  32.  
  33.  
  34. void bfs(){
  35.     //  i , j , char , cost , (con)
  36.     queue < pair < pair < int , int > , pair < char , int > > > q;
  37.     map < pair < pair < int , int > , int > , int  > dis;
  38.     q.push({{X0,Y0} , {str[0], 0}});
  39.     dis[{{X0,Y0} , str[0]}]=0;
  40.  
  41.     while(!q.empty()){
  42.         int i = q.front().fi.fi , j = q.front().fi.se ,
  43.         cs = q.front().se.se;
  44.         char c = q.front().se.fi; q.pop();
  45.  
  46.        
  47.  
  48.         for(int op = 0 ; op < 4 ; op++){
  49.             if(turn[op] != c and dis.find({{i,j} , turn[op]}) == dis.end()) {
  50.                 q.push({{i,j} , { turn[op]  , 1 + cs }});
  51.                  dis[{{i,j} , turn[op]}]=cs+1;
  52.             }
  53.         }
  54.  
  55.         for(int  moves = 1 ;  moves <= 3 ;  moves++){
  56.             for(int qw=0; qw < 4 ; qw++){
  57.                 int x = i + dx[qw]* moves , y =j + dy[qw]* moves;
  58.  
  59.                 if(valid(x,y) and dis.find({{x,y} , turn[qw]}) == dis.end()){
  60.                     if(x == X1 and y == Y1){
  61.                         ret = cs + 1 ;
  62.                         return ;
  63.                     }
  64.                     dis[{{x,y} , turn[qw]}]=cs+1;
  65.                     q.push({{x,y} , { turn[qw]  , 1 + cs }});
  66.                 }
  67.             }
  68.         }
  69.  
  70.  
  71.     }
  72.  
  73. }
  74.  
  75.  
  76.  
  77. void solve(){
  78.  
  79.     bool flag=0;
  80.  
  81. while(cin >> n >> m and n and m){
  82.     if(flag) cout << nl;
  83.     flag=1;
  84.     grid.assign(n,vector<int>(m,0));
  85.     for(auto& x : grid) cin(x);
  86.     cin >> X0 >> Y0 >> X1 >>Y1 >> str;
  87.     // X0-- , Y0-- , X1-- , Y1 --;
  88.     ret =-1;
  89.     bfs();
  90.  
  91.     cout << ret ;
  92.    
  93. }
  94.  
  95.  
  96.  
  97. }
  98.  
  99. int main(){
  100.     ios_base::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);  
  101.  
  102.  
  103.     int t=1;
  104.         // cin >> t ;
  105.     for(int i=1 ; i <= t ; i++){
  106.         // cout << "Case "<< i <<": " ;
  107.         solve();
  108.     }
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement