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

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define ll long long

using namespace std;

long long MOD = 1000000007;

struct info {
  int cost, series, y, x, prev;
};

bool operator<(const info &info1, const info &info2) {
  if (info1.cost < info2.cost) {
    return true;
  } else if (info1.cost > info2.cost) {
    return false;
  } else if (info1.series < info2.series) {
    return true;
  } else {
    return false;
  }
}
bool operator>(const info &info1, const info &info2) {
  if (info1.cost > info2.cost) {
    return true;
  } else if (info1.cost < info2.cost) {
    return false;
  } else if (info1.series > info2.series) {
    return true;
  } else {
    return false;
  }
}

struct proceed {
  int y, x, c;
};

template <typename T> void cout_vec(vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    if (i != 0) {
      cout << ' ';
    }
    cout << vec[i];
  }
  cout << endl;
}

template <typename T> void cout_mat(vector<vector<T>> &mat) {
  for (int i = 0; i < mat.size(); i++) {
    vector<T> vec = mat[i];
    cout_vec(mat[i]);
  }
}

int H, W, K;
vector<proceed> move_ = {{1, 0, 0}, {-1, 0, 1}, {0, 1, 2}, {0, -1, 3}};
int bfs(int sx, int sy, vector<vector<char>> &grid) {
  priority_queue<info, vector<info>, greater<info>> pque;
  pque.push({0, 0, sy, sx, 4});
  vector<vector<vector<int>>> dist(
      H + 2, vector<vector<int>>(W + 2, vector<int>(5, MOD)));

  while ((int)pque.size() > 0) {
    info now = pque.top();
    pque.pop();

    // cout << now.y << ' ' << now.x << ' ' << now.prev;
    if (grid[now.y][now.x] == 'g') {
      // cout_mat(dist);
      return now.cost;
    } else if (dist[now.y][now.x][now.prev] <= now.cost) {
      continue;
    }
    dist[now.y][now.x][now.prev] = now.cost;

    for (const auto &p : move_) {
      if (grid[now.y + p.y][now.x + p.x] != '@') {
        if (p.c == now.prev && now.series < K) {
          pque.push({now.cost, now.series + 1, now.y + p.y, now.x + p.x, p.c});
        } else {
          pque.push({now.cost + 1, 1, now.y + p.y, now.x + p.x, p.c});
        }
      }
    }
  }

  // cout_mat(grid);
  // cout_mat(dist);
  return -1;
}

int main() {
  cin >> H >> W >> K;

  int sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;

  vector<vector<char>> grid(H + 2, vector<char>(W + 2, '@'));
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      char c;
      cin >> c;
      grid[i][j] = c;
    }
  }
  grid[gy][gx] = 'g';

  cout << bfs(sx, sy, grid) << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define ll long long

using namespace std;

long long MOD = 1000000007;

struct info {
  int cost, series, y, x, prev;
};

bool operator<(const info &info1, const info &info2) {
  return info1.cost < info2.cost;
}
bool operator>(const info &info1, const info &info2) {
  return info1.cost > info2.cost;
}

struct proceed {
  int y, x, c;
};

template <typename T> void cout_vec(vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    if (i != 0) {
      cout << ' ';
    }
    cout << vec[i];
  }
  cout << endl;
}

template <typename T> void cout_mat(vector<vector<T>> &mat) {
  for (int i = 0; i < mat.size(); i++) {
    vector<T> vec = mat[i];
    cout_vec(mat[i]);
  }
}

int H, W, K;
vector<proceed> move_ = {{1, 0, 0}, {-1, 0, 1}, {0, 1, 2}, {0, -1, 3}};
int bfs(int sx, int sy, vector<vector<char>> &grid) {
  priority_queue<info, vector<info>, greater<info>> pque;
  pque.push({0, 0, sy, sx, 4});
  vector<vector<vector<int>>> dist(
      H + 2, vector<vector<int>>(W + 2, vector<int>(5, MOD)));

  while ((int)pque.size() > 0) {
    info now = pque.top();
    pque.pop();

    // cout << now.y << ' ' << now.x << ' ' << now.prev;
    if (grid[now.y][now.x] == 'g') {
      // cout_mat(dist);
      return now.cost;
    } else if (dist[now.y][now.x][now.prev] < now.cost) {
      continue;
    }
    dist[now.y][now.x][now.prev] = now.cost;

    for (const auto &p : move_) {
      if (grid[now.y + p.y][now.x + p.x] != '@') {
        if (p.c == now.prev && now.series < K) {
          pque.push({now.cost, now.series + 1, now.y + p.y, now.x + p.x, p.c});
        } else {
          pque.push({now.cost + 1, 1, now.y + p.y, now.x + p.x, p.c});
        }
      }
    }
  }

  // cout_mat(grid);
  // cout_mat(dist);
  return -1;
}

int main() {
  cin >> H >> W >> K;

  int sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;

  vector<vector<char>> grid(H + 2, vector<char>(W + 2, '@'));
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      char c;
      cin >> c;
      grid[i][j] = c;
    }
  }
  grid[gy][gx] = 'g';

  cout << bfs(sx, sy, grid) << endl;
}