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

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

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

int main() {
  int h, w;
  int s_h, s_w;
  int e_h, e_w;
  int cost = 0;
  cin >> h >> w >> s_h >> s_w >> e_h >> e_w;
  vector<string> m(h);
  for (int i = 0; i < h; i++) {
    cin >> m[i];
  }
  vector<vector<int>> dist(h, vector<int>(w, -1));
  vector<vector<int>> warp(h, vector<int>(w, -1));

  dist[s_h - 1][s_w - 1] = 0;

  queue<pair<int, int>> que;
  queue<pair<int, int>> warp_que;

  que.push(make_pair(s_h - 1, s_w - 1));

  while (!que.empty()) {
    while (!que.empty()) {
      pair<int, int> cur = que.front();
      int x = cur.first;
      int y = cur.second;
      que.pop();

      for (int dir = 0; dir < 4; ++dir) {
        int next_x = x + dx[dir];
        int next_y = y + dy[dir];
        if (next_x < 0 || next_x >= h || next_y < 0 || next_y >= w)
          continue;
        if (m[next_x][next_y] == '#')
          continue;

        if (dist[next_x][next_y] == -1) {
          que.push(make_pair(next_x, next_y));
          dist[next_x][next_y] = dist[x][y] + 1;
        }
      }
      for (int k = x - 2; k <= x + 2; k++) {
        for (int l = y - 2; l <= y + 2; l++) {
          if (k < 0 || k >= h || l < 0 || l >= w)
            continue;
          if (m[k][l] == '#')
            continue;
          if (warp[k][l] == -1) {
            warp[k][l] = cost + 1;
            warp_que.push(make_pair(k, l));
          }
        }
      }
    }
    if (dist[e_h - 1][e_w - 1] != -1)
      break;

    while (!warp_que.empty()) {
      pair<int, int> w_cur = warp_que.front();
      int w_x = w_cur.first;
      int w_y = w_cur.second;
      warp_que.pop();

      if (dist[w_x][w_y] == -1 && warp[w_x][w_y] != -1) {
        que.push(make_pair(w_x, w_y));
        dist[w_x][w_y] = 0;
      }
    }
    cost++;
  }
  if (dist[e_h - 1][e_w - 1] != -1)
    cout << cost << endl;
  else
    cout << -1 << endl;
}


Error: TLE

Fixed submission:

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

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

int main() {
  int h, w;
  int s_h, s_w;
  int e_h, e_w;
  int cost = 0;
  cin >> h >> w >> s_h >> s_w >> e_h >> e_w;
  vector<string> m(h);
  for (int i = 0; i < h; i++) {
    cin >> m[i];
  }
  vector<vector<int>> dist(h, vector<int>(w, -1));
  vector<vector<int>> warp(h, vector<int>(w, -1));

  dist[s_h - 1][s_w - 1] = 0;

  queue<pair<int, int>> que;
  que.push(make_pair(s_h - 1, s_w - 1));

  while (!que.empty()) {
    while (!que.empty()) {
      pair<int, int> cur = que.front();
      int x = cur.first;
      int y = cur.second;
      que.pop();

      for (int k = x - 2; k <= x + 2; k++) {
        for (int l = y - 2; l <= y + 2; l++) {
          if (k < 0 || k >= h || l < 0 || l >= w)
            continue;
          if (m[k][l] == '#')
            continue;
          if (warp[k][l] == -1) {
            warp[k][l] = cost + 1;
          }
        }
      }
      for (int dir = 0; dir < 4; ++dir) {
        int next_x = x + dx[dir];
        int next_y = y + dy[dir];
        if (next_x < 0 || next_x >= h || next_y < 0 || next_y >= w)
          continue;
        if (m[next_x][next_y] == '#')
          continue; // 壁はダメ

        if (dist[next_x][next_y] == -1) {
          que.push(make_pair(next_x, next_y));
          dist[next_x][next_y] = dist[x][y] + 1;
        }
      }
    }
    if (dist[e_h - 1][e_w - 1] != -1)
      break;

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {

        if (dist[i][j] == -1 && warp[i][j] != -1) {
          que.push(make_pair(i, j));
          dist[i][j] = 0;
        }
      }
    }
    cost++;
  }
  if (dist[e_h - 1][e_w - 1] != -1)
    cout << cost << endl;
  else
    cout << -1 << endl;
}