Language: C++

#include <algorithm>
#include<iostream>
#include<vector>
#include<deque>
#include<queue>
#include<stack>
#include<list>
#include<map>
#include<set>
#include<string>
#include <sstream>
#include<bitset>

#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<limits.h>
#define MAX 1000+2
const int INF = 0x7fffffff;
using lll = long long;
using ull = unsigned long long;
using namespace std;
using Graph = vector<vector<int>>;

int mmap[MAX][MAX];
int iland[MAX][MAX];
bool seen[MAX][MAX];
map<int,int> land;
//1.島を探す
void search(int x,int y,int n){
  //cout << "x:" << x << "y:" << y << endl;
  seen[y][x] = true;
  iland[y][x] = n;
  //上
  if(mmap[y-1][x] == 0 && seen[y-1][x] == false){
    search(x,y-1,n);
  }
  
  //左
  if(mmap[y][x-1] == 0 && seen[y][x-1] == false){
    search(x-1,y,n);
  }
  
  //右
  if(mmap[y][x+1] == 0 && seen[y][x+1] == false){
    search(x+1,y,n);
  }
  
  //下
  if(mmap[y+1][x] == 0 && seen[y+1][x] == false){
    search(x,y+1,n);
  }
}

//2.繋っているグラフを作成
//3.繋っていない場合 -1
//4.繋っている場合最小値を探す


int main(){
  lll ii,jj,kk;
  vector<int> ret;
  int h,w,ch,cw,dh,dw;

  cin >>  h >>  w;
  
  cin >> ch >> cw;
  cin >> dh >> dw;


  string temp;
  memset(mmap,0,sizeof(mmap));
  memset(iland,0,sizeof(iland));
  memset(seen,false,sizeof(seen));
  
  for(ii=0;ii<=h+1;ii++){
    for(jj=0;jj<=w+1;jj++){
      mmap[ii][jj] = 1;
    }
  }
  
  for(ii=1;ii<=h;ii++){
    cin >> temp;
    for(jj=1;jj<=w;jj++){
      if(temp[jj-1] == '.'){
        mmap[ii][jj] = 0;
      }
      else{
        mmap[ii][jj] = 1;
      }
    }
  }

  //島の数を探す
  int n = 0;

  for(ii=1;ii<=h;ii++){
    for(jj=1;jj<=w;jj++){
      if(mmap[ii][jj] == 0 && seen[ii][jj] == false){
        //cout << "search" << endl;
        n++;
        search(jj,ii,n);

      }
    }
  }

  //cout << "n:" << n << endl;
  Graph g(n+1);
  set<int> s[n+1];

  //for(ii=0;ii<=h+1;ii++){
  //  for(jj=0;jj<=w+1;jj++){
  //    cout << iland[ii][jj];
  //  }
  //  cout << endl;
  //}
  
  int ll,mm;
  int pos,posi;

  //繋っている島を判定
  for(ii=1;ii<=h;ii++){
    for(jj=1;jj<=w;jj++){
      pos = iland[ii][jj];

      for(ll=ii-2;ll<=ii+2;ll++){
        for(mm=jj-2;mm<=jj+2;mm++){

          if(ll>=1 && ll<=h &&
             mm>= 1 && mm<=w){
            posi = iland[ll][mm];
            //cout << "ll:" << ll << "mm:" << mm << "posi:" << posi << endl;
            if(posi != pos){
              s[pos].insert(posi);
            }
          }
          
        }
      }
    }
  }

  //for(ii=1;ii<=n;ii++){
  //  auto it = s[ii].begin();
  //  cout << "ii:" << ii << ":";
  //  while(it != s[ii].end()){
  //    cout << *it << ",";
  //    it++;
  //  }
  //  cout << endl;
  //}
  
  for(ii=1;ii<=n;ii++){
    auto it = s[ii].begin();
    while(it != s[ii].end()){
      g[ii].push_back(*it);
      it++;
    }
  }
  int from,to;
  from = iland[ch][cw];
  to   = iland[dh][dw];

  bool seen2[n+1];
  memset(seen2,false,sizeof(seen2));
  vector<int> todo;
  todo.push_back(from);
  int dist[n+1] ;
  int tempDist;
  dist[from] = 0;
  for(ii=0;ii<=n;ii++){
    dist[ii] = -1;
  }
  dist[from] = 0;
  int v;
  
  
  while(todo.empty() != true){
    v = todo[0];
    todo.erase(todo.begin());
    tempDist = dist[v]+1;
    for(auto w : g[v]){
      if(seen2[w] == true){continue;}
      dist[w] = tempDist;
      seen2[w] = true;
      todo.push_back(w);
    }
  }

  //for(ii=1;ii<=n;ii++){
  //  cout << "dist:" << dist[ii] << endl;;
  //}
  
  cout << dist[to] << endl;
  
  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:102:15: warning: ignoring return value of '_ForwardIterator std::search(_ForwardIterator, _ForwardIterator, const _Searcher&) [with _ForwardIterator = long long int; _Searcher = int]', declared with attribute 'nodiscard' [-Wunused-result]
  102 |         search(jj,ii,n);
      |         ~~~~~~^~~~~~~~~
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algo.h:4184:5: note: declared here
 4184 |     search(_ForwardIterator __first, _ForwardIterator __last,
      |     ^~~~~~
/usr/include/c++/14/bits/stl_algo.h: In instantiation of '_ForwardIterator std::search(_ForwardIterator, _ForwardIterator, const _Searcher&) [with _ForwardIterator = long long int; _Searcher = int]':
a.cc:102:15:   required from here
  102 |         search(jj,ii,n);
      |         ~~~~~~^~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4186:24: error: expression cannot be used as a function
 4186 |     { return __searcher(__first, __last).first; }
      |              ~~~~~~~~~~^~~~~~~~~~~~~~~~~