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

#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define mp make_pair
#define ll long long
#define pb push_back

void solve() {

  int h, w;
  cin >> h >> w;

  int a, b;
  int c, d;

  cin >> a >> b;

  cin >> c >> d;

  a--;
  c--;
  b--;
  d--;

  int i, j;

  vector<string> v(h);

  for (i = 0; i < h; i++) {
    cin >> v[i];
  }

  deque<pair<int, int>> q;

  q.push_front(mp(a, b));

  vector<int> temp(w, 0);
  vector<int> temp2(w, 100000000);
  vector<vector<int>> visited(h, temp);
  vector<vector<int>> dist(h, temp2);
  dist[a][b] = 0;

  visited[a][b] = 1;

  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop_front();

    int x = p.first;
    int y = p.second;
    int level = dist[x][y];
    visited[x][y] = 1;
    // cout<<x<<" "<<y<<" "<<dist[x][y]<<endl;

    for (i = -2; i <= 2; i++) {
      for (j = -2; j <= 2; j++) {
        if (i == 0 && j == 0)
          continue;

        int x1 = x + i;
        int y1 = y + j;

        if (x1 >= 0 && x1 < h && y1 >= 0 && y1 < w) {
          if (v[x1][y1] == '.') {
            int distance = abs(x1 - x) + abs(y1 - y);
            int val;
            if (distance == 1) {
              val = 0;
            } else
              val = 1;
            if (dist[x1][y1] > level + val) {
              if (val == 1)
                q.push_back(mp(x1, y1));
              else
                q.push_front(mp(x1, y1));
              dist[x1][y1] = level + val;
            }
          }
        }
      }
    }
  }

  if (visited[c][d] == 0) {
    cout << -1;
  } else
    cout << dist[c][d];
}

int main() {

  int t = 1;
  // cin>>t;

  while (t--)
    solve();

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define mp make_pair
#define ll long long
#define pb push_back

void solve() {

  int h, w;
  cin >> h >> w;

  int a, b;
  int c, d;

  cin >> a >> b;

  cin >> c >> d;

  a--;
  c--;
  b--;
  d--;

  int i, j;

  vector<string> v(h);

  for (i = 0; i < h; i++) {
    cin >> v[i];
  }

  queue<pair<int, int>> q;

  q.push(mp(a, b));

  vector<int> temp(w, 0);
  vector<int> temp2(w, 100000000);
  vector<vector<int>> visited(h, temp);
  vector<vector<int>> dist(h, temp2);
  dist[a][b] = 0;

  visited[a][b] = 1;

  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();

    int x = p.first;
    int y = p.second;
    int level = dist[x][y];
    visited[x][y] = 1;
    // cout<<x<<" "<<y<<" "<<dist[x][y]<<endl;

    for (i = -2; i <= 2; i++) {
      for (j = -2; j <= 2; j++) {
        if (i == 0 && j == 0)
          continue;

        int x1 = x + i;
        int y1 = y + j;

        if (x1 >= 0 && x1 < h && y1 >= 0 && y1 < w) {
          if (v[x1][y1] == '.') {
            int distance = abs(x1 - x) + abs(y1 - y);
            int val;
            if (distance == 1) {
              val = 0;
            } else
              val = 1;
            if (dist[x1][y1] > level + val) {
              q.push(mp(x1, y1));
              dist[x1][y1] = level + val;
            }
          }
        }
      }
    }
  }

  if (visited[c][d] == 0) {
    cout << -1;
  } else
    cout << dist[c][d];
}

int main() {

  int t = 1;
  // cin>>t;

  while (t--)
    solve();

  return 0;
}