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

#include <algorithm>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
const int N = 1e3 + 10;
typedef long long int ll;
char g[N][N];
int vis[N][N];
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
int sx, sy, ex, ey, n, m;
struct node {
  int x, y, step;
  node(int x, int y, int step) : x(x), y(y), step(step) {}
  node() {}
  bool operator<(const node &other) const { return this->step > other.step; }
};

void bfs() {
  priority_queue<node> q;
  q.push(node(sx, sy, 0));
  while (!q.empty()) {
    node now = q.top();
    q.pop();
    if (now.x == ex && now.y == ey) {
      cout << now.step << endl;
      return;
    }
    if (vis[now.x][now.y])
      continue;
    vis[now.x][now.y] = 1;
    for (int i = 0; i < 4; i++) {
      int nx = now.x + dx[i];
      int ny = now.y + dy[i];
      if (nx > n || nx < 1 || ny > m || ny < 1)
        continue;
      if (g[nx][ny] == '#' || vis[nx][ny])
        continue;
      q.push(node(nx, ny, now.step));
    }
    for (int nx = now.x - 2; nx <= now.x + 2; nx++) {
      if (nx < 1 || nx > n)
        continue;
      for (int ny = now.y - 2; ny <= now.y + 2; ny++) {
        if (ny > m || ny < 1)
          continue;
        if (g[nx][ny] == '#' || vis[nx][ny])
          continue;
        q.push(node(nx, ny, now.step + 1));
      }
    }
  }
  cout << -1 << endl;
}
int main() {
  scanf("%d%d", &n, &m);
  scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
  for (int i = 1; i <= n; i++)
    scanf("%s", &g[i][1]);
  bfs();
  return 0;
}
/*
3 4
1 0 0 0
0 0 0 0
0 0 0 2
*/

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
const int N = 1e3 + 10;
typedef long long int ll;
char g[N][N];
int vis[N][N], step[N][N];
int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
int sx, sy, ex, ey, n, m;
void bfs() {
  memset(step, 0x3f3f3f3f, sizeof(step));
  queue<int> q;
  q.push(sx);
  q.push(sy);
  vis[sx][sy] = 1;
  step[sx][sy] = 0;
  while (!q.empty()) {
    int nowx = q.front();
    q.pop();
    int nowy = q.front();
    q.pop();
    vis[nowx][nowy] = 0;
    for (int i = 0; i < 4; i++) {
      int nx = nowx + dx[i];
      int ny = nowy + dy[i];
      if (nx > n || nx < 1 || ny > m || ny < 1)
        continue;
      if (g[nx][ny] == '#')
        continue;
      if (step[nowx][nowy] < step[nx][ny]) {
        step[nx][ny] = step[nowx][nowy];
        if (!vis[nx][ny]) {
          q.push(nx);
          q.push(ny);
          vis[nx][ny] = 1;
        }
      }
    }
    for (int nx = nowx - 2; nx <= nowx + 2; nx++) {
      if (nx < 1 || nx > n)
        continue;
      for (int ny = nowy - 2; ny <= nowy + 2; ny++) {
        if (ny > m || ny < 1)
          continue;
        if (g[nx][ny] == '#')
          continue;
        if (step[nowx][nowy] + 1 < step[nx][ny]) {
          step[nx][ny] = step[nowx][nowy] + 1;
          if (!vis[nx][ny]) {
            q.push(nx);
            q.push(ny);
            vis[nx][ny] = 1;
          }
        }
      }
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
  for (int i = 1; i <= n; i++)
    scanf("%s", &g[i][1]);
  bfs();
  if (step[ex][ey] == 0x3f3f3f3f)
    cout << -1 << endl;
  else
    cout << step[ex][ey] << endl;
  return 0;
}
/*
3 4
1 0 0 0
0 0 0 0
0 0 0 2
*/