HTML & JS
#include<bits/stdc++.h>

using namespace std;

int solve(int A, int B, int C, int D, vector<string> &E) 
{
    queue<pair<pair<int,int>,int>> q;
    A--;
    B--;
    q.push({{A,B},0});
    C--;
    D--;
    if(A==C and B==D)
    return 0;
    while(!q.empty())
    {
        pair<pair<int,int>,int> temp=q.front();
        q.pop();
        int x=temp.first.first;
        int y=temp.first.second;
        int dist=temp.second;
        
        //cout<<"x "<<x<<" y "<<y<<" dist "<<dist<<endl;
        if(x==C and y==D)
        return dist;
        
        int i=x;
        int j=y;
        
        while(i<E.size() and E[i][j]!='1')
        {
            i++;
            q.push({{i,j},dist+1});
        }
        //cout<<"i "<<i<<" j "<<j<<endl;
        
        i=x;
        j=y;
        
        while(j<E[0].size() and E[i][j]!='1')
        {
            j++;
            q.push({{i,j},dist+1});
        }
        //cout<<"i "<<i<<" j "<<j<<endl;
        i=x;
        j=y;
        
        while(i>=0 and E[i][j]!='1')
        {
            i--;
            q.push({{i,j},dist+1});
        }
        //cout<<"i "<<i<<" j "<<j<<endl;
        i=x;
        j=y;
        
        while(j>=0 and E[i][j]!='1')
        {
            j--;
            q.push({{i,j},dist+1});
        }
        //cout<<"i "<<i<<" j "<<j<<endl;
    }
    return -1;
} 

int main()
{
    int A=1;
    int B=1;
    int C=4;
    int D=5;
    vector<string> E = { "00010", "00011", "11000", "01000" };
    cout<<solve(A,B,C,D,E)<<endl;
    
    return 0;
}

Generated URL:

Runtime Errors:

Segmentation Fault (SIGSEGV)

Output:

No Output
Report Bug