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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxN = 1e3 + 5;
int dp[maxN][maxN];
int s1, s2, t1, t2;
int w, h;
vector<string> maze;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
int dis[maxN][maxN];
void bfs(int s1, int s2) {
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      dis[i][j] = INF;
  deque<pair<int, int>> qq;
  qq.push_front({s1, s2});
  dis[s1][s2] = 0;

  while (!qq.empty()) {
    int xx = qq.front().first;
    int yy = qq.front().second;
    qq.pop_front();
    int d = dis[xx][yy];
    for (int j = 0; j < 4; ++j) {
      int tempX = xx + dx[j];
      int tempY = yy + dy[j];

      if (tempX >= h || tempX < 0)
        continue;
      if (tempY >= w || tempY < 0)
        continue;
      if (maze[tempX][tempY] == '#')
        continue;
      if (dis[tempX][tempY] <= d)
        continue;
      dis[tempX][tempY] = d;
      qq.push_front({tempX, tempY});
    }

    // 5x5 ways

    for (int di = -2; di <= 2; ++di) {
      for (int dj = -2; dj <= 2; ++dj) {
        int tempX = xx + di;
        int tempY = yy + dj;

        if (tempX >= h || tempX < 0)
          continue;
        if (tempY >= w || tempY < 0)
          continue;
        if (maze[tempX][tempY] == '#')
          continue;
        if (dis[tempX][tempY] <= d + 1)
          continue;
        dis[tempX][tempY] = d + 1;
        qq.push_back({tempX, tempY});
      }
    }
  }
}
int main() {
  cin >> h >> w;
  cin >> s1 >> s2;
  cin >> t1 >> t2;
  int ans;
  for (int i = 0; i < h; ++i) {
    string s;
    cin >> s;
    maze.push_back(s);
  }
  --s1, --s2, --t1, --t2;
  bfs(s1, s2);
  ans = dis[t1][t2];
  if (ans == INF)
    ans = -1;
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxN = 1e3 + 5;
int dp[maxN][maxN];
int s1, s2, t1, t2;
int w, h;
vector<string> maze;
int dx[] = {0, 1, -1, 0};
int dy[] = {1, 0, 0, -1};
const int INF = 1e9;
int dis[maxN][maxN];
void bfs(int s1, int s2) {
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      dis[i][j] = INF;
  queue<int> qq;
  qq.push(s1);
  qq.push(s2);
  dis[s1][s2] = 0;

  while (!qq.empty()) {
    int xx = qq.front();
    qq.pop();
    int yy = qq.front();
    qq.pop();
    int d = dis[xx][yy];
    for (int j = 0; j < 4; ++j) {
      int tempX = xx + dx[j];
      int tempY = yy + dy[j];

      if (tempX >= h || tempX < 0)
        continue;
      if (tempY >= w || tempY < 0)
        continue;
      if (maze[tempX][tempY] == '#')
        continue;
      if (dis[tempX][tempY] <= d)
        continue;
      dis[tempX][tempY] = d;
      qq.push(tempX);
      qq.push(tempY);
    }

    // 5x5 ways

    for (int di = -2; di <= 2; ++di) {
      for (int dj = -2; dj <= 2; ++dj) {
        int tempX = xx + di;
        int tempY = yy + dj;

        if (tempX >= h || tempX < 0)
          continue;
        if (tempY >= w || tempY < 0)
          continue;
        if (maze[tempX][tempY] == '#')
          continue;
        if (dis[tempX][tempY] <= d + 1)
          continue;
        dis[tempX][tempY] = d + 1;
        qq.push(tempX);
        qq.push(tempY);
      }
    }
  }
}
int main() {
  cin >> h >> w;
  cin >> s1 >> s2;
  cin >> t1 >> t2;
  int ans;
  for (int i = 0; i < h; ++i) {
    string s;
    cin >> s;
    maze.push_back(s);
  }
  --s1, --s2, --t1, --t2;
  bfs(s1, s2);
  ans = dis[t1][t2];
  if (ans == INF)
    ans = -1;
  cout << ans;
  return 0;
}