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

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

int _main() {

  int h, w;
  cin >> h >> w;
  int _sx, _sy, sx, sy;
  cin >> _sy >> _sx;
  int _gx, _gy, gx, gy;
  cin >> _gy >> _gx;
  sx = _sx - 1;
  sy = _sy - 1;
  gx = _gx - 1;
  gy = _gy - 1;

  vector<vector<bool>> road(w, vector<bool>(h));
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      char c;
      cin >> c;
      if (c == '.') {
        road[x][y] = true;
      } else {
        road[x][y] = false;
      }
    }
  }

  vector<vector<int>> cost(w, vector<int>(h, INFINITY));
  queue<tuple<int, int>> searchfrom;
  vector<vector<int>> searching_b(w, vector<int>(h, false));
  searchfrom.push(tuple<int, int>(sx, sy));
  int costnow = 0;
  while (true) {
    queue<tuple<int, int>> searching = queue<tuple<int, int>>(searchfrom);
    queue<tuple<int, int>> searched = queue<tuple<int, int>>();
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        // searching_b[x][y]=false;
      }
    }
    searchfrom = queue<tuple<int, int>>();
    while (searching.size() > 0) {
      tuple<int, int> coord = searching.front();
      int cx = get<0>(coord);
      int cy = get<1>(coord);
      searching.pop();

      if (cx == gx && cy == gy) {
        return costnow;
      }

      for (int x = max(0, cx - 1); x <= min(w - 1, cx + 1); x++) {
        for (int y = max(0, cy - 1); y <= min(h - 1, cy + 1); y++) {
          int lenSu = abs(x - cx) + abs(y - cy);
          if (road[x][y] && costnow < cost[x][y]) {
            if (lenSu == 0) {
              cost[x][y] = costnow;
              searched.push(tuple<int, int>(x, y));
            } else if (lenSu == 1 && !searching_b[x][y]) {
              searching_b[x][y] = true;
              searching.push(tuple<int, int>(x, y));
            }
          }
        }
      }
    }
    while (searched.size() > 0) {
      tuple<int, int> coord = searched.front();
      int cx = get<0>(coord);
      int cy = get<1>(coord);
      searched.pop();

      for (int x = max(0, get<0>(coord) - 2);
           x <= min(w - 1, get<0>(coord) + 2); x++) {
        for (int y = max(0, get<1>(coord) - 2);
             y <= min(h - 1, get<1>(coord) + 2); y++) {
          if (road[x][y] && costnow < cost[x][y]) {
            searchfrom.push(tuple<int, int>(x, y));
          }
        }
      }
    }
    if (searchfrom.size() == 0)
      return -1;
    costnow++;
  }
}

int main() {
  int result = _main();
  cout << result;
  return 0;
}

Error: TLE

Fixed submission:

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

int _main() {

  int h, w;
  cin >> h >> w;
  int _sx, _sy, sx, sy;
  cin >> _sy >> _sx;
  int _gx, _gy, gx, gy;
  cin >> _gy >> _gx;
  sx = _sx - 1;
  sy = _sy - 1;
  gx = _gx - 1;
  gy = _gy - 1;

  vector<vector<bool>> road(w, vector<bool>(h));
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      char c;
      cin >> c;
      if (c == '.') {
        road[x][y] = true;
      } else {
        road[x][y] = false;
      }
    }
  }

  vector<vector<int>> cost(w, vector<int>(h, INFINITY));
  queue<tuple<int, int>> searchfrom;
  vector<vector<bool>> searching_b(w, vector<bool>(h, false));
  searchfrom.push(tuple<int, int>(sx, sy));
  int costnow = 0;
  while (true) {
    queue<tuple<int, int>> searching = queue<tuple<int, int>>(searchfrom);
    queue<tuple<int, int>> searched = queue<tuple<int, int>>();
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        // searching_b[x][y]=false;
      }
    }
    searchfrom = queue<tuple<int, int>>();
    while (searching.size() > 0) {
      tuple<int, int> coord = searching.front();
      int cx = get<0>(coord);
      int cy = get<1>(coord);
      searching.pop();

      if (cx == gx && cy == gy) {
        return costnow;
      }

      for (int x = max(0, cx - 1); x <= min(w - 1, cx + 1); x++) {
        for (int y = max(0, cy - 1); y <= min(h - 1, cy + 1); y++) {
          int lenSu = abs(x - cx) + abs(y - cy);
          if (road[x][y] && costnow < cost[x][y]) {
            if (lenSu == 0) {
              cost[x][y] = costnow;
              searched.push(tuple<int, int>(x, y));
            } else if (lenSu == 1 /*&& !searching_b[x][y]*/) {
              // searching_b[x][y]=true;
              searching.push(tuple<int, int>(x, y));
            }
          }
        }
      }
    }
    while (searched.size() > 0) {
      tuple<int, int> coord = searched.front();
      int cx = get<0>(coord);
      int cy = get<1>(coord);
      searched.pop();

      for (int x = max(0, get<0>(coord) - 2);
           x <= min(w - 1, get<0>(coord) + 2); x++) {
        for (int y = max(0, get<1>(coord) - 2);
             y <= min(h - 1, get<1>(coord) + 2); y++) {
          if (road[x][y] && costnow < cost[x][y]) {
            searchfrom.push(tuple<int, int>(x, y));
          }
        }
      }
    }
    if (searchfrom.size() == 0)
      return -1;
    costnow++;
  }
}

int main() {
  int result = _main();
  cout << result;
  return 0;
}