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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = (1 << 30) - 1;
const ll LINF = (1LL << 60) - 1;

int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> maze(h);
  for (int i = 0; i < h; ++i) {
    cin >> maze[i];
  }

  int res[1010][1010];
  for (int i = 0; i < 1010; ++i) {
    for (int j = 0; j < 1010; ++j) {
      res[i][j] = INF;
    }
  }

  queue<pair<pii, int>> que;
  queue<pair<pii, int>> que2;
  res[ch][cw] = 0;
  que.push(make_pair(pii(ch, cw), 0));
  while (!que.empty()) {
    pair<pii, int> p = que.front();
    que.pop();
    int x = p.first.first;
    int y = p.first.second;
    if (p.second > res[x][y])
      continue;

    // 移動A
    for (int i = 0; i < 4; ++i) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w)
        continue;
      if (maze[nx][ny] == '#')
        continue;

      if (res[x][y] >= res[nx][ny])
        continue;
      res[nx][ny] = res[x][y];
      que.push(make_pair(pii(nx, ny), res[nx][ny]));
    }
    // 移動B
    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        if (i == 0 && j == 0)
          continue;
        int nx = x + i;
        int ny = y + j;
        if (nx < 0 || nx >= h || ny < 0 || ny >= w)
          continue;
        if (maze[nx][ny] == '#')
          continue;

        if (res[x][y] + 1 >= res[nx][ny])
          continue;
        res[nx][ny] = res[x][y] + 1;
        que2.push(make_pair(pii(nx, ny), res[nx][ny]));
      }
    }

    if (que.empty()) { // 歩き優先で
      while (!que2.empty()) {
        pair<pii, int> p = que2.front();
        que2.pop();
        que.push(p);
      }
    }
  }

  cout << (res[dh][dw] == INF ? -1 : res[dh][dw]) << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = (1 << 30) - 1;
const ll LINF = (1LL << 60) - 1;

int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> maze(h);
  for (int i = 0; i < h; ++i) {
    cin >> maze[i];
  }

  int res[1010][1010];
  for (int i = 0; i < 1010; ++i) {
    for (int j = 0; j < 1010; ++j) {
      res[i][j] = INF;
    }
  }

  queue<pair<pii, int>> que;
  res[ch][cw] = 0;
  que.push(make_pair(pii(ch, cw), 0));
  while (!que.empty()) {
    pair<pii, int> p = que.front();
    que.pop();
    int x = p.first.first;
    int y = p.first.second;
    if (p.second > res[x][y])
      continue;

    // 移動A
    for (int i = 0; i < 4; ++i) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w)
        continue;
      if (maze[nx][ny] == '#')
        continue;

      if (res[x][y] >= res[nx][ny])
        continue;
      res[nx][ny] = res[x][y];
      que.push(make_pair(pii(nx, ny), res[nx][ny]));
    }
    // 移動B
    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        if (i == 0 && j == 0)
          continue;
        int nx = x + i;
        int ny = y + j;
        if (nx < 0 || nx >= h || ny < 0 || ny >= w)
          continue;
        if (maze[nx][ny] == '#')
          continue;

        if (res[x][y] + 1 >= res[nx][ny])
          continue;
        res[nx][ny] = res[x][y] + 1;
        que.push(make_pair(pii(nx, ny), res[nx][ny]));
      }
    }
  }

  cout << (res[dh][dw] == INF ? -1 : res[dh][dw]) << endl;
}