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

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

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int h, w, sx, sy, ex, ey;
  cin >> h >> w >> sx >> sy >> ex >> ey;
  sx--, sy--, ex--, ey--;
  vector<string> g(h);
  for (int i = 0; i < h; i++)
    cin >> g[i];

  vector<vector<int>> ans(h, vector<int>(w, 1e9));
  ans[sx][sy] = 0;
  queue<vector<int>> q;
  q.push({sx, sy});
  //	int a{1};
  while (!q.empty()) {
    vector<int> cur = q.front();
    //		cout << a++ << endl;
    //		cout << cur[0] << " " << cur[1] << endl;
    q.pop();
    //		for (int i = cur[0] - 1; i <= cur[0] + 1; i++) {
    //			for (int j = cur[1] - 1; j <= cur[1] + 1; j++) {
    //				if (i < 0 || j < 0 || i >= h || j >= w || (abs(cur[0] - i) +
    //abs(cur[1] - j) != 1)) continue; 				if (g[i][j] != '#' && ans[i][j] >
    //ans[cur[0]][cur[1]]) { 					ans[i][j] = ans[cur[0]][cur[1]]; 					q.push({i, j});
    //				}
    //			}
    //		}
    int x{cur[0] - 1}, y{cur[1]};
    while (x >= 0 && g[x][y] != '#' && ans[x][y] > ans[cur[0]][cur[1]]) {
      ans[x][y] = ans[cur[0]][cur[1]];
      q.push({x--, y});
    }
    x = cur[0] + 1;
    while (x < h && g[x][y] != '#' && ans[x][y] > ans[cur[0]][cur[1]]) {
      ans[x][y] = ans[cur[0]][cur[1]];
      q.push({x++, y});
    }
    x = cur[0], y = cur[1] - 1;
    while (y >= 0 && g[x][y] != '#' && ans[x][y] > ans[cur[0]][cur[1]]) {
      ans[x][y] = ans[cur[0]][cur[1]];
      q.push({x, y--});
    }
    y = cur[1] + 1;
    while (y < w && g[x][y] != '#' && ans[x][y] > ans[cur[0]][cur[1]]) {
      ans[x][y] = ans[cur[0]][cur[1]];
      q.push({x, y++});
    }
    for (int i = cur[0] - 2; i <= cur[0] + 2; i++) {
      for (int j = cur[1] - 2; j <= cur[1] + 2; j++) {
        if (i < 0 || j < 0 || i >= h || j >= w)
          continue;
        if (g[i][j] != '#' && ans[i][j] > ans[cur[0]][cur[1]] + 1) {
          q.push({i, j});
          ans[i][j] = ans[cur[0]][cur[1]] + 1;
        }
      }
    }
  }
  cout << (ans[ex][ey] == 1e9 ? -1 : ans[ex][ey]);
}

Error: TLE

Fixed submission:

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

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int h, w, sx, sy, ex, ey;
  cin >> h >> w >> sx >> sy >> ex >> ey;
  sx--, sy--, ex--, ey--;
  vector<string> g(h);
  for (int i = 0; i < h; i++)
    cin >> g[i];

  vector<vector<int>> ans(h, vector<int>(w, 1e9));
  ans[sx][sy] = 0;
  queue<vector<int>> q;
  q.push({sx, sy});
  while (!q.empty()) {
    vector<int> cur = q.front();
    q.pop();
    for (int i = cur[0] - 1; i <= cur[0] + 1; i++) {
      for (int j = cur[1] - 1; j <= cur[1] + 1; j++) {
        if (i < 0 || j < 0 || i >= h || j >= w ||
            (abs(cur[0] - i) + abs(cur[1] - j) != 1))
          continue;
        if (g[i][j] != '#' && ans[i][j] > ans[cur[0]][cur[1]]) {
          ans[i][j] = ans[cur[0]][cur[1]];
          q.push({i, j});
        }
      }
    }
    for (int i = cur[0] - 2; i <= cur[0] + 2; i++) {
      for (int j = cur[1] - 2; j <= cur[1] + 2; j++) {
        if (i < 0 || j < 0 || i >= h || j >= w)
          continue;
        if (g[i][j] != '#' && ans[i][j] > ans[cur[0]][cur[1]] + 1) {
          q.push({i, j});
          ans[i][j] = ans[cur[0]][cur[1]] + 1;
        }
      }
    }
  }
  cout << (ans[ex][ey] == 1e9 ? -1 : ans[ex][ey]);
}