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

#include <sys/time.h>

#include <algorithm>
#include <cctype>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

using ll = long long;
using P = pair<int, int>;
using VP = pair<int, pair<int, int>>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mkp(a, b) make_pair(a, b)
ll mod = 1000000007LL;

int h, w;
int dp[1010][1010];

string m[1010];

void solve(int y, int x, int val) {
  if (y < 0 || y >= h || x < 0 || x >= w)
    return;
  if (dp[y][x] <= val)
    return;
  if (m[y][x] == '#')
    return;

  dp[y][x] = val;

  // cout << "y:" << y << " x:" << x << " val:" << val << endl;

  solve(y, x + 1, val);
  solve(y, x - 1, val);
  solve(y + 1, x, val);
  solve(y - 1, x, val);

  for (int dx = -2; dx <= 2; dx++) {
    for (int dy = -2; dy <= 2; dy++) {
      int nx = x + dx;
      int ny = y + dy;
      if (m[ny][nx] == '#')
        continue;
      if (ny < 0 || ny >= h || nx < 0 || nx >= w)
        continue;
      if (dp[ny][nx] <= val)
        continue;

      solve(ny, nx, val + 1);
    }
  }
}

int main() {
  priority_queue<VP, vector<VP>, greater<VP>> que;

  int sx, gx, sy, gy;

  cin >> h >> w;
  cin >> sy >> sx >> gy >> gx;

  sy--;
  sx--;
  gy--;
  gx--;

  rep(i, h) { cin >> m[i]; }

  rep(y, h) {
    rep(x, w) { dp[y][x] = 10000000; }
  }

  que.push(make_pair(0, make_pair(sy, sx)));

  while (!que.empty()) {
    int dxs[] = {-1, 1, 0, 0};
    int dys[] = {0, 0, -1, 1};

    VP cur = que.top();
    que.pop();
    int val = cur.first;
    int y = cur.second.first;
    int x = cur.second.second;

    if (dp[y][x] <= val)
      continue;
    dp[y][x] = val;

    for (int i = 0; i < 4; i++) {
      int nx = x + dxs[i];
      int ny = y + dys[i];
      if (ny < 0 || ny >= h || nx < 0 || nx >= w)
        continue;
      if (m[ny][nx] == '#')
        continue;
      if (dp[ny][nx] <= val)
        continue;
      que.push(make_pair(val, make_pair(ny, nx)));
    }

    for (int dx = -2; dx <= 2; dx++) {
      for (int dy = -2; dy <= 2; dy++) {
        if (abs(dx) + abs(dy) <= 1)
          continue;

        int nx = x + dx;
        int ny = y + dy;
        if (ny < 0 || ny >= h || nx < 0 || nx >= w)
          continue;

        if (m[ny][nx] == '#')
          continue;
        if (dp[ny][nx] <= val)
          continue;
        que.push(make_pair(val + 1, make_pair(ny, nx)));
      }
    }
  }

  if (dp[gy][gx] == 10000000) {
    cout << -1 << endl;
  } else {
    cout << dp[gy][gx] << endl;
  }
  /*
          rep(i, h) {
                  rep(j ,w) {
                          cout << dp[i][j] << " ";
                  }
                  cout << endl;
          }
  */
  return 0;
}


Error: TLE

Fixed submission:

#include <sys/time.h>

#include <algorithm>
#include <cctype>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

using ll = long long;
using P = pair<int, int>;
// using VP = pair<int, pair<int, int> > ;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mkp(a, b) make_pair(a, b)
ll mod = 1000000007LL;

int h, w;
int dp[1010][1010];
int kri_dp[1010][1010];
string m[1010];

void solve(int y, int x, int val) {
  if (y < 0 || y >= h || x < 0 || x >= w)
    return;
  if (dp[y][x] <= val)
    return;
  if (m[y][x] == '#')
    return;

  dp[y][x] = val;
  kri_dp[y][x] = val;
  // cout << "y:" << y << " x:" << x << " val:" << val << endl;

  solve(y, x + 1, val);
  solve(y, x - 1, val);
  solve(y + 1, x, val);
  solve(y - 1, x, val);

  for (int dx = -2; dx <= 2; dx++) {
    for (int dy = -2; dy <= 2; dy++) {
      int nx = x + dx;
      int ny = y + dy;
      if (m[ny][nx] == '#')
        continue;
      if (ny < 0 || ny >= h || nx < 0 || nx >= w)
        continue;
      if (dp[ny][nx] <= val)
        continue;
      if (kri_dp[ny][nx] <= val)
        continue;
      kri_dp[ny][nx] = val;

      solve(ny, nx, val + 1);
    }
  }
}

int main() {
  // priority_queue<int, vector<int>, greater<int> > queue;

  int sx, gx, sy, gy;

  cin >> h >> w;
  cin >> sy >> sx >> gy >> gx;

  sy--;
  sx--;
  gy--;
  gx--;

  rep(i, h) { cin >> m[i]; }

  rep(y, h) {
    rep(x, w) {
      dp[y][x] = 10000000;
      kri_dp[y][x] = 10000000;
    }
  }

  solve(sy, sx, 0);

  if (dp[gy][gx] == 10000000) {
    cout << -1 << endl;
  } else {
    cout << dp[gy][gx] << endl;
  }
  /*
          rep(i, h) {
                  rep(j ,w) {
                          cout << dp[i][j] << " ";
                  }
                  cout << endl;
          }
  */
  return 0;
}