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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

const int nil = 1000010;
const int MAX = 1010;

char meiro[MAX][MAX];
int cost[MAX][MAX];
int visited[MAX][MAX];

int H, W;

void bfs(int ch, int cw) {
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cost[i][j] = nil;
      visited[i][j] = false;
    }
  }

  deque<pair<int, pair<int, int>>> q;
  q.push_front(make_pair(0, make_pair(ch, cw)));
  cost[ch][cw] = 0;

  while (q.size()) {
    pair<int, pair<int, int>> p = q.front();
    q.pop_front();
    pair<int, int> pt = p.second;
    int nowc = p.first;

    if (visited[pt.first][pt.second])
      continue;
    else
      visited[pt.first][pt.second] = true;

    cost[pt.first][pt.second] = nowc;

    for (int h = -2; h < 3; h++) {
      for (int w = -2; w < 3; w++) {
        if (h == 0 && w == 0)
          continue;

        int nexth = pt.first + h;
        int nextw = pt.second + w;
        if (nexth < 0 || nexth >= H || nextw < 0 || nextw >= W)
          continue;
        if (meiro[nexth][nextw] == '#')
          continue;

        if (visited[nexth][nextw])
          continue;

        if ((w == 0 && (h == -1 || h == 1)) ||
            (h == 0 && (w == -1 || w == 1))) {
          q.push_front(make_pair(nowc, make_pair(nexth, nextw)));
        } else {
          q.push_back(make_pair(nowc + 1, make_pair(nexth, nextw)));
        }
      }
    }
  }
}

int main() {
  cin >> H >> W;

  int ch, cw;
  int dh, dw;
  cin >> ch >> cw;
  cin >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> meiro[i][j];
    }
  }

  bfs(ch, cw);

  if (cost[dh][dw] != nil)
    cout << cost[dh][dw] << endl;
  else
    cout << "-1" << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

const int nil = 1000010;
const int MAX = 1010;

char meiro[MAX][MAX];
int cost[MAX][MAX];

int H, W;

void bfs(int ch, int cw) {
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cost[i][j] = nil;
    }
  }

  queue<pair<int, int>> q;
  q.push(make_pair(ch, cw));
  cost[ch][cw] = 0;

  while (q.size()) {
    pair<int, int> pt = q.front();
    q.pop();

    int nowc = cost[pt.first][pt.second];

    for (int h = -2; h < 3; h++) {
      for (int w = -2; w < 3; w++) {
        if (h == 0 && w == 0)
          continue;

        int nexth = pt.first + h;
        int nextw = pt.second + w;
        if (nexth < 0 || nexth >= H || nextw < 0 || nextw >= W)
          continue;
        if (meiro[nexth][nextw] == '#')
          continue;

        int nextc = 1;
        if (w == 0 && (h == -1 || h == 1))
          nextc--;
        if (h == 0 && (w == -1 || w == 1))
          nextc--;

        if (nextc + nowc < cost[nexth][nextw]) {
          q.push(make_pair(nexth, nextw));
          cost[nexth][nextw] = nextc + nowc;
        }
      }
    }
  }
}

int main() {
  cin >> H >> W;

  int ch, cw;
  int dh, dw;
  cin >> ch >> cw;
  cin >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> meiro[i][j];
    }
  }

  bfs(ch, cw);

  if (cost[dh][dw] != nil)
    cout << cost[dh][dw] << endl;
  else
    cout << "-1" << endl;

  return 0;
}