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

#include <bits/stdc++.h>

using namespace std;
typedef pair<int, int> P;
const int INF = 1e9;
int h, w, ch, cw, dh, dw;
// int ans = INF;

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

vector<P> wdxdy;

int bfs(vector<string> s) {
  vector<vector<int>> dist(h, vector<int>(w, INF));
  priority_queue<pair<int, P>, vector<pair<int, P>>, greater<pair<int, P>>> que;
  que.push(pair<int, P>(0, P(ch, cw)));
  dist[ch][cw] = 0;
  while (!que.empty()) {
    P cur = que.top().second;
    int d = que.top().first;
    que.pop();
    int cy = cur.first, cx = cur.second;

    if (dist[cy][cx] < d)
      continue;
    int cnt = 0;
    for (int i = 0; i < 4; ++i) {
      int ny = cy + dy[i];
      int nx = cx + dx[i];
      if (ny >= 0 && ny < h && nx >= 0 && nx < w && s[ny][nx] == '.' &&
          dist[ny][nx] > dist[cy][cx]) {
        cnt++;
        que.push(pair<int, P>(dist[cy][cx], P(ny, nx)));
        dist[ny][nx] = dist[cy][cx];
      }
    }
    if (cnt < 4) {
      for (int i = 0; i < wdxdy.size(); ++i) {
        int ny = cur.first + wdxdy[i].first;
        int nx = cur.second + wdxdy[i].second;
        if (ny >= 0 && ny < h && nx >= 0 && nx < w && s[ny][nx] == '.' &&
            dist[ny][nx] > dist[cy][cx] + 1) {
          que.push(pair<int, P>(dist[cy][cx] + 1, P(ny, nx)));
          dist[ny][nx] = dist[cy][cx] + 1;
        }
      }
    }
  }
  return dist[dh][dw];
}

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

  for (int i = -2; i <= 2; ++i) {
    for (int j = -2; j <= 2; ++j) {
      if (i == 0 && j == 0)
        continue;
      wdxdy.push_back(P(i, j));
    }
  }
  // for (int i = 0; i < 4; ++i) {
  // }
  // dfs(s, P(ch, cw), 0);
  int ans = bfs(s);
  if (ans == INF)
    puts("-1");
  else
    cout << ans << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef pair<int, int> P;
const int INF = 1e9;
int h, w, ch, cw, dh, dw;
// int ans = INF;

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

vector<P> wdxdy;
bool visited[1005][1005];

int bfs(vector<string> s) {
  vector<vector<int>> dist(h, vector<int>(w, INF));
  queue<P> que;
  que.push(P(ch, cw));
  dist[ch][cw] = 0;
  while (!que.empty()) {
    P cur = que.front();
    que.pop();
    int cy = cur.first, cx = cur.second;
    int cnt = 0;
    for (int i = 0; i < 4; ++i) {
      int ny = cur.first + dy[i];
      int nx = cur.second + dx[i];
      if (ny >= 0 && ny < h && nx >= 0 && nx < w && s[ny][nx] == '.' &&
          dist[ny][nx] > dist[cy][cx]) {
        cnt++;
        que.push(P(ny, nx));
        dist[ny][nx] = dist[cy][cx];
      }
    }
    if (cnt < 4) {
      for (int i = 0; i < wdxdy.size(); ++i) {
        int ny = cur.first + wdxdy[i].first;
        int nx = cur.second + wdxdy[i].second;
        if (ny >= 0 && ny < h && nx >= 0 && nx < w && s[ny][nx] == '.' &&
            dist[ny][nx] > dist[cy][cx] + 1) {
          que.push(P(ny, nx));
          dist[ny][nx] = dist[cy][cx] + 1;
        }
      }
    }
  }
  return dist[dh][dw];
}

// void dfs(vector<string> sx, P cur, int n) {
//   if (cur.first == dh && cur.second == dw) {
//     ans = min(ans, n);
//     return;
//   }

//   visited[cur.first][cur.second] = true;
//   int cnt = 0;
//   for (int i = 0; i < 4; ++i) {
//     int ny = cur.first + dy[i];
//     int nx = cur.second + dx[i];
//     if (ny >= 0 && ny < h && nx >= 0 && nx < w && sx[ny][nx] == '.' &&
//     !visited[ny][nx]) {
//       cnt++;
//       dfs(sx, P(ny, nx), n);
//     }
//   }
//   for (int i = 0; i < wdxdy.size(); ++i) {
//     int ny = cur.first + wdxdy[i].first;
//     int nx = cur.second + wdxdy[i].second;
//     if (ny >= 0 && ny < h && nx >= 0 && nx < w && sx[ny][nx] == '.' && n + 1
//     < ans && !visited[ny][nx]) {

//       dfs(sx, P(ny, nx), n + 1);
//     }
//   }
//   return;
// }

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

  for (int i = -2; i <= 2; ++i) {
    for (int j = -2; j <= 2; ++j) {
      if (i == 0 && j == 0)
        continue;
      wdxdy.push_back(P(i, j));
    }
  }
  // for (int i = 0; i < 4; ++i) {
  // }
  // dfs(s, P(ch, cw), 0);
  int ans = bfs(s);
  if (ans == INF)
    puts("-1");
  else
    cout << ans << '\\n';
  return 0;
}