Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // #### Zeinab
- #include<bits/stdc++.h>
- using namespace std;
- #define nl "\n"
- #define fi first
- #define se second
- #define pb push_back
- #define ll long long
- #define RV return void
- #define sz(x) int(x.size())
- #define all(v) v.begin(), v.end()
- #define rall(v) v.rbegin(), v.rend()
- #define cin(v) for(auto&x:v) cin >> x;
- #define cout(v) for(auto&x:v) cout << x << " ";
- int n , m , X0 , Y0 , X1 , Y1;
- string str;
- // w , e , n , s
- int dx[]= {0 , 0 ,-1 , 1};
- int dy[]= {-1 , 1 , 0 , 0};
- char turn[]={'w' , 'e' , 'n' , 's'};
- vector < vector < int > > grid;
- bool valid(int i , int j){
- return i >=0 and j >=0 and i < n and j < m and !grid[i][j] ;
- }
- int ret ;
- void bfs(){
- // i , j , char , cost , (con)
- queue < pair < pair < int , int > , pair < char , int > > > q;
- map < pair < pair < int , int > , int > , int > dis;
- q.push({{X0,Y0} , {str[0], 0}});
- dis[{{X0,Y0} , str[0]}]=0;
- while(!q.empty()){
- int i = q.front().fi.fi , j = q.front().fi.se ,
- cs = q.front().se.se;
- char c = q.front().se.fi; q.pop();
- for(int op = 0 ; op < 4 ; op++){
- if(turn[op] != c and dis.find({{i,j} , turn[op]}) == dis.end()) {
- q.push({{i,j} , { turn[op] , 1 + cs }});
- dis[{{i,j} , turn[op]}]=cs+1;
- }
- }
- for(int moves = 1 ; moves <= 3 ; moves++){
- for(int qw=0; qw < 4 ; qw++){
- int x = i + dx[qw]* moves , y =j + dy[qw]* moves;
- if(valid(x,y) and dis.find({{x,y} , turn[qw]}) == dis.end()){
- if(x == X1 and y == Y1){
- ret = cs + 1 ;
- return ;
- }
- dis[{{x,y} , turn[qw]}]=cs+1;
- q.push({{x,y} , { turn[qw] , 1 + cs }});
- }
- }
- }
- }
- }
- void solve(){
- bool flag=0;
- while(cin >> n >> m and n and m){
- if(flag) cout << nl;
- flag=1;
- grid.assign(n,vector<int>(m,0));
- for(auto& x : grid) cin(x);
- cin >> X0 >> Y0 >> X1 >>Y1 >> str;
- // X0-- , Y0-- , X1-- , Y1 --;
- ret =-1;
- bfs();
- cout << ret ;
- }
- }
- int main(){
- ios_base::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);
- int t=1;
- // cin >> t ;
- for(int i=1 ; i <= t ; i++){
- // cout << "Case "<< i <<": " ;
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement