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

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>

using llint = long long int;

using state = std::array<llint, 3>;

std::vector<state> walk_all(const std::vector<std::vector<char>> &S,
                            const state &now,
                            std::vector<std::vector<int>> &step) {
  std::priority_queue<state> tmp;
  std::vector<state> ret;
  tmp.push(now);
  while (!tmp.empty()) {
    state now_ = tmp.top();
    tmp.pop();
    ret.push_back(now_);

    for (int x = -1; x <= 1; x += 2) {
      for (int y = 1; y <= 2; y++) {
        state next = {now_[0], now_[1], now_[2]};
        next[y] += x;
        if (next[1] < 0)
          continue;
        if (next[2] < 0)
          continue;
        if (next[1] >= S.size())
          continue;
        if (next[2] >= S[0].size())
          continue;
        if (S[next[1]][next[2]] == '#')
          continue;
        if (step[next[1]][next[2]] != -1 && step[next[1]][next[2]] <= -next[0])
          continue;

        step[next[1]][next[2]] = -next[0];
        tmp.push(next);
      }
    }
  }

  return ret;
}
std::vector<state> warp_all(const std::vector<std::vector<char>> &S,
                            const std::vector<state> &walked,
                            std::vector<std::vector<int>> &step) {
  std::vector<state> ret;

  for (const auto &now : walked) {
    for (int x = -2; x <= 2; x += 1) {
      for (int y = -2; y <= 2; y += 1) {
        state next = {now[0] - 1, now[1] + x, now[2] + y};
        if (next[1] < 0)
          continue;
        if (next[2] < 0)
          continue;
        if (next[1] >= S.size())
          continue;
        if (next[2] >= S[0].size())
          continue;
        if (S[next[1]][next[2]] == '#')
          continue;
        if (step[next[1]][next[2]] != -1 && step[next[1]][next[2]] <= -next[0])
          continue;

        step[next[1]][next[2]] = -next[0];
        ret.push_back(next);
      }
    }
  }

  return ret;
}

void solve(void) {

  int h, w, ch, cw, dh, dw;
  std::cin >> h >> w >> ch >> cw >> dh >> dw;

  std::vector<std::vector<char>> S(h + 2, std::vector<char>(w + 2, '#'));
  std::vector<std::vector<int>> step(h + 2, std::vector<int>(w + 2, -1));

  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      std::cin >> S[i][j];
    }
  }

  std::priority_queue<state> pq;
  pq.push({-0, ch, cw});
  step[ch][cw] = 0;
  bool update = false;
  int checked_cnt = 0;
  while (!pq.empty()) {
    // std::cout << ++checked_cnt << std::endl;
    auto now = pq.top();
    pq.pop();
    // std::cout << now[0] << " " << now[1] << " " << now[2] << std::endl;
    std::vector<state> walked_list = walk_all(S, now, step);
    std::vector<state> warped_list = warp_all(S, walked_list, step);
    for (const auto &elem : warped_list) {
      pq.push(elem);
    }
  }
  // for(int i=0; i<step.size(); i++){
  //   for(int j=0; j<step[0].size(); j++){
  //     std::cout << step[i][j] << " ";
  //   }
  //   std::cout << std::endl;
  // }

  std::cout << step[dh][dw] << std::endl;

  return;
}
int main(void) {
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>

using llint = long long int;

using state = std::array<llint, 3>;

std::vector<state> walk_all(const std::vector<std::vector<char>> &S,
                            const state &now,
                            std::vector<std::vector<int>> &step) {
  std::priority_queue<state> tmp;
  std::vector<state> ret;
  tmp.push(now);
  while (!tmp.empty()) {
    state now_ = tmp.top();
    tmp.pop();
    ret.push_back(now_);

    for (int x = -1; x <= 1; x += 2) {
      for (int y = 1; y <= 2; y++) {
        state next = {now_[0], now_[1], now_[2]};
        next[y] += x;
        if (next[1] < 0)
          continue;
        if (next[2] < 0)
          continue;
        if (next[1] >= S.size())
          continue;
        if (next[2] >= S[0].size())
          continue;
        if (S[next[1]][next[2]] == '#')
          continue;
        if (step[next[1]][next[2]] != -1 && step[next[1]][next[2]] <= next[0])
          continue;

        step[next[1]][next[2]] = next[0];
        tmp.push(next);
      }
    }
  }

  return ret;
}
std::vector<state> warp_all(const std::vector<std::vector<char>> &S,
                            const std::vector<state> &walked,
                            std::vector<std::vector<int>> &step) {
  std::vector<state> ret;

  for (const auto &now : walked) {
    for (int x = -2; x <= 2; x += 1) {
      for (int y = -2; y <= 2; y += 1) {
        state next = {now[0] + 1, now[1] + x, now[2] + y};
        if (next[1] < 0)
          continue;
        if (next[2] < 0)
          continue;
        if (next[1] >= S.size())
          continue;
        if (next[2] >= S[0].size())
          continue;
        if (S[next[1]][next[2]] == '#')
          continue;
        if (step[next[1]][next[2]] != -1 && step[next[1]][next[2]] <= next[0])
          continue;

        step[next[1]][next[2]] = next[0];
        ret.push_back(next);
      }
    }
  }

  return ret;
}

void solve(void) {

  int h, w, ch, cw, dh, dw;
  std::cin >> h >> w >> ch >> cw >> dh >> dw;

  std::vector<std::vector<char>> S(h + 2, std::vector<char>(w + 2, '#'));
  std::vector<std::vector<int>> step(h + 2, std::vector<int>(w + 2, -1));

  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      std::cin >> S[i][j];
    }
  }

  std::priority_queue<state> pq;
  pq.push({-0, ch, cw});
  step[ch][cw] = 0;
  bool update = false;
  while (!pq.empty()) {
    auto now = pq.top();
    pq.pop();
    std::vector<state> walked_list = walk_all(S, now, step);
    std::vector<state> warped_list = warp_all(S, walked_list, step);
    for (const auto &elem : warped_list) {
      pq.push(elem);
    }
  }
  // for(int i=0; i<step.size(); i++){
  //   for(int j=0; j<step[0].size(); j++){
  //     std::cout << step[i][j] << " ";
  //   }
  //   std::cout << std::endl;
  // }

  std::cout << step[dh][dw] << std::endl;

  return;
}
int main(void) {
  solve();
  return 0;
}