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

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <queue>
using namespace std;
const int M = 1e3 * 2;
char s[M][M];
int n, m;
int s_x, s_y, t_x, t_y, ans[M][M];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
struct node {
  int x, y;
};
void bfs() {
  deque<node> q;
  node a, b;
  a.x = s_x;
  a.y = s_y;
  ans[s_x][s_y] = 0;
  q.push_back(a);
  while (q.size()) {
    a = q.front();
    q.pop_front();
    if (a.x == t_x && a.y == t_y) {
      printf("%d", ans[t_x][t_y]);
      return;
    }
    for (int i = 0; i < 4; i++) {
      int nx = a.x + dx[i];
      int ny = a.y + dy[i];
      if (nx <= 0 || nx > n || ny <= 0 || ny > m)
        continue;
      if (s[nx][ny] == '.') {
        if (ans[nx][ny] > ans[a.x][a.y]) {
          b.x = nx;
          b.y = ny;
          ans[nx][ny] = ans[a.x][a.y];
          q.push_front(b);
        }
      }
    }
    for (int i = a.x - 2; i <= a.x + 2; i++) {
      for (int j = a.y - 2; j <= a.y + 2; j++) {
        int nx = i;
        int ny = j;
        if (nx <= 0 || nx > n || ny <= 0 || ny > m)
          continue;
        if (s[nx][ny] == '.') {
          if (ans[nx][ny] > ans[a.x][a.y] + 1) {
            b.x = nx;
            b.y = ny;
            ans[nx][ny] = ans[a.x][a.y] + 1;
            q.push_back(b);
          }
        }
      }
    }
  }
  printf("-1");
  return;
}
int main() {
  memset(ans, 0x3f, sizeof(ans));
  scanf("%d %d", &n, &m);
  scanf("%d %d", &s_x, &s_y);
  scanf("%d %d", &t_x, &t_y);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> s[i][j];
    }
  }
  //	printf("%c %c\\n",s[1][2],s[1][3]);
  bfs();

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <queue>
using namespace std;
const int M = 1e3 * 2;
char s[M][M];
int n, m;
bool mp[M][M];
int s_x, s_y, t_x, t_y, ans[M][M];
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int Dx[5] = {0, 1, 2, -1, -2}, Dy[5] = {0, 1, 2, -1, -2};
struct node {
  int x, y;
};

void Make(int X, int Y) {}
void bfs() {
  deque<node> q;
  node a, b;
  a.x = s_x;
  a.y = s_y;
  ans[s_x][s_y] = 0;
  mp[s_x][s_y] = 1;
  q.push_front(a);
  while (!q.empty()) {
    a = q.front();
    q.pop_front();

    if (a.x == t_x && a.y == t_y) {
      printf("%d\\n", ans[t_x][t_y]);
      return;
    }
    //	if(mp[a.x][a.y]==1) continue;
    for (int i = 0; i < 5; i++) {
      for (int j = 0; j < 5; j++) {
        if ((i != 0) || (j != 0)) {
          int nx = a.x + Dx[i];
          int ny = a.y + Dy[j];
          if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && s[nx][ny] != '#' &&
              mp[nx][ny] == 0) {
            //			printf("%d %d\\n",nx,ny);
            //	mp[nx][ny]=1;
            b.x = nx;
            b.y = ny;
            ans[nx][ny] = min(ans[nx][ny], ans[a.x][a.y] + 1);
            // printf("%d %d\\n",b.x,b.y);
            q.push_back(b);
          }
        }
      }
    }
    for (int i = 0; i < 4; i++) {
      int nx = a.x + dx[i];
      int ny = a.y + dy[i];
      if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && s[nx][ny] != '#' &&
          mp[nx][ny] == 0) {
        mp[nx][ny] = 1;
        b.x = nx;
        b.y = ny;
        ans[nx][ny] = min(ans[nx][ny], ans[a.x][a.y]);
        // printf("%d %d\\n",b.x,b.y);
        q.push_front(b);
      }
    }
  }
  printf("-1\\n");
  return;
}
int main() {
  memset(ans, 0x3f, sizeof(ans));
  scanf("%d %d", &n, &m);
  scanf("%d %d", &s_x, &s_y);

  scanf("%d %d", &t_x, &t_y);

  for (int i = 1; i <= n; i++) {
    scanf("%s", (s[i] + 1));
  }
  bfs();

  return 0;
}