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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
// #include<bits/stdc++.h>
#define int long long
#define for0(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i <= (n); i++)
#define mp make_pair
#define all(x) x.begin(), x.end()
#define puts(x) cout << x << "\\n"
#define endwith return
constexpr int mod = 1000000007;
using namespace std;
int input() {
  int t;
  cin >> t;
  return t;
}
string m[1234567];
int h, w, k, x, y, xg, yg;
set<pair<int, int>> s1;
map<pair<int, int>, int> m2;
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
               greater<pair<int, pair<int, int>>>>
    pq;
signed main() {
  cin >> h >> w >> k;
  cin >> x >> y >> xg >> yg;
  x--;
  y--;
  xg--;
  yg--;
  for0(i, h) for0(j, w) m2[mp(i, j)] = mod;
  for0(i, h) cin >> m[i];
  pq.push(mp(0, mp(x, y)));
  while (pq.size()) {
    pair<int, pair<int, int>> p1 = pq.top();
    pq.pop();
    if (p1.second == mp(xg, yg)) {
      puts(p1.first);
      return 0;
    }
    if (s1.find(p1.second) != s1.end())
      continue;
    s1.insert(p1.second);
    int c = p1.first, x1 = p1.second.first, y1 = p1.second.second;
    m2[p1.second] = c;
    for1(i, k) {
      if (x1 - i < 0)
        break;
      if (m[x1 - i][y1] == '@')
        break;
      if (m2[mp(x1 - i, y1)] < c + 1)
        break;
      pq.push(mp(c + 1, mp(x1 - i, y1)));
      m2[mp(x1 - i, y1)] = c + 1;
    }
    for1(i, k) {
      if (x1 + i >= h)
        break;
      if (m[x1 + i][y1] == '@')
        break;
      if (m2[mp(x1 + i, y1)] < c + 1)
        break;
      pq.push(mp(c + 1, mp(x1 + i, y1)));
      m2[mp(x1 + i, y1)] = c + 1;
    }
    for1(i, k) {
      if (y1 - i < 0)
        break;
      if (m[x1][y1 - i] == '@')
        break;
      if (m2[mp(x1, y1 - i)] < c + 1)
        break;
      pq.push(mp(c + 1, mp(x1, y1 - i)));
      m2[mp(x1, y1 - i)] = c + 1;
    }
    for1(i, k) {
      if (y1 + i >= w)
        break;
      if (m[x1][y1 + i] == '@')
        break;
      if (m2[mp(x1, y1 + i)] < c + 1)
        break;
      pq.push(mp(c + 1, mp(x1, y1 + i)));
      m2[mp(x1, y1 + i)] = c + 1;
    }
  }
  puts(-1);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
// #include<bits/stdc++.h>
#define int long long
#define for0(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i <= (n); i++)
#define mp make_pair
#define all(x) x.begin(), x.end()
#define puts(x) cout << x << "\\n"
#define endwith return
constexpr int mod = 1000000007;
using namespace std;
int input() {
  int t;
  cin >> t;
  return t;
}
string m[1234567];
int h, w, k, x, y, xg, yg;
set<pair<int, int>> s1;
map<pair<int, int>, int> m2;
priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
               greater<pair<int, pair<int, int>>>>
    pq;
signed main() {
  cin >> h >> w >> k;
  cin >> x >> y >> xg >> yg;
  x--;
  y--;
  xg--;
  yg--;
  for0(i, h) for0(j, w) m2[mp(i, j)] = mod;
  for0(i, h) cin >> m[i];
  pq.push(mp(0, mp(x, y)));
  while (pq.size()) {
    pair<int, pair<int, int>> p1 = pq.top();
    pq.pop();
    if (p1.second == mp(xg, yg)) {
      puts(p1.first);
      return 0;
    }
    if (s1.find(p1.second) != s1.end())
      continue;
    s1.insert(p1.second);
    int c = p1.first, x1 = p1.second.first, y1 = p1.second.second;
    m2[p1.second] = c;
    for1(i, k) {
      if (x1 - i < 0)
        break;
      if (m[x1 - i][y1] == '@')
        break;
      if (m2[mp(x1 - i, y1)] <= c + 1)
        break;
      pq.push(mp(c + 1, mp(x1 - i, y1)));
    }
    for1(i, k) {
      if (x1 + i >= h)
        break;
      if (m[x1 + i][y1] == '@')
        break;
      if (m2[mp(x1 + i, y1)] <= c + 1)
        break;
      pq.push(mp(c + 1, mp(x1 + i, y1)));
    }
    for1(i, k) {
      if (y1 - i < 0)
        break;
      if (m[x1][y1 - i] == '@')
        break;
      if (m2[mp(x1, y1 - i)] <= c + 1)
        break;
      pq.push(mp(c + 1, mp(x1, y1 - i)));
    }
    for1(i, k) {
      if (y1 + i >= w)
        break;
      if (m[x1][y1 + i] == '@')
        break;
      if (m2[mp(x1, y1 + i)] <= c + 1)
        break;
      pq.push(mp(c + 1, mp(x1, y1 + i)));
    }
  }
  puts(-1);
}