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

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

struct point {
  int x;
  int y;
  int cost;
  point() {
    x = 0;
    y = 0;
    cost = 0;
  }
  point(int x, int y, int cost = 0) {
    this->x = x;
    this->y = y;
    this->cost = cost;
  }
  bool operator<(const point &a) const { return cost < a.cost; }
  bool operator>(const point &a) const { return cost > a.cost; }
  void print() { printf("x:%2d y:%2d cost:%3d\\n", x, y, cost); }
};

vector<vector<char>> g;
int h, w;

int Dijkstra(point c, point d) {
  priority_queue<point, vector<point>, greater<point>> que;
  que.push(c);
  int dx[4] = {0, 1, 0, -1};
  int dy[4] = {1, 0, -1, 0};
  vector<vector<bool>> visited(h, vector<bool>(w, false));
  while (!que.empty()) {
    point now = que.top();
    que.pop();
    if (visited[now.y][now.x])
      continue;
    visited[now.y][now.x] = true;
    if (now.x == d.x && now.y == d.y)
      return now.cost;
    point next;
    rep(i, 4) {
      next = now;
      next.x += dx[i];
      next.y += dy[i];
      if (next.x < 0 || next.x >= w || next.y < 0 || next.y >= h)
        continue;
      if (g[next.y][next.x] == '#')
        continue;
      if (visited[next.y][next.x])
        continue;
      que.push(next);
    }
    /*
     * warp
     */
    rep(i, 5) rep(j, 5) {
      next = now;
      next.x += i - 2;
      next.y += j - 2;
      if (next.x < 0 || next.x >= w || next.y < 0 || next.y >= h)
        continue;
      if (g[next.y][next.x] == '#')
        continue;
      if (visited[next.y][next.x])
        continue;
      que.push(point(next.x, next.y, now.cost + 1));
    }
  }
  return -1;
}

int main() {
  cin >> h >> w;
  g.resize(h);
  rep(i, h) g[i].resize(w);
  point c;
  cin >> c.y >> c.x;
  c.x--;
  c.y--;
  point d;
  cin >> d.y >> d.x;
  d.x--;
  d.y--;
  rep(i, h) rep(j, w) cin >> g[i][j];
  cout << Dijkstra(c, d) << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int H, W;
int c[2];
int d[2];
vector<vector<char>> g;

vector<vector<bool>> visited;
const ll INF = 1000000000000000;
ll warp = 0;
ll ans = INF;
void dfs(int now[2]) {
  int dx[4] = {0, 1, 0, -1};
  int dy[4] = {1, 0, -1, 0};
  visited[now[0]][now[1]] = true;
  bool flag = true;
  int next[2];
  if (now[0] == d[0] && now[1] == d[1]) {
    ans = min(ans, warp);
    visited[now[0]][now[1]] = false;
    return;
  }
  rep(i, 4) {
    next[0] = now[0] + dy[i];
    next[1] = now[1] + dx[i];
    if (next[0] < 0 || next[0] >= H || next[1] < 0 || next[1] >= W)
      continue;
    if (visited[next[0]][next[1]] == true)
      continue;
    if (g[next[0]][next[1]] == '#')
      continue;
    dfs(next);
  }
  // warp
  rep(i, 4) {
    next[0] = now[0] + dy[i];
    next[1] = now[1] + dx[i];
    if (!(next[0] < 0 || next[0] >= H || next[1] < 0 || next[1] >= W)) {
      if (!(visited[next[0]][next[1]] == true || g[next[0]][next[1]] == '#')) {
        flag = false;
      }
    }
  }
  if (flag) {
    rep(j, 5) rep(k, 5) {
      next[0] = now[0] + j - 2;
      next[1] = now[1] + k - 2;
      if (next[0] < 0 || next[0] >= H || next[1] < 0 || next[1] >= W)
        continue;
      if (visited[next[0]][next[1]] == true)
        continue;
      if (g[next[0]][next[1]] == '#')
        continue;
      warp++;
      dfs(next);
      warp--;
    }
  }
  visited[now[0]][now[1]] = false;
}

int main() {
  cin >> H >> W;
  cin >> c[0] >> c[1];
  cin >> d[0] >> d[1];
  c[0]--;
  c[1]--;
  d[0]--;
  d[1]--;
  visited.resize(H);
  rep(i, H) visited[i].resize(W);
  g.resize(H);
  rep(i, H) g[i].resize(W);
  rep(i, H) rep(j, W) cin >> g[i][j];
  dfs(c);
  if (ans == INF) {
    cout << -1 << endl;
    return 0;
  }
  cout << ans << endl;
}