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

#include <bits/stdc++.h>
using namespace std;
int a, b, k, x1, y_1, x2, y2, d[500005], v[1005][1005];
bool n[1005][1005], vi[500005];
char c;
struct st {
  int w, v;
  st(int V, int W) {
    w = W;
    v = V;
  }
};
struct st1 {
  int w, dd;
  st1() {}
  st1(int W, int DD) {
    w = W;
    dd = DD;
  }
  friend bool operator<(st1 x, st1 y) { return x.dd > y.dd; }
};
priority_queue<st1> q;
vector<st> ans[500005];
int Dijkstra(int s, int t) {
  memset(d, 0x3f, sizeof(d));
  memset(vi, 0, sizeof(vi));
  d[s] = 0;
  q.push(st1(s, 0));
  while (q.size()) {
    int x = q.top().w;
    q.pop();
    if (vi[x]) {
      continue;
    }
    vi[x] = 1;
    int siz = ans[x].size();
    for (int i = 0; i < siz; i++) {
      int vr = ans[x][i].v, w = ans[x][i].w;
      if (d[vr] > d[x] + w) {
        d[vr] = d[x] + w;
        q.push(st1(vr, d[vr]));
      }
    }
  }
  return d[t];
}
void dfs(int x, int y) {
  if (x > 1 && v[x - 1][y] == 0 && n[x - 1][y] == 0) {
    n[x - 1][y] = 1;
    v[x - 1][y] = k;
    dfs(x - 1, y);
    n[x - 1][y] = 0;
  }
  if (x < a && v[x + 1][y] == 0 && n[x + 1][y] == 0) {
    n[x + 1][y] = 1;
    v[x + 1][y] = k;
    dfs(x + 1, y);
    n[x + 1][y] = 0;
  }
  if (y > 1 && v[x][y - 1] == 0 && n[x][y - 1] == 0) {
    n[x][y - 1] = 1;
    v[x][y - 1] = k;
    dfs(x, y - 1);
    n[x][y - 1] = 0;
  }
  if (y < b && v[x][y + 1] == 0 && n[x][y + 1] == 0) {
    n[x][y + 1] = 1;
    v[x][y + 1] = k;
    dfs(x, y + 1);
    n[x][y + 1] = 0;
  }
}
int main() {
  scanf("%d %d\\n", &a, &b);
  scanf("%d %d\\n", &x2, &y2);
  scanf("%d %d", &x1, &y_1);
  for (int i = 1; i <= a; i++) {
    scanf("\\n");
    for (int j = 1; j <= b; j++) {
      scanf("%c", &c);
      if (c == '#')
        n[i][j] = 1;
    }
  }
  for (int i = 1; i <= a; i++) {
    for (int j = 1; j <= b; j++) {
      if (v[i][j] == 0 && n[i][j] == 0) {
        k++;
        v[i][j] = k;
        n[i][j] = 1;
        dfs(i, j);
        n[i][j] = 0;
      }
    }
  }
  for (int i = 1; i <= a; i++) {
    for (int j = 1; j <= b; j++) {
      for (int i1 = max(1, i - 2); i1 <= min(a, i + 2); i1++) {
        for (int j1 = max(1, j - 2); j1 <= min(b, j + 2); j1++) {
          if (v[i][j] != 0 && v[i1][j1] != 0 && v[i][j] != v[i1][j1]) {
            ans[v[i][j]].push_back(st(v[i1][j1], 1));
            ans[v[i1][j1]].push_back(st(v[i][j], 1));
          }
        }
      }
    }
  }
  if (Dijkstra(v[x1][y_1], v[x2][y2]) == 0x3f3f3f3f) {
    printf("-1");
  } else
    printf("%d", Dijkstra(v[x1][y_1], v[x2][y2]));
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, m, xx, yy, bx, by, ans = 0x3f3f3f3f;
bool a[1005][1005];
int ex[4] = {1, 0, -1, 0};
int ey[4] = {0, 1, 0, -1};

void dfs(int x, int y, int cnt) {
  if (cnt >= ans)
    return;
  if (x == bx && y == by) {
    if (cnt < ans)
      ans = cnt;
    return;
  }
  bool l = 0;
  for (int i = 0; i < 4; i++) {
    int nx = x + ex[i], ny = y + ey[i];
    if (!a[nx][ny] && nx >= 1 && ny >= 1 && nx <= n && ny <= m) {
      a[nx][ny] = 1, l = 1;
      dfs(nx, ny, cnt);
      a[nx][ny] = 0;
    }
  }
  if (!l) {
    for (int i = x - 2; i <= x + 2; i++) {
      for (int j = y - 2; j <= y + 2; j++) {
        if (!a[i][j] && i >= 1 && j >= 1 && i <= n && j <= m) {
          a[i][j] = 1;
          dfs(i, j, cnt + 1);
          a[i][j] = 0;
        }
      }
    }
  }
  return;
}

int main() {
  scanf("%d %d %d %d %d %d", &n, &m, &xx, &yy, &bx, &by);
  for (int i = 1; i <= n; i++) {
    getchar();
    for (int j = 1; j <= m; j++) {
      char y;
      scanf("%c", &y);
      if (y == '#')
        a[i][j] = 1;
    }
  }
  a[xx][yy] = 1;
  dfs(xx, yy, 0);
  if (ans == 0x3f3f3f3f)
    printf("-1");
  else
    printf("%d", ans);
  return 0;
}