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 < n; i++)
#define MOD 1000000007
#define MOD2 998244353
#define INF MOD
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;

int shortest_length(int sy, int sx, int gy, int gx, vector<string> field,
                    char block) {
  int h = field.size();
  int w = field[0].size();
  vector<vector<int>> d(h, vector<int>(w, INF));
  d[sy][sx] = 0;
  deque<P> que;
  que.push_back(make_pair(sy, sx));
  while (que.size()) {
    P p = que.front();
    que.pop_front();
    int y = p.first;
    int x = p.second;
    // if (x == gx && y == gy) break;

    for (int dy = -2; dy <= 2; dy++) {
      for (int dx = -2; dx <= 2; dx++) {
        if ((dy == 0 && dx == 0) || (dy == 0 && dx == -1) ||
            (dy == 1 && dx == 0) || (dy == 0 && dx == 1) ||
            (dy == -1 && dx == 0)) {
          int ny = y + dy;
          int nx = x + dx;
          if (nx >= 0 && nx < w && ny >= 0 && ny < h &&
              field[ny][nx] != block && d[ny][nx] > d[y][x]) {
            d[ny][nx] = d[y][x];
            que.push_front(make_pair(ny, nx));
          }
        } else {
          int ny = y + dy;
          int nx = x + dx;
          if (nx >= 0 && nx < w && ny >= 0 && ny < h &&
              field[ny][nx] != block && d[ny][nx] > d[y][x] + 1) {
            d[ny][nx] = d[y][x] + 1;
            que.push_back(make_pair(ny, nx));
          }
        }
      }
    }
  }
  if (d[gy][gx] == INF)
    return -1;
  return d[gy][gx];
}

int main() {
  int h, w;
  int cx, cy, dx, dy;
  cin >> h >> w >> cy >> cx >> dy >> dx;
  cx--, cy--, dy--, dx--;
  vector<string> field(h);
  rep(i, h) { cin >> field[i]; }
  int ans = shortest_length(cy, cx, dy, dx, field, '#');
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define MOD 1000000007
#define MOD2 998244353
#define INF MOD
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;

int shortest_length(int sy, int sx, int gy, int gx, vector<string> field,
                    char block) {
  int h = field.size();
  int w = field[0].size();
  vector<vector<int>> d(h, vector<int>(w, INF));
  d[sy][sx] = 0;
  queue<P> que;
  que.push(make_pair(sy, sx));
  while (que.size()) {
    P p = que.front();
    que.pop();
    int y = p.first;
    int x = p.second;
    // if (x == gx && y == gy) break;

    for (int dy = -2; dy <= 2; dy++) {
      for (int dx = -2; dx <= 2; dx++) {
        if ((dy == 0 && dx == 0) || (dy == 0 && dx == -1) ||
            (dy == 1 && dx == 0) || (dy == 0 && dx == 1) ||
            (dy == -1 && dx == 0)) {
          int ny = y + dy;
          int nx = x + dx;
          if (nx >= 0 && nx < w && ny >= 0 && ny < h &&
              field[ny][nx] != block && d[ny][nx] > d[y][x]) {
            d[ny][nx] = d[y][x];
            que.push(make_pair(ny, nx));
          }
        } else {
          int ny = y + dy;
          int nx = x + dx;
          if (nx >= 0 && nx < w && ny >= 0 && ny < h &&
              field[ny][nx] != block && d[ny][nx] > d[y][x] + 1) {
            d[ny][nx] = d[y][x] + 1;
            que.push(make_pair(ny, nx));
          }
        }
      }
    }
  }
  if (d[gy][gx] == INF)
    return -1;
  return d[gy][gx];
}

int main() {
  int h, w;
  int cx, cy, dx, dy;
  cin >> h >> w >> cy >> cx >> dy >> dx;
  cx--, cy--, dy--, dx--;
  vector<string> field(h);
  rep(i, h) { cin >> field[i]; }
  int ans = shortest_length(cy, cx, dy, dx, field, '#');
  cout << ans << endl;
}