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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define MOD (ll)1000000007

char a[1050][1050];
int dp[1001][1001];
bool v[1001][1001];
pii d;

struct point {
  int x, y, time;
};

point go[4] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};

int main(void) {
  int h, w;
  int cx, cy;
  scanf("%d %d", &h, &w);
  scanf("%d %d %d %d", &cx, &cy, &d.first, &d.second);
  for (int i = 1; i <= h; i++)
    scanf("%s", a[i] + 1);

  for (int i = 0; i <= h; i++)
    for (int j = 0; j <= w; j++)
      dp[i][j] = 1e9;

  queue<point> q, q2;
  point t, temp;
  q.push({cx, cy, 0});
  q2.push({cx, cy, 0});
  dp[cx][cy] = 0;
  while (!q.empty() || !q2.empty()) {
    while (!q.empty()) {
      t = q.front();
      q.pop();
      q2.push(t);
      for (int i = 0; i < 4; i++) {
        temp = {t.x + go[i].x, t.y + go[i].y, t.time};
        if (temp.x >= 1 && temp.x <= h && temp.y >= 1 && temp.y <= w &&
            dp[temp.x][temp.y] == 1e9 && a[temp.x][temp.y] == '.') {
          dp[temp.x][temp.y] = min(dp[temp.x][temp.y], t.time);
          q.push({temp.x, temp.y, dp[t.x][t.y]});
          q2.push({temp.x, temp.y, dp[t.x][t.y]});
        }
      }
    }
    while (!q2.empty()) {
      t = q2.front();
      q2.pop();
      for (int x = t.x - 2; x <= t.x + 2; x++)
        for (int y = t.y - 2; y <= t.y + 2; y++) {
          if (x >= 1 && x <= h && y >= 1 && y <= w && dp[x][y] == 1e9 &&
              a[x][y] == '.') {
            dp[x][y] = t.time + 1;
            q.push({x, y, t.time + 1});
          }
        }
    }
  }

  /*for (int i = 1; i <= h; i++)
  {
      for (int j = 1; j <= w; j++)
          printf("%d ", dp[i][j]);
      printf("\\n");
  }*/
  printf("%d", dp[d.first][d.second] != 1e9 ? dp[d.first][d.second] : -1);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define MOD (ll)1000000007

char a[1050][1050];
int dp[1001][1001];
bool v[1001][1001];
pii d;
int h, w;

struct point {
  int x, y, time;
};

point go[4] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
queue<point> q;

void mv(point t) {
  // if (dp[t.x][t.y] != 1e9)
  // return;
  // printf("[%d %d %d]\\n", t.x, t.y, t.time);
  dp[t.x][t.y] = t.time;
  point temp;
  for (int i = 0; i < 4; i++) {
    temp = {t.x + go[i].x, t.y + go[i].y, t.time};
    if (temp.x >= 1 && temp.y >= 1 && dp[temp.x][temp.y] > t.time &&
        a[temp.x][temp.y] == '.') {
      mv({temp.x, temp.y, t.time});
    }
  }
  for (int x = t.x - 2; x <= t.x + 2; x++)
    for (int y = t.y - 2; y <= t.y + 2; y++) {
      if (x >= 1 && y >= 1 && dp[x][y] > t.time + 1 && a[x][y] == '.') {
        mv({x, y, t.time + 1});
      }
    }
}

int main(void) {
  int cx, cy;
  scanf("%d %d", &h, &w);
  scanf("%d %d %d %d", &cx, &cy, &d.first, &d.second);
  for (int i = 1; i <= h; i++)
    scanf("%s", a[i] + 1);

  for (int i = 0; i <= h; i++)
    for (int j = 0; j <= w; j++)
      dp[i][j] = 1e9;

  point t;
  q.push({cx, cy, 0});
  while (!q.empty()) {
    t = q.front();
    q.pop();

    mv(t);
  }

  /*for (int i = 1; i <= h; i++)
  {
          for (int j = 1; j <= w; j++)
                  printf("%d ", dp[i][j]);
          printf("\\n");
  }*/
  printf("%d", dp[d.first][d.second] != 1e9 ? dp[d.first][d.second] : -1);
  return 0;
}