Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

void dfs(int n, int d);

int main() {
  int h, w, sh, sw, gh, gw;
  cin >> h >> w >> sh >> sw >> gh >> gw;
  sh--;
  sw--;
  gh--;
  gw--;
  vector<string> s(h);
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }

  vector<vector<int>> map(h, vector<int>(w, -1));
  int id = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#')
        continue;
      if (map[i][j] != -1)
        continue;
      queue<pair<int, int>> q;
      q.push(make_pair(i, j));
      while (!q.empty()) {
        int x = q.front().first;
        int y = q.front().second;
        q.pop();

        if (map[x][y] != -1)
          continue;
        map[x][y] = id;

        int dx[] = {-1, 1, 0, 0};
        int dy[] = {0, 0, -1, 1};
        for (int i = 0; i < 4; i++) {
          int nx = x + dx[i];
          int ny = y + dy[i];
          if (nx < 0)
            continue;
          if (ny < 0)
            continue;
          if (nx >= h)
            continue;
          if (ny >= w)
            continue;
          if (s[nx][ny] == '#')
            continue;
          q.push(make_pair(nx, ny));
        }
      }
      id++;
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
    }
  }

  vector<set<int>> st(id);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#')
        continue;
      for (int k = -2; k <= 2; k++) {
        for (int l = -2; l <= 2; l++) {
          int nx = i + k;
          int ny = l + j;
          if (nx < 0)
            continue;
          if (ny < 0)
            continue;
          if (nx >= h)
            continue;
          if (ny >= w)
            continue;
          if (s[nx][ny] == '#')
            continue;
          if (map[i][j] == map[nx][ny])
            continue;
          st[map[i][j]].insert(map[nx][ny]);
          st[map[nx][ny]].insert(map[i][j]);
        }
      }
    }
  }

  vector<int> ans(id, -1);
  queue<pair<int, int>> r;
  r.push(make_pair(map[sh][sw], 0));
  while (!r.empty()) {
    int c = r.front().first;
    int d = r.front().second;
    r.pop();

    if (ans[c] != -1 && ans[c] <= d)
      continue;
    ans[c] = d;
    for (auto &i : st[c]) {
      if (ans[i] != -1 && ans[i] <= d + 1)
        continue;
      r.push(make_pair(i, d + 1));
    }
  }
  /*for (int i = 0; i < h; i++) {
          for (int j = 0; j < w; j++) {
                  if (s[i][j] == '#')continue;
                  cout << ans[map[i][j]] << (j == w - 1 ? "\\n" : " ");
          }
  }*/
  cout << ans[map[gh][gw]] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

int main() {
  int h, w, sh, sw, gh, gw;
  cin >> h >> w >> sh >> sw >> gh >> gw;
  sh--;
  sw--;
  gh--;
  gw--;
  vector<string> s(h);
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }

  vector<vector<int>> ans(h, vector<int>(w, -1));
  queue<tuple<int, int, int>> q;
  q.push(make_tuple(sh, sw, 0));
  while (!q.empty()) {
    int x = get<0>(q.front());
    int y = get<1>(q.front());
    int warp = get<2>(q.front());
    q.pop();

    if (ans[x][y] != -1 && ans[x][y] <= warp)
      continue;
    ans[x][y] = warp;

    int dx[] = {-1, 1, 0, 0};
    int dy[] = {0, 0, -1, 1};
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0)
        continue;
      if (ny < 0)
        continue;
      if (nx >= h)
        continue;
      if (ny >= w)
        continue;
      if (s[nx][ny] == '#')
        continue;
      q.push(make_tuple(nx, ny, warp));
    }

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (i == 0 && j == 0)
          continue;

        int nx = x + i;
        int ny = y + j;
        if (nx < 0)
          continue;
        if (ny < 0)
          continue;
        if (nx >= h)
          continue;
        if (ny >= w)
          continue;
        if (s[nx][ny] == '#')
          continue;
        q.push(make_tuple(nx, ny, warp + 1));
      }
    }
  }
  /*for (int i = 0; i < h; i++) {
          for (int j = 0; j < w; j++) {
                  cout << ans[i][j] << (j == w - 1 ? "\\n" : " ");
          }
  }*/
  cout << ans[gh][gw] << endl;

  return 0;
}