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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)

typedef int long long ll;
using namespace std;
typedef pair<int, int> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int d[5] = {-2, -1, 0, 1, 2};
const ll MOD = 1e9 + 7;
static const int MAX = 100;
static const int INF = (1 << 23);
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
int h, w;
int ch, cw, dh, dw;
vector<vector<int>> v(1001, vector<int>(1001, 0));
vector<vector<int>> miti(1001, vector<int>(1001, MOD));

int main() {
  cin >> h >> w;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  rep(i, h) {
    rep(j, w) {
      char s;
      cin >> s;
      if (s == '#')
        v[i][j] = 1;
    }
  }

  miti[ch][cw] = 0;
  deque<P> q;
  q.push_front({ch, cw});
  while (!q.empty()) {
    P now = q.front();
    q.pop_front();
    rep(i, 4) {
      int ny = now.first + dy[i];
      int nx = now.second + dx[i];
      if (ny < 0 || nx < 0 || ny >= h || nx >= w || v[ny][nx] != 0)
        continue;
      if (miti[ny][nx] > miti[now.first][now.second]) {
        miti[ny][nx] = miti[now.first][now.second];
        q.push_front({ny, nx});
      }
    }
    rep(i, 5) {
      rep(j, 5) {
        int ny = now.first + d[i];
        int nx = now.second + d[j];
        if (ny < 0 || nx < 0 || ny >= h || nx >= w || v[ny][nx] != 0)
          continue;
        if (miti[ny][nx] > miti[now.first][now.second] + 1) {
          miti[ny][nx] = miti[now.first][now.second] + 1;
          q.push_back({ny, nx});
        }
      }
    }
  }

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

  cout << (miti[dh][dw] == MOD ? -1 : miti[dh][dw]) << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)

typedef int long long ll;
using namespace std;
typedef pair<int, int> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int dxx[5] = {-2, -1, 0, 1, 2};
const ll MOD = 1e9 + 7;
static const int MAX = 100;
static const int INF = (1 << 23);
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
int h, w;
int ch, cw, dh, dw;
vector<vector<int>> v(1001, vector<int>(1001, 0));
vector<vector<bool>> f(1001, vector<bool>(1001, false));
vector<vector<int>> miti(1001, vector<int>(1001, MOD));
set<pair<int, int>> nes;
void dfs(int y, int x) {
  bool idoua = true;
  rep(i, 4) {
    int ny = dy[i] + y;
    int nx = dx[i] + x;
    if (ny < 0 || nx < 0 || ny >= h || nx >= w || v[ny][nx] == 1)
      continue;
    if (miti[ny][nx] > miti[y][x]) {
      // idoua = true;
      miti[ny][nx] = miti[y][x];
      dfs(ny, nx);
    }
  }

  if (idoua) {
    for (auto &a : nes) {
      int ny = a.first + y;

      int nx = a.second + x;

      if (ny < 0 || nx < 0 || ny >= h || nx >= w || v[ny][nx] == 1)
        continue;
      if (miti[ny][nx] > miti[y][x] + 1) {
        miti[ny][nx] = 1 + miti[y][x];

        dfs(ny, nx);
      }
    }
  }
}

int main() {
  rep(i, 5) {
    rep(j, 5) { nes.insert({dxx[i], dxx[j]}); }
  }

  rep(i, 4) {
    pair<int, int> k = {dx[i], dy[i]};
    if (nes.find(k) != nes.end()) {
      nes.erase(k);
    }
  }

  cin >> h >> w;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  rep(i, h) {
    rep(j, w) {
      char s;
      cin >> s;
      if (s == '#')
        v[i][j] = 1;
    }
  }
  f[ch][cw] = true;
  miti[ch][cw] = 0;
  dfs(ch, cw);
  cout << (miti[dh][dw] == MOD ? -1 : miti[dh][dw]) << endl;

  return 0;
}