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

#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;

#define MAXN 5000
#define INF 0x7f7f7f7f

int dis[4 + 5][2] = {{}, {1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int s[MAXN + 5][MAXN + 5];
char c[MAXN + 5][MAXN + 5];
bool vis[MAXN + 5][MAXN + 5];

void scan(int &x) {
  x = 0;

  char c = getchar();
  int f = 1;

  while ('0' > c || c > '9') {
    if (c == '-') {
      f = -1;
    }

    c = getchar();
  }

  while ('0' <= c && c <= '9') {
    x = x * 10 - '0' + c;

    c = getchar();
  }

  x *= f;

  return;
}

void print(int x) {
  if (x < 0) {
    putchar('-');
    print(0 - x);

    return;
  }

  if (x >= 10) {
    print(x / 10);
  }

  putchar(x % 10 + '0');

  return;
}

struct node {
  int x, y, n;
  node() {}

  node(int a, int b, int c) {
    x = a;
    y = b;
    n = c;
  }
};
bool operator<(const node a, const node b) {
  if (a.n < b.n) {
    return 1;
  } else {
    return 0;
  }
}
bool operator>(const node a, const node b) {
  if (a.n > b.n) {
    return 1;
  } else {
    return 0;
  }
}

int main() {
  int n, m;
  int xa, ya;
  int xb, yb;
  priority_queue<node, vector<node>, greater<node>> q;

  scan(n);
  scan(m);
  scan(xa);
  scan(ya);
  scan(xb);
  scan(yb);
  for (int i = 1; i <= n; i++) {
    scanf("%s", c[i] + 1);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      s[i][j] = INF;
    }
  }
  q.push(node(xa, ya, 0));
  s[xa][ya] = 0;
  vis[xa][yb] = 1;
  while (!q.empty()) {
    node a = q.top();

    q.pop();
    vis[a.x][a.y] = 1;
    for (int i = 1; i <= 4; i++) {
      int x, y;

      x = a.x + dis[i][0];
      y = a.y + dis[i][1];
      if (x > 0 && x <= n && y > 0 && y <= m && c[x][y] != '#' &&
          s[x][y] > s[a.x][a.y]) {
        s[x][y] = s[a.x][a.y];
        if (!vis[x][y]) {
          q.push(node(x, y, s[x][y]));
        }
      }
    }
    for (int i = max(a.x - 2, 1); i <= a.x + 2; i++) {
      if (i > n) {
        break;
      }
      for (int j = max(a.y - 2, 1); j <= a.y + 2; j++) {
        if (j > m) {
          break;
        }
        if (c[i][j] != '#' && s[i][j] > s[a.x][a.y] + 1) {
          s[i][j] = s[a.x][a.y] + 1;
          if (!vis[i][j]) {
            q.push(node(i, j, s[i][j]));
          }
        }
      }
    }
  }
  if (s[xb][yb] >= INF) {
    print(-1);
  } else {
    print(s[xb][yb]);
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;

#define MAXN 5000
#define INF 0x7f7f7f7f

int dis[4 + 5][2] = {{}, {1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int s[MAXN + 5][MAXN + 5];
char c[MAXN + 5][MAXN + 5];
bool vis[MAXN + 5][MAXN + 5];

void scan(int &x) {
  x = 0;

  char c = getchar();
  int f = 1;

  while ('0' > c || c > '9') {
    if (c == '-') {
      f = -1;
    }

    c = getchar();
  }

  while ('0' <= c && c <= '9') {
    x = x * 10 - '0' + c;

    c = getchar();
  }

  x *= f;

  return;
}

void print(int x) {
  if (x < 0) {
    putchar('-');
    print(0 - x);

    return;
  }

  if (x >= 10) {
    print(x / 10);
  }

  putchar(x % 10 + '0');

  return;
}

struct node {
  int x, y;
  node() {}

  node(int a, int b) {
    x = a;
    y = b;
  }
};

int main() {
  int n, m;
  int xa, ya;
  int xb, yb;
  queue<node> q;

  scan(n);
  scan(m);
  scan(xa);
  scan(ya);
  scan(xb);
  scan(yb);
  for (int i = 1; i <= n; i++) {
    scanf("%s", c[i] + 1);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      s[i][j] = INF;
    }
  }
  q.push(node(xa, ya));
  s[xa][ya] = 0;
  while (!q.empty()) {
    node a = q.front();

    q.pop();
    vis[a.x][a.y] = 0;
    for (int i = 1; i <= 4; i++) {
      int x, y;

      x = a.x + dis[i][0];
      y = a.y + dis[i][1];
      if (x > 0 && x <= n && y > 0 && y <= m && c[x][y] != '#' &&
          s[x][y] > s[a.x][a.y]) {
        s[x][y] = s[a.x][a.y];
        if (!vis[x][y]) {
          q.push(node(x, y));
        }
      }
    }
    for (int i = max(a.x - 2, 1); i <= a.x + 2; i++) {
      if (i > n) {
        break;
      }
      for (int j = max(a.y - 2, 1); j <= a.y + 2; j++) {
        if (j > m) {
          break;
        }
        if (c[i][j] != '#' && s[i][j] > s[a.x][a.y] + 1) {
          s[i][j] = s[a.x][a.y] + 1;
          if (!vis[i][j]) {
            q.push(node(i, j));
          }
        }
      }
    }
  }
  if (s[xb][yb] >= INF) {
    print(-1);
  } else {
    print(s[xb][yb]);
  }

  return 0;
}