Language: C++

#include<bits/stdc++.h>
using namespace std;

vector< vector< int > > grid_bfs(vector< string > &s, char start, const string &wall = "#") {
  const int vx[] = {0, 1, 0, -1}, vy[] = {1, 0, -1, 0};
  vector< vector< int > > min_cost(s.size(), vector< int >(s[0].size(), -1));
  queue< pair< int, int > > que;
  for(int i = 0; i < s.size(); i++) {
    for(int j = 0; j < s[i].size(); j++) {
      if(s[i][j] == start) {
        que.emplace(i, j);
        min_cost[i][j] = 0;
      }
    }
  }
  while(!que.empty()) {
    auto p = que.front();
    que.pop();
    for(int i = 0; i < 4; i++) {
      int ny = p.first + vy[i], nx = p.second + vx[i];
      if(nx < 0 || ny < 0 || nx >= s[0].size() || ny >= s.size()) continue;
      if(min_cost[ny][nx] != -1) continue;
      if(wall.find(s[ny][nx]) != string::npos) continue;
      min_cost[ny][nx] = min_cost[p.first][p.second] + 1;
      que.emplace(ny, nx);
    }
  }
  return min_cost;
}

int main() {
  int H, W;
  cin >> H >> W;
  vector <string> S(H);
  int ch,cw,dh,dw;
  cin >> ch >> cw >> dh >> dw;
  ch-=1;
  cw-=1;
  dh-=1;
  dw-=1;
  for (int i=0; i<H; i++) cin >> S.at(i);
  S.at(ch).at(cw) = 'c';
//  S.at(dh).at(dw) = 'd';

  int ans = 0;

  while (true) {
    // (1) 移動
    vector <vector<int>> grids = grid_bfs(S, 'c');
    S.at(ch).at(cw) = '#'; 
    long double distmin = pow(pow(H,2)+pow(W,2),0.5);
    for (int i=0; i<grids.size(); i++) {
      for (int j=0; j<grids.at(i).size(); j++) {
        if (grids.at(i).at(j)>0) {
          long double dist = pow(pow((i-dh),2)+pow((j-dw),2),0.5);
          if (dist<distmin) {
            distmin = dist;
            ch = i;
            cw = j;
          }
        }
      }
    }
    // 終了判定
    if (ch==dh && cw==dw) {
      cout << ans;
      return 0;
    }

    // (2) ワープ
    int cht = ch;
    int cwt = cw;
    for (int i=max(0,cht-2); i<=min(H-1,cht+2); i++) {
      for (int j=max(0,cwt-2); j<=min(W-1,cwt+2); j++) {
        if (S.at(i).at(j)=='.') {
          long double dist = pow(pow((i-dh),2)+pow(j-dw,2),0.5);
          if (dist<distmin) {
            distmin = dist;
            ch = i;
            cw = j;
          }
        }
      }
    }
    if (ch!=cht || cw!=cwt) ans++; 
    else {
      cout << -1;
      return 0;
    }
    // 終了判定
    if (ch==dh && cw==dw) {
      cout << ans;
      return 0;
    }
    S.at(ch).at(cw) = 'c';
  }
}#include<bits/stdc++.h>
using namespace std;

vector< vector< int > > grid_bfs(vector< string > &s, char start, const string &wall = "#") {
  const int vx[] = {0, 1, 0, -1}, vy[] = {1, 0, -1, 0};
  vector< vector< int > > min_cost(s.size(), vector< int >(s[0].size(), -1));
  queue< pair< int, int > > que;
  for(int i = 0; i < s.size(); i++) {
    for(int j = 0; j < s[i].size(); j++) {
      if(s[i][j] == start) {
        que.emplace(i, j);
        min_cost[i][j] = 0;
      }
    }
  }
  while(!que.empty()) {
    auto p = que.front();
    que.pop();
    for(int i = 0; i < 4; i++) {
      int ny = p.first + vy[i], nx = p.second + vx[i];
      if(nx < 0 || ny < 0 || nx >= s[0].size() || ny >= s.size()) continue;
      if(min_cost[ny][nx] != -1) continue;
      if(wall.find(s[ny][nx]) != string::npos) continue;
      min_cost[ny][nx] = min_cost[p.first][p.second] + 1;
      que.emplace(ny, nx);
    }
  }
  return min_cost;
}

int main() {
  int H, W;
  cin >> H >> W;
  vector <string> S(H);
  int ch,cw,dh,dw;
  cin >> ch >> cw >> dh >> dw;
  ch-=1;
  cw-=1;
  dh-=1;
  dw-=1;
  for (int i=0; i<H; i++) cin >> S.at(i);
  S.at(ch).at(cw) = 'c';
//  S.at(dh).at(dw) = 'd';

  int ans = 0;

  while (true) {
    // (1) 移動
    vector <vector<int>> grids = grid_bfs(S, 'c');
    S.at(ch).at(cw) = '#'; 
    long double distmin = pow(pow(H,2)+pow(W,2),0.5);
    for (int i=0; i<grids.size(); i++) {
      for (int j=0; j<grids.at(i).size(); j++) {
        if (grids.at(i).at(j)>0) {
          long double dist = pow(pow((i-dh),2)+pow((j-dw),2),0.5);
          if (dist<distmin) {
            distmin = dist;
            ch = i;
            cw = j;
          }
        }
      }
    }
    // 終了判定
    if (ch==dh && cw==dw) {
      cout << ans;
      return 0;
    }

    // (2) ワープ
    int cht = ch;
    int cwt = cw;
    for (int i=max(0,cht-2); i<=min(H-1,cht+2); i++) {
      for (int j=max(0,cwt-2); j<=min(W-1,cwt+2); j++) {
        if (S.at(i).at(j)=='.') {
          long double dist = pow(pow((i-dh),2)+pow(j-dw,2),0.5);
          if (dist<distmin) {
            distmin = dist;
            ch = i;
            cw = j;
          }
        }
      }
    }
    if (ch!=cht || cw!=cwt) ans++; 
    else {
      cout << -1;
      return 0;
    }
    // 終了判定
    if (ch==dh && cw==dw) {
      cout << ans;
      return 0;
    }
    S.at(ch).at(cw) = 'c';
  }
}

Compiler output:
a.cc:97:2: error: stray '#' in program
   97 | }#include<bits/stdc++.h>
      |  ^
a.cc:97:3: error: 'include' does not name a type
   97 | }#include<bits/stdc++.h>
      |   ^~~~~~~
a.cc:100:25: error: redefinition of 'std::vector<std::vector<int> > grid_bfs(std::vector<std::__cxx11::basic_string<char> >&, char, const std::string&)'
  100 | vector< vector< int > > grid_bfs(vector< string > &s, char start, const string &wall = "#") {
      |                         ^~~~~~~~
a.cc:4:25: note: 'std::vector<std::vector<int> > grid_bfs(std::vector<std::__cxx11::basic_string<char> >&, char, const std::string&)' previously defined here
    4 | vector< vector< int > > grid_bfs(vector< string > &s, char start, const string &wall = "#") {
      |                         ^~~~~~~~
a.cc:127:5: error: redefinition of 'int main()'
  127 | int main() {
      |     ^~~~
a.cc:31:5: note: 'int main()' previously defined here
   31 | int main() {
      |     ^~~~