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

#include <cstring>
#include <iostream>
#include <queue>
using namespace std;

const int MAXN = 1e3 + 5;

int n, m;
int sx, sy, tx, ty;
int ans[MAXN][MAXN];
int tox[10] = {0, 1, -1, 0, 0};
int toy[10] = {0, 0, 0, 1, -1};

char map[MAXN][MAXN];

bool vis[MAXN][MAXN];

int bfs();

struct node {
  int x, y, val;
  node() {}
  node(int X, int Y, int VAL) { x = X, y = Y, val = VAL; }
};

bool operator<(const node &x, const node &y) { return x.val > y.val; }

int main() {
  cin >> n >> m;
  cin >> sx >> sy >> tx >> ty;
  for (int i = 1; i <= n; i++) {
    scanf("%s", map[i] + 1);
  }
  //	printf ("\\n");
  //	for (int i = 1; i <= n; i++) {
  //		for (int j = 1; j <= m; j++) {
  //			printf ("%c", map[i][j]);
  //		}
  //		printf ("\\n");
  //	}
  //	printf ("\\n");
  int ans = bfs();
  if (ans == 0x3f3f3f3f)
    cout << "-1";
  else
    cout << ans;
}

int bfs() {
  memset(ans, 0x3f, sizeof ans);
  priority_queue<node> q;
  q.push(node(sx, sy, 0));
  ans[sx][sy] = 0;
  while (!q.empty()) {
    node tem = q.top();
    q.pop();
    int x = tem.x, y = tem.y;
    if (vis[x][y] == 1)
      continue;
    for (int i = 1; i <= 4; i++) {
      int xx = x + tox[i], yy = y + toy[i];
      if (xx < 1 || xx > n)
        continue;
      if (yy < 1 || yy > m)
        continue;
      if (map[xx][yy] == '#')
        continue;
      //			cout << xx << " " << yy << endl;
      if (ans[xx][yy] > ans[x][y]) {
        ans[xx][yy] = ans[x][y];
        q.push(node(xx, yy, ans[xx][yy]));
      }
    }
    for (int i = x - 2; i <= x + 2; i++) {
      for (int j = y - 2; j <= y + 2; j++) {
        if (i < 1 || i > n)
          break;
        if (j < 1 || j > m)
          continue;
        if (map[i][j] == '#')
          continue;
        //				cout << i << " " << j << endl;
        if (ans[i][j] > ans[x][y] + 1) {
          ans[i][j] = ans[x][y] + 1;
          q.push(node(i, j, ans[i][j]));
        }
      }
    }
    //		for (int i = 1; i <= n; i++) {
    //			for (int j = 1; j <= m; j++)
    //				printf ("%d ", ans[i][j]);
    //			printf ("\\n");
    //		}
    //		printf ("\\n\\n\\n");
  }
  return ans[tx][ty];
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
using namespace std;

const int MAXN = 1e3 + 5;

int n, m;
int sx, sy, tx, ty;
int ans[MAXN][MAXN];
int tox[10] = {0, 1, -1, 0, 0};
int toy[10] = {0, 0, 0, 1, -1};

char map[MAXN][MAXN];

int bfs();

struct node {
  int x, y, val;
  node() {}
  node(int X, int Y, int VAL) { x = X, y = Y, val = VAL; }
};

int main() {
  cin >> n >> m;
  cin >> sx >> sy >> tx >> ty;
  for (int i = 1; i <= n; i++) {
    scanf("%s", map[i] + 1);
  }
  //	printf ("\\n");
  //	for (int i = 1; i <= n; i++) {
  //		for (int j = 1; j <= m; j++) {
  //			printf ("%c", map[i][j]);
  //		}
  //		printf ("\\n");
  //	}
  //	printf ("\\n");
  int ans = bfs();
  if (ans == 0x3f3f3f3f)
    cout << "-1";
  else
    cout << ans;
}

int bfs() {
  memset(ans, 0x3f, sizeof ans);
  queue<node> q;
  q.push(node(sx, sy, 0));
  ans[sx][sy] = 0;
  while (!q.empty()) {
    node tem = q.front();
    q.pop();
    int x = tem.x, y = tem.y;
    if (tem.val > ans[x][y])
      continue;
    for (int i = 1; i <= 4; i++) {
      int xx = x + tox[i], yy = y + toy[i];
      if (xx < 1 || xx > n)
        continue;
      if (yy < 1 || yy > m)
        continue;
      if (map[xx][yy] == '#')
        continue;
      //			cout << xx << " " << yy << endl;
      if (ans[xx][yy] > ans[x][y]) {
        ans[xx][yy] = ans[x][y];
        q.push(node(xx, yy, ans[xx][yy]));
      }
    }
    for (int i = x - 2; i <= x + 2; i++) {
      for (int j = y - 2; j <= y + 2; j++) {
        if (i < 1 || i > n)
          break;
        if (j < 1 || j > m)
          continue;
        if (map[i][j] == '#')
          continue;
        //				cout << i << " " << j << endl;
        if (ans[i][j] > ans[x][y] + 1) {
          ans[i][j] = ans[x][y] + 1;
          q.push(node(i, j, ans[i][j]));
        }
      }
    }
    //		for (int i = 1; i <= n; i++) {
    //			for (int j = 1; j <= m; j++)
    //				printf ("%d ", ans[i][j]);
    //			printf ("\\n");
    //		}
    //		printf ("\\n\\n\\n");
  }
  return ans[tx][ty];
}