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

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <deque>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define ll long long
#define rep2(i, a, b) for (int i = a; i <= b; ++i)
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep3(i, a, b) for (int i = a; i >= b; i--)
#define REP(e, v) for (auto e : v)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define mt make_tuple
#define pq priority_queue<int>
#define pqg priority_queue<int, vector<int>, greater<int>>
#define pb push_back
#define vec vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define vecpll vector<pll>
#define vecbl vector<bool>
#define endl "\\n"
#define ALL(c) (c).begin(), (c).end()
using namespace std;
int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}
string stin() {
  string s;
  cin >> s;
  return s;
}
int main() {
  int h = in(), w = in(), k = in(), int_max = 2000000;
  int sx = in() - 1, sy = in() - 1, tx = in() - 1, ty = in() - 1;
  vector<vec> a(h, vec(w)), cnt(h, vec(w, int_max));
  cnt[sx][sy] = 0;
  rep(i, h) {
    string s = stin();
    rep(j, w) {
      if (s[j] == '.')
        a[i][j] = 0;
      else
        a[i][j] = 1;
    }
  }
  priority_queue<pair<int, pii>, vector<pair<int, pii>>,
                 greater<pair<int, pii>>>
      que;
  que.push(mp(0, mp(sx, sy)));
  while (!que.empty()) {
    int cost = que.top().first;
    int x = que.top().second.first;
    int y = que.top().second.second;
    que.pop();
    if (x == tx && y == ty) {
      cout << cost << endl;
      return 0;
    }
    int xx = x - 1;
    int yy = y - 1;
    while (xx >= 0 && a[xx][y] == 0 && xx >= x - k) {
      if (cnt[xx][y] == int_max) {
        cnt[xx][y] = cost + 1;
        que.push(mp(cost + 1, mp(xx, y)));
      } else if (cnt[xx][y] <= cost)
        break;
      xx--;
    }
    while (yy >= 0 && a[x][yy] == 0 && yy >= y - k) {
      if (cnt[x][yy] == int_max) {
        cnt[x][yy] = cost + 1;
        que.push(mp(cost + 1, mp(x, yy)));
      } else if (cnt[x][yy] <= cost)
        break;
      yy--;
    }
    xx = x + 1;
    yy = y + 1;
    while (xx < h && a[xx][y] == 0 && xx <= x + k) {
      if (cnt[xx][y] == int_max) {
        cnt[xx][y] = cost + 1;
        que.push(mp(cost + 1, mp(xx, y)));
      } else if (cnt[xx][y] <= cost)
        break;
      xx++;
    }
    while (yy < w && a[x][yy] == 0 && yy <= y + k) {
      if (cnt[x][yy] == int_max) {
        cnt[x][yy] = cost + 1;
        que.push(mp(cost + 1, mp(x, yy)));
      } else if (cnt[x][yy] <= cost)
        break;
      yy++;
    }
  }
  cout << -1 << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <deque>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define ll long long
#define rep2(i, a, b) for (int i = a; i <= b; ++i)
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep3(i, a, b) for (int i = a; i >= b; i--)
#define REP(e, v) for (auto e : v)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define mt make_tuple
#define pq priority_queue<int>
#define pqg priority_queue<int, vector<int>, greater<int>>
#define pb push_back
#define vec vector<int>
#define vecll vector<ll>
#define vecpii vector<pii>
#define vecpll vector<pll>
#define vecbl vector<bool>
#define endl "\\n"
#define ALL(c) (c).begin(), (c).end()
using namespace std;
int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}
string stin() {
  string s;
  cin >> s;
  return s;
}
int main() {
  int h = in(), w = in(), k = in();
  int sx = in() - 1, sy = in() - 1, tx = in() - 1, ty = in() - 1;
  vector<vec> a(h, vec(w));
  vector<vector<bool>> done(h, vector<bool>(w, false));
  done[sx][sy] = true;
  rep(i, h) {
    string s = stin();
    rep(j, w) {
      if (s[j] == '.')
        a[i][j] = 0;
      else {
        a[i][j] = 1;
        done[i][j] = true;
      }
    }
  }
  priority_queue<pair<int, pii>, vector<pair<int, pii>>,
                 greater<pair<int, pii>>>
      que;
  que.push(mp(0, mp(sx, sy)));
  while (!que.empty()) {

    int cost = que.top().first;
    int x = que.top().second.first;
    int y = que.top().second.second;
    que.pop();
    if (x == tx && y == ty) {
      cout << cost << endl;
      return 0;
    }
    int xx = x - 1;
    int yy = y - 1;
    while (xx >= 0 && a[xx][y] == 0 && xx >= x - k) {
      if (!done[xx][y]) {
        done[xx][y] = true;
        que.push(mp(cost + 1, mp(xx, y)));
      }
      xx--;
    }
    while (yy >= 0 && a[x][yy] == 0 && yy >= y - k) {
      if (!done[x][yy]) {
        done[x][yy] = true;
        que.push(mp(cost + 1, mp(x, yy)));
      }
      yy--;
    }
    xx = x + 1;
    yy = y + 1;
    while (xx < h && a[xx][y] == 0 && xx <= x + k) {
      if (!done[xx][y]) {
        done[xx][y] = true;
        que.push(mp(cost + 1, mp(xx, y)));
      }
      xx++;
    }
    while (yy < w && a[x][yy] == 0 && yy <= y + k) {
      if (!done[x][yy]) {
        done[x][yy] = true;
        que.push(mp(cost + 1, mp(x, yy)));
      }
      yy++;
    }
  }
  cout << -1 << endl;
}