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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define ll long long
#define vecll vector<long long>
#define vec2ll vector<vector<long long>>
#define vecchar vector<char>
#define vecstr vector<string>
#define vec2str vector<vector<string>>
#define vecbool vector<bool>
#define vec2bool vector<vector<bool>>
#define pairll pair<ll, ll>
#define vecpairll vector<pair<long long, long long>>
#define forll(s, a, b) for (long long s = a; s < b; s++)
#define forllde(s, a) for (long long s = a; s > 0; s--)

#define Pi 3.1415926535897932384626

bool isInFirld(int col, int row, const pairll &point) {
  const int c = point.second;
  const int r = point.first;
  return (0 <= c && c < col) && (0 <= r && r < row);
}

int main() {
  ll h, w, ans;
  bool flag = false;

  cin >> h >> w;

  pairll c, d;

  cin >> c.first >> c.second;
  cin >> d.first >> d.second;

  c.first--;
  c.second--;
  d.first--;
  d.second--;

  vecstr map(h);

  forll(i, 0, h) { cin >> map[i]; }

  pair<pairll, bool> start = {c, false}, goal = {d, false};

  vec2ll memo;
  forll(i, 0, map.size()) {
    vecll v(map[i].size(), -1);
    memo.push_back(v);
  }

  const pairll points[4] = {
      {-1, 0},
      {1, 0},
      {0, -1},
      {0, 1},
  };

  queue<pair<pairll, bool>> data;
  data.push(start);

  queue<pair<pairll, bool>> warp;

  memo[c.first][c.second] = 0;

  while (!data.empty() || !warp.empty()) {
    flag = false;

    if (data.empty() && !warp.empty()) {
      while (!warp.empty()) {
        pair<pairll, bool> cur = warp.front();
        warp.pop();

        forll(i, -2 - min((ll)0, cur.first.first - 2),
              3 - max((ll)0, cur.first.first + 2 - (h - 1))) {
          forll(j, -2 - min((ll)0, cur.first.second - 2),
                3 - max((ll)0, cur.first.second + 2 - (w - 1))) {
            if (i == 0 && j == 0)
              continue;

            pairll next = {(cur.first.first + i), (cur.first.second + j)};

            if (isInFirld(map[0].size(), map.size(), next)) {
              const char s = map[next.first][next.second];

              if (s == '.') {
                if (memo[next.first][next.second] == -1 ||
                    memo[next.first][next.second] >
                        memo[cur.first.first][cur.first.second] + 1) {

                  data.push({next, false});

                  memo[next.first][next.second] =
                      memo[cur.first.first][cur.first.second] + 1;
                }
              }
            }
          }
        }
      }
    } else {
      pair<pairll, bool> cur = data.front();
      data.pop();

      for (const auto &point : points) {
        pairll next = {(cur.first.first + point.first),
                       (cur.first.second + point.second)};

        if (isInFirld(map[0].size(), map.size(), next)) {

          const char s = map[next.first][next.second];

          if (s == '.') {
            if (memo[next.first][next.second] == -1 ||
                memo[next.first][next.second] >
                    memo[cur.first.first][cur.first.second]) {

              data.push({next, false});

              memo[next.first][next.second] =
                  memo[cur.first.first][cur.first.second];
            }
          } else if (s == '#' && !flag) {
            flag = true;
            cur.second = true;

            warp.push(cur);
          }
        }
      }
    }
  }

  ans = memo[d.first][d.second];

  cout << ans;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define ll long long
#define vecll vector<long long>
#define vec2ll vector<vector<long long>>
#define vecchar vector<char>
#define vecstr vector<string>
#define vec2str vector<vector<string>>
#define vecbool vector<bool>
#define vec2bool vector<vector<bool>>
#define pairll pair<ll, ll>
#define vecpairll vector<pair<long long, long long>>
#define forll(s, a, b) for (long long s = a; s < b; s++)
#define forllde(s, a) for (long long s = a; s > 0; s--)

#define Pi 3.1415926535897932384626

bool isInFirld(int col, int row, const pairll &point) {
  const int c = point.second;
  const int r = point.first;
  return (0 <= c && c < col) && (0 <= r && r < row);
}

int main() {
  ll h, w, ans;
  bool flag = false;

  cin >> h >> w;

  pairll c, d;

  cin >> c.first >> c.second;
  cin >> d.first >> d.second;

  c.first--;
  c.second--;
  d.first--;
  d.second--;

  vecstr map(h);

  forll(i, 0, h) { cin >> map[i]; }

  pairll start = c, goal = d;

  vec2ll memo;
  forll(i, 0, map.size()) {
    vecll v(map[i].size(), -1);
    memo.push_back(v);
  }

  const pairll points[4] = {
      {-1, 0},
      {1, 0},
      {0, -1},
      {0, 1},
  };

  queue<pairll> data;
  data.push(start);

  memo[c.first][c.second] = 0;

  while (!data.empty()) {
    flag = false;

    pairll cur = data.front();
    data.pop();

    for (const auto &point : points) {
      pairll next = {(cur.first + point.first), (cur.second + point.second)};

      if (isInFirld(map[0].size(), map.size(), next)) {

        char s = map[next.first][next.second];

        if (s == '.') {
          if (memo[next.first][next.second] == -1 ||
              memo[next.first][next.second] > memo[cur.first][cur.second]) {

            data.push(next);

            memo[next.first][next.second] = memo[cur.first][cur.second];
          }
        } else if (s == '#' && !flag) {
          flag = true;
          forll(i, -2 - min((ll)0, cur.first - 2),
                3 - max((ll)0, cur.first + 2 - (h - 1))) {
            forll(j, -2 - min((ll)0, cur.second - 2),
                  3 - max((ll)0, cur.second + 2 - (w - 1))) {
              if (i == 0 && j == 0)
                continue;

              next = {(cur.first + i), (cur.second + j)};

              if (isInFirld(map[0].size(), map.size(), next)) {
                s = map[next.first][next.second];

                if (s == '.') {
                  if (memo[next.first][next.second] == -1 ||
                      memo[next.first][next.second] >
                          memo[cur.first][cur.second] + 1) {

                    data.push(next);

                    memo[next.first][next.second] =
                        memo[cur.first][cur.second] + 1;
                  }
                }
              }
            }
          }
        }
      }
    }
  }

  ans = memo[d.first][d.second];

  cout << ans;
}