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

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define P pair<int, int>

typedef vector<int> vec;
typedef vector<vec> mat;

int h, w;
int ch, cw;
int dh, dw;
string s[1000];
int dp[1000][1000];
queue<pair<int, P>> que;

void f(int nh, int nw) {

  // rep(i, h){
  //     rep(j, w){
  //         cout << dp[i][j];
  //     }
  //     cout << endl;
  // }
  // cout << endl;

  int num = dp[nh][nw];
  int dx[] = {-1, 0, 1, 0};
  int dy[] = {0, 1, 0, -1};
  rep(i, 4) {
    int ah = nh + dx[i];
    int aw = nw + dy[i];
    if (0 <= ah && ah < h && 0 <= aw && aw < w && s[ah][aw] == '.') {
      if (dp[ah][aw] < 0) {
        dp[ah][aw] = num;
        f(ah, aw);
      }
    }
  }
  for (int i = -2; i <= 2; i++) {
    for (int j = -2; j <= 2; j++) {
      int ah = nh + i;
      int aw = nw + j;
      if (0 <= ah && ah < h && 0 <= aw && aw < w && s[ah][aw] == '.') {
        if (dp[ah][aw] < 0) {
          pair<int, P> p;
          p.first = num + 1;
          p.second.first = ah;
          p.second.second = aw;
          que.push(p);
        }
      }
    }
  }
}

int main() {
  cin >> h >> w;
  cin >> ch >> cw;
  ch--;
  cw--;
  cin >> dh >> dw;
  dh--;
  dw--;
  rep(i, h) cin >> s[i];
  rep(i, h) rep(j, w) dp[i][j] = -1;

  pair<int, P> p1;
  p1.first = 0;
  p1.second.first = ch;
  p1.second.second = cw;
  que.push(p1);

  while (que.size() > 0) {
    pair<int, P> p;
    p = que.front();
    que.pop();
    int nh = p.second.first;
    int nw = p.second.second;
    if (dp[nh][nw] < 0) {
      dp[nh][nw] = p.first;
      f(nh, nw);
    }
  }

  // rep(i, h){
  //     rep(j, w){
  //         cout << dp[i][j];
  //     }
  //     cout << endl;
  // }

  cout << dp[dh][dw] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define P pair<int, int>

typedef vector<int> vec;
typedef vector<vec> mat;

int h, w;
int ch, cw;
int dh, dw;
string s[1000];
int dp[1000][1000];

void f(int nh, int nw) {

  // rep(i, h){
  //     rep(j, w){
  //         cout << dp[i][j];
  //     }
  //     cout << endl;
  // }
  // cout << endl;

  int num = dp[nh][nw];
  int dx[] = {-1, 0, 1, 0};
  int dy[] = {0, 1, 0, -1};
  rep(i, 4) {
    int ah = nh + dx[i];
    int aw = nw + dy[i];
    if (0 <= ah && ah < h && 0 <= aw && aw < w && s[ah][aw] == '.') {
      if (dp[ah][aw] < 0) {
        dp[ah][aw] = num;
        f(ah, aw);
      } else if (dp[ah][aw] > num) {
        dp[ah][aw] = num;
        f(ah, aw);
      }
    }
  }
  for (int i = -2; i <= 2; i++) {
    for (int j = -2; j <= 2; j++) {
      int ah = nh + i;
      int aw = nw + j;
      if (0 <= ah && ah < h && 0 <= aw && aw < w && s[ah][aw] == '.') {
        if (dp[ah][aw] < 0) {
          dp[ah][aw] = num + 1;
          f(ah, aw);
        } else if (dp[ah][aw] > num + 1) {
          dp[ah][aw] = num + 1;
          f(ah, aw);
        }
      }
    }
  }
}

int main() {
  cin >> h >> w;
  cin >> ch >> cw;
  ch--;
  cw--;
  cin >> dh >> dw;
  dh--;
  dw--;
  rep(i, h) cin >> s[i];
  rep(i, h) rep(j, w) dp[i][j] = -1;

  dp[ch][cw] = 0;
  f(ch, cw);

  // rep(i, h){
  //     rep(j, w){
  //         cout << dp[i][j];
  //     }
  //     cout << endl;
  // }

  cout << dp[dh][dw] << endl;
}