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

// Author - Soumik Roy
// Date & Time - August 24, 2020 9:59 PM
// Problem name - D - Wizard in Maze
// Problem url - https://atcoder.jp/contests/abc176/tasks/abc176_d
// Time limit - 2000 ms
// Memory Limit - 1024 MB

#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define MOD 1000000007
#define vi vector<int>
#define vll vector<ll>
#define pll pair<ll, ll>
#define pii pair<int, int>
#define all(p) p.begin(), p.end()
#define mid(s, e) (s + (e - s) / 2)
#define eb emplace_back
#define ull unsigned long long
#define bug(x) cout << "  [ " #x << " = " << x << " ]" << endl;
#define KAMEHAMEHA ios_base::sync_with_stdio(0);
#define RASENGAN                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
const int INF = 1e9;
int h, w, ch, cw, dh, dw;
int dir[] = {0, 1, 0, -1, 1, 0, -1, 0};
vector<string> grid;
int bfs() {
  int magic[h + 1][w + 1];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      magic[i][j] = INF;
  }
  priority_queue<pair<int, pii>, vector<pair<int, pii>>,
                 greater<pair<int, pii>>>
      q;
  q.push(mp(0, mp(ch, cw)));
  magic[ch][cw] = 0;
  while (!q.empty()) {
    pii node = q.top().S;
    int m = magic[node.F][node.S];
    q.pop();
    for (int i = 0; i < 8; i += 2) {
      pii next = mp(node.F + dir[i], node.S + dir[i + 1]);
      if (next.F < 0 || next.S < 0 || next.F >= h || next.S >= w)
        continue;
      if (grid[next.F][next.S] == '#')
        continue;
      if (magic[next.F][next.S] > m) {
        magic[next.F][next.S] = m;
        q.push(mp(m, next));
      }
    }
    for (int nx = node.F - 2; nx <= node.F + 2; nx++) {
      for (int ny = node.S - 2; ny <= node.S + 2; ny++) {
        if (nx < 0 || ny < 0 || nx >= h || ny >= w)
          continue;
        if (grid[nx][ny] == '#')
          continue;

        if (magic[nx][ny] > m + 1) {
          magic[nx][ny] = m + 1;
          q.push(mp(m + 1, mp(nx, ny)));
        }
      }
    }
  }
  return magic[dh][dw];
}
void solve() {
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;
  grid.clear();
  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    grid.eb(s);
  }

  int ans = bfs();
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
}
int main() {
  KAMEHAMEHA
  int t = 1;
  // cin>>t;
  for (int cn = 1; cn <= t; cn++) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

// Author - Soumik Roy
// Date & Time - August 24, 2020 9:59 PM
// Problem name - D - Wizard in Maze
// Problem url - https://atcoder.jp/contests/abc176/tasks/abc176_d
// Time limit - 2000 ms
// Memory Limit - 1024 MB

#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define MOD 1000000007
#define vi vector<int>
#define vll vector<ll>
#define pll pair<ll, ll>
#define pii pair<int, int>
#define all(p) p.begin(), p.end()
#define mid(s, e) (s + (e - s) / 2)
#define eb emplace_back
#define ull unsigned long long
#define bug(x) cout << "  [ " #x << " = " << x << " ]" << endl;
#define KAMEHAMEHA ios_base::sync_with_stdio(0);
#define RASENGAN                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
const int INF = 1e9;
int h, w, ch, cw, dh, dw;
int dir[] = {0, 1, 0, -1, 1, 0, -1, 0};
vector<string> grid;
int bfs() {
  int magic[h + 1][w + 1];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      magic[i][j] = INF;
  }
  queue<pair<int, int>> q;
  q.push(mp(ch, cw));
  magic[ch][cw] = 0;
  while (!q.empty()) {
    pii node = q.front();
    int m = magic[node.F][node.S];
    q.pop();
    for (int i = 0; i < 8; i += 2) {
      pii next = mp(node.F + dir[i], node.S + dir[i + 1]);
      if (next.F < 0 || next.S < 0 || next.F >= h || next.S >= w)
        continue;
      if (grid[next.F][next.S] == '#')
        continue;
      if (magic[next.F][next.S] > m) {
        magic[next.F][next.S] = m;
        q.push(next);
      }
    }
    for (int nx = node.F - 2; nx <= node.F + 2; nx++) {
      for (int ny = node.S - 2; ny <= node.S + 2; ny++) {
        if (nx < 0 || ny < 0 || nx >= h || ny >= w)
          continue;
        if (grid[nx][ny] == '#')
          continue;

        if (magic[nx][ny] > m + 1) {
          magic[nx][ny] = m + 1;
          q.push(mp(nx, ny));
        }
      }
    }
  }
  return magic[dh][dw];
}
void solve() {
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;
  grid.clear();
  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    grid.eb(s);
  }

  int ans = bfs();
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
}
int main() {
  KAMEHAMEHA
  int t = 1;
  // cin>>t;
  for (int cn = 1; cn <= t; cn++) {
    solve();
  }
  return 0;
}