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

#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define ll long long

using namespace std;

vector<int> conv(int a) {
  if (a < 5) {
    return {-2, a - 2};
  }
  if (a > 14) {
    return {2, a - 17};
  }
  if (a < 10) {
    if (a == 7) {
      return {0, -2};
    }
    return {-1, a - 7};
  }
  if (a == 12) {
    return {0, 2};
  }
  return {1, a - 12};
}

int main(void) {
  int high, wide;
  cin >> high >> wide;
  int magihi, magiwi;
  int disthi, distwi;
  cin >> magihi >> magiwi;
  cin >> disthi >> distwi;
  vector<vector<int>> maze(high + 2, vector<int>(wide + 2, -1));
  for (int i = 0; i < high; i++) {
    string st;
    cin >> st;
    for (int j = 0; j < wide; j++) {
      if (st[j] == '.') {
        maze[i + 1][j + 1] = 100000000;
      } else {
        maze[i + 1][1 + j] = -1;
      }
    }
  }
  bool cont = true;
  int s = 0;
  int o = 0;
  maze[magihi][magiwi] = 0;
  vector<vector<int>> prev = {{magihi, magiwi}};
  vector<vector<int>> warprev = {{magihi, magiwi}};
  while (cont == true) {
    if (maze[disthi][distwi] != 100000000) {
      cout << maze[disthi][distwi] << endl;
      return 0;
    }
    if (prev.size() > s) {
      vector<int> serch = prev[s];
      s++;
      if (maze[serch[0] + 1][serch[1]] > maze[serch[0]][serch[1]]) {
        warprev.push_back({serch[0] + 1, serch[1]});
        prev.push_back({serch[0] + 1, serch[1]});
        maze[serch[0] + 1][serch[1]] = maze[serch[0]][serch[1]];
      }

      if (maze[serch[0] - 1][serch[1]] > maze[serch[0]][serch[1]]) {
        warprev.push_back({serch[0] - 1, serch[1]});
        prev.push_back({serch[0] - 1, serch[1]});
        maze[serch[0] - 1][serch[1]] = maze[serch[0]][serch[1]];
      }

      if (maze[serch[0]][serch[1] + 1] > maze[serch[0]][serch[1]]) {
        warprev.push_back({serch[0], serch[1] + 1});
        prev.push_back({serch[0], serch[1] + 1});
        maze[serch[0]][serch[1] + 1] = maze[serch[0]][serch[1]];
      }

      if (maze[serch[0]][serch[1] - 1] > maze[serch[0]][serch[1]]) {
        warprev.push_back({serch[0], serch[1] - 1});
        prev.push_back({serch[0], serch[1] - 1});
        maze[serch[0]][serch[1] - 1] = maze[serch[0]][serch[1]];
      }
    } else {
      vector<int> serch = warprev[o];
      o++;
      for (int j = 0; j < 20; j++) {
        vector<int> plus = conv(j);
        if (0 < serch[0] + plus[0] and serch[0] + plus[0] < high + 1 and
            serch[1] + plus[1] > 0 and serch[1] + plus[1] <= wide) {
          if (maze[serch[0] + plus[0]][serch[1] + plus[1]] >
              maze[serch[0]][serch[1]] + 1) {
            maze[serch[0] + plus[0]][serch[1] + plus[1]] =
                maze[serch[0]][serch[1]] + 1;
            warprev.push_back({serch[0] + plus[0], serch[1] + plus[1]});
            prev.push_back({serch[0] + plus[0], serch[1] + plus[1]});
          }
        }
      }
    }
    if (prev.size() <= s and warprev.size() <= o) {
      cont = false;
    }
  }

  int ans = maze[disthi][distwi];
  if (ans == 100000000) {
    ans = -1;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define ll long long

using namespace std;

vector<int> conv(int a) {
  if (a < 5) {
    return {-2, a - 2};
  }
  if (a > 14) {
    return {2, a - 17};
  }
  if (a < 10) {
    if (a == 7) {
      return {0, -2};
    }
    return {-1, a - 7};
  }
  if (a == 12) {
    return {0, 2};
  }
  return {1, a - 12};
}

int main(void) {
  int high, wide;
  cin >> high >> wide;
  int magihi, magiwi;
  int disthi, distwi;
  cin >> magihi >> magiwi;
  cin >> disthi >> distwi;
  vector<vector<int>> maze(high + 2, vector<int>(wide + 2, -1));
  for (int i = 0; i < high; i++) {
    string st;
    cin >> st;
    for (int j = 0; j < wide; j++) {
      if (st[j] == '.') {
        maze[i + 1][j + 1] = 100000000;
      } else {
        maze[i + 1][1 + j] = -1;
      }
    }
  }
  bool cont = true;
  maze[magihi][magiwi] = 0;
  vector<vector<int>> prev = {{magihi, magiwi}};
  vector<vector<int>> warprev = {{magihi, magiwi}};
  while (cont == true) {
    if (maze[disthi][distwi] != 100000000) {
      cout << maze[disthi][distwi] << endl;
      return 0;
    }
    if (prev.size() > 0) {
      vector<int> serch = prev[0];
      prev.erase(prev.begin());
      if (maze[serch[0] + 1][serch[1]] > maze[serch[0]][serch[1]]) {
        warprev.push_back({serch[0] + 1, serch[1]});
        prev.push_back({serch[0] + 1, serch[1]});
        maze[serch[0] + 1][serch[1]] = maze[serch[0]][serch[1]];
      }

      if (maze[serch[0] - 1][serch[1]] > maze[serch[0]][serch[1]]) {
        warprev.push_back({serch[0] - 1, serch[1]});
        prev.push_back({serch[0] - 1, serch[1]});
        maze[serch[0] - 1][serch[1]] = maze[serch[0]][serch[1]];
      }

      if (maze[serch[0]][serch[1] + 1] > maze[serch[0]][serch[1]]) {
        warprev.push_back({serch[0], serch[1] + 1});
        prev.push_back({serch[0], serch[1] + 1});
        maze[serch[0]][serch[1] + 1] = maze[serch[0]][serch[1]];
      }

      if (maze[serch[0]][serch[1] - 1] > maze[serch[0]][serch[1]]) {
        warprev.push_back({serch[0], serch[1] - 1});
        prev.push_back({serch[0], serch[1] - 1});
        maze[serch[0]][serch[1] - 1] = maze[serch[0]][serch[1]];
      }
    } else {
      vector<int> serch = warprev[0];
      warprev.erase(warprev.begin());
      for (int j = 0; j < 20; j++) {
        vector<int> plus = conv(j);
        if (0 < serch[0] + plus[0] and serch[0] + plus[0] < high + 1 and
            serch[1] + plus[1] > 0 and serch[1] + plus[1] <= wide) {
          if (maze[serch[0] + plus[0]][serch[1] + plus[1]] >
              maze[serch[0]][serch[1]] + 1) {
            maze[serch[0] + plus[0]][serch[1] + plus[1]] =
                maze[serch[0]][serch[1]] + 1;
            warprev.push_back({serch[0] + plus[0], serch[1] + plus[1]});
            prev.push_back({serch[0] + plus[0], serch[1] + plus[1]});
          }
        }
      }
    }
    if (prev.size() == 0 and warprev.size() == 0) {
      cont = false;
    }
  }

  int ans = maze[disthi][distwi];
  if (ans == 100000000) {
    ans = -1;
  }
  cout << ans << endl;
  return 0;
}