グリッド上の最短経路を求める

= 壁

. = 道

のグリッドでi,jからx,yまでの最短距離を計算する

int way(vector<vector<char>>& v ,int I,int J,int x,int y){
    queue<pair<int,int>> q;
    q.push({I,J});
    vector<vector<int>> ans(v.size(),vector<int>(v[0].size(),-1));
    while(q.size()!=0){
        int i = q.front().first;
        int j = q.front().second;
        q.pop();
        if(v[i-1][j]=='.' && ans[i-1][j]==-1){
            ans[i-1][j] = ans[i][j]+1;
            q.push({i-1,j});
        }
        if(v[i+1][j]=='.' && ans[i+1][j]==-1){
            ans[i+1][j] = ans[i][j]+1;
            q.push({i+1,j});
        }
        if(v[i][j-1]=='.' && ans[i][j-1]==-1){
            ans[i][j-1] = ans[i][j]+1;
            q.push({i,j-1});
        }
        if(v[i][j+1]=='.' && ans[i][j+1]==-1){
            ans[i][j+1] = ans[i][j]+1;
            q.push({i,j+1});
        }        
    }
    return {ans[x][y]};
}