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

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

// この２つはmazeの大きさによって可変
const int MAX_X = 1001;
const int MAX_Y = 1001;

const int INF = -1;
int h, w; // h:mazeの高さ、w:mazeの幅
char maze[MAX_X][MAX_Y];
int sx, sy, gx, gy;
int warp_n = 0;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int dx_w[24] = {-2, -1, 0,  1,  2, -2, -1, 0,  1,  2, -2, -1,
                1,  2,  -2, -1, 0, 1,  2,  -2, -1, 0, 1,  2};
int dy_w[24] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0,
                0,  0,  1,  1,  1,  1,  1,  2,  2,  2,  2, 2};
int d[MAX_X][MAX_Y];

int bfs() {

  queue<P> que;
  queue<P> que_w;
  rep(i, w) {
    rep(j, h) { d[i][j] = INF; }
  }
  que.push(P(sx, sy));
  que_w.push(P(sx, sy));
  d[sx][sy] = 0;

  while (1) {
    while (que.size()) {
      P p = que.front();
      que.pop();
      que_w.push(P(p.first, p.second));
      if (p.first == gx && p.second == gy)
        break;

      rep(i, 4) {
        int nx = p.first + dx[i];
        int ny = p.second + dy[i];

        if (0 <= nx && nx < w && 0 <= ny && ny < h && maze[nx][ny] != '#' &&
            d[nx][ny] == INF) {
          que.push(P(nx, ny));
          que_w.push(P(nx, ny));
          d[nx][ny] = d[p.first][p.second];
        }
      }
    }

    if (d[gx][gy] == INF) {
      /*
      rep(y,h){
          rep(x,w){
              if(d[x][y]==(warp_n)){
                  que_w.push(P(x,y));
              }
          }
      }
      */
      warp_n++;

      while (que_w.size()) {
        P p_w = que_w.front();
        que_w.pop();
        rep(i, 24) {
          int nx_w = p_w.first + dx_w[i];
          int ny_w = p_w.second + dy_w[i];

          if (0 <= nx_w && nx_w < w && 0 <= ny_w && ny_w < h &&
              maze[nx_w][ny_w] != '#' && d[nx_w][ny_w] == INF) {
            que.push(P(nx_w, ny_w));
            d[nx_w][ny_w] = warp_n;
            if (nx_w == gx && ny_w == gy)
              break;
          }
        }
      }

      if (que.size() == 0)
        break;
    } else {
      break;
    }
  }

  return d[gx][gy];
}

int main() {
  cin >> h >> w;
  cin >> sy >> sx >> gy >> gx;
  sy--;
  sx--;
  gy--;
  gx--;

  // mazeのinput
  string tmp;
  rep(y, h) {
    cin >> tmp;
    rep(x, w) { maze[x][y] = tmp[x]; }
  }

  int res = bfs();
  cout << res << endl;
}


Error: TLE

Fixed submission:

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

// この２つはmazeの大きさによって可変
const int MAX_X = 1001;
const int MAX_Y = 1001;

const int INF = -1;
int h, w; // h:mazeの高さ、w:mazeの幅
char maze[MAX_X][MAX_Y];
int sx, sy, gx, gy;
int warp_n = 0;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int dx_w[24] = {-2, -1, 0,  1,  2, -2, -1, 0,  1,  2, -2, -1,
                1,  2,  -2, -1, 0, 1,  2,  -2, -1, 0, 1,  2};
int dy_w[24] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0,
                0,  0,  1,  1,  1,  1,  1,  2,  2,  2,  2, 2};
int d[MAX_X][MAX_Y];

int bfs() {

  queue<P> que;
  rep(i, w) {
    rep(j, h) { d[i][j] = INF; }
  }
  que.push(P(sx, sy));
  d[sx][sy] = 0;

  while (1) {
    while (que.size()) {
      P p = que.front();
      que.pop();
      if (p.first == gx && p.second == gy)
        break;

      rep(i, 4) {
        int nx = p.first + dx[i];
        int ny = p.second + dy[i];

        if (0 <= nx && nx < w && 0 <= ny && ny < h && maze[nx][ny] != '#' &&
            d[nx][ny] == INF) {
          que.push(P(nx, ny));
          d[nx][ny] = d[p.first][p.second];
        }
      }
    }

    if (d[gx][gy] == INF) {
      queue<P> que_w;
      rep(y, h) {
        rep(x, w) {
          if (d[x][y] == (warp_n)) {
            que_w.push(P(x, y));
          }
        }
      }

      warp_n++;

      while (que_w.size()) {
        P p_w = que_w.front();
        que_w.pop();
        rep(i, 24) {
          int nx_w = p_w.first + dx_w[i];
          int ny_w = p_w.second + dy_w[i];

          if (0 <= nx_w && nx_w < w && 0 <= ny_w && ny_w < h &&
              maze[nx_w][ny_w] != '#' && d[nx_w][ny_w] == INF) {
            que.push(P(nx_w, ny_w));
            d[nx_w][ny_w] = warp_n;
            if (nx_w == gx && ny_w == gy)
              break;
          }
        }
      }

      if (que.size() == 0)
        break;
    } else {
      break;
    }
  }

  return d[gx][gy];
}

int main() {
  cin >> h >> w;
  cin >> sy >> sx >> gy >> gx;
  sy--;
  sx--;
  gy--;
  gx--;

  // mazeのinput
  string tmp;
  rep(y, h) {
    cin >> tmp;
    rep(x, w) { maze[x][y] = tmp[x]; }
  }

  int res = bfs();
  cout << res << endl;
}