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

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn = 1e3 + 10;
const ll mod = 20180623;
char s[maxn][maxn];
int vis[maxn][maxn];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int res = -1;
int ex, ey;
int n, m;
struct ppp {
  int x, y, cnt;
};
ppp pp;
bool check(ppp c) {
  if (c.x < 1 || c.x > n || c.y < 1 || c.y > m ||
      (vis[c.x][c.y] != 0 && c.cnt >= vis[c.x][c.y]) || s[c.x][c.y] == '#')
    return false;
  return true;
}
queue<ppp> tk;
void bfs() {
  queue<ppp> que;
  pp.cnt = 1;
  que.push(pp);
  tk.push(pp);
  while (!que.empty()) {
    ppp now = que.front();
    que.pop();
    if (now.x == ex && now.y == ey) {
      res = now.cnt - 1;
    }
    for (int i = 0; i < 4; i++) {
      ppp z = now;
      z.x += dx[i];
      z.y += dy[i];
      if (check(z)) {
        vis[z.x][z.y] = z.cnt;
        que.push(z);
        tk.push(z);
      }
    }
  }
}

void bfs1(queue<ppp> t) {
  queue<ppp> que = t;
  while (!que.empty()) {
    ppp now = que.front();
    tk.push(now);
    que.pop();
    if (now.x == ex && now.y == ey) {
      res = now.cnt - 1;
    }
    for (int i = 0; i < 4; i++) {
      ppp z = now;
      z.x += dx[i];
      z.y += dy[i];
      if (check(z)) {
        vis[z.x][z.y] = z.cnt;
        que.push(z);
        tk.push(z);
      }
    }
  }
}

void bfs2() {
  while (1) {
    queue<ppp> temp;
    while (!tk.empty()) {
      ppp now = tk.front();
      tk.pop();
      for (int i = max(1, now.x - 2); i <= min(n, now.x + 2); i++) {
        for (int j = max(1, now.y - 2); j <= min(m, now.y + 2); j++) {
          if (s[i][j] == '.' && vis[i][j] == 0) {
            vis[i][j] = now.cnt + 1;
            ppp c = now;
            c.x = i;
            c.y = j;
            c.cnt = now.cnt + 1;
            temp.push(c);
          }
        }
      }
    }
    bfs1(temp);
    if (vis[ex][ey] != 0 || tk.empty()) {
      return;
    }
  }
}

int main() {
  scanf("%d%d", &n, &m);
  scanf("%d%d%d%d", &pp.x, &pp.y, &ex, &ey);
  for (int i = 1; i <= n; i++) {
    getchar();
    for (int j = 1; j <= m; j++) {
      scanf("%c", &s[i][j]);
    }
  }
  bfs();
  if (vis[ex][ey] == 0)
    bfs2();
  printf("%d", vis[ex][ey] - 1);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn = 1e3 + 10;
const ll mod = 20180623;
char s[maxn][maxn];
int vis[maxn][maxn];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int res = -1;
int ex, ey;
int n, m;
struct ppp {
  int x, y, cnt;
};
ppp p;
bool check(ppp c) {
  if (c.x < 1 || c.x > n || c.y < 1 || c.y > m ||
      (vis[c.x][c.y] != 0 && c.cnt >= vis[c.x][c.y]) || s[c.x][c.y] == '#')
    return false;
  return true;
}
void bfs() {
  queue<ppp> que;
  p.cnt = 1;
  que.push(p);
  while (!que.empty()) {
    ppp now = que.front();
    que.pop();
    for (int i = 0; i < 4; i++) {
      ppp z = now;
      z.x += dx[i];
      z.y += dy[i];
      if (check(z)) {
        vis[z.x][z.y] = z.cnt;
        que.push(z);
      }
    }
    for (int i = max(1, now.x - 2); i <= min(n, now.x + 2); i++) {
      for (int j = max(1, now.y - 2); j <= min(m, now.y + 2); j++) {
        if (s[i][j] == '.' && (vis[i][j] == 0 || vis[i][j] > now.cnt + 1)) {
          vis[i][j] = now.cnt + 1;
          ppp c = now;
          c.x = i;
          c.y = j;
          c.cnt = now.cnt + 1;
          que.push(c);
        }
      }
    }
  }
  if (vis[ex][ey] == 0)
    return;
  res = vis[ex][ey] - 1;
}

int main() {
  scanf("%d%d", &n, &m);
  scanf("%d%d%d%d", &p.x, &p.y, &ex, &ey);
  for (int i = 1; i <= n; i++) {
    getchar();
    for (int j = 1; j <= m; j++) {
      scanf("%c", &s[i][j]);
    }
  }
  bfs();
  printf("%d", res);
  return 0;
}