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

#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define FORR(i, m, n) for (ll i = m; i >= n; i--)
#define REPO(i, n) for (ll i = 1; i <= n; i++)
#define ll long long
#define INF (ll)1 << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(), n.end()
#define MOD 1000000007
#define P pair<ll, ll>

ll h, w, l, sx, sy, gx, gy;
string t[2100000];
ll v1[4] = {0, 0, 1, -1}, v2[4] = {1, -1, 0, 0};

#define PP pair<ll, P>
int main() {
  cin >> h >> w >> l;
  vector<vector<ll>> s(h, vector<ll>(w, INF));
  vector<vector<ll>> used1(h, vector<ll>(w, INF));
  vector<vector<ll>> used2(h, vector<ll>(w, INF));
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  REP(i, h) { cin >> t[i]; }
  priority_queue<PP, vector<PP>, greater<PP>> q;
  s[sx][sy] = 0;
  q.push(PP(0, P(sx, sy)));
  while (!q.empty()) {
    PP now = q.top();
    q.pop();
    ll a = now.second.first, b = now.second.second;
    if (s[a][b] < now.first)
      continue;
    // cout << a << " ! " << b << endl;
    REP(nxt, 4) {
      if (nxt <= 1)
        used1[a][b] = min(used1[a][b], s[a][b] + 1);
      if (nxt >= 2)
        used2[a][b] = min(used2[a][b], s[a][b] + 1);
      REPO(j, l) {
        // cout << nxt << " " << j << endl;
        ll x = a + v1[nxt] * j, y = b + v2[nxt] * j;
        if (0 > x or x >= h or 0 > y or y >= w)
          break;
        if (t[x][y] == '@')
          break;
        // cout << nxt << " !! " << j << endl;
        if (nxt <= 1 and used1[x][y] < s[a][b] + 1)
          break;
        if (nxt >= 2 and used2[x][y] < s[a][b] + 1)
          break;
        if (nxt <= 1)
          used1[x][y] = min(used1[x][y], s[a][b] + 1);
        if (nxt >= 2)
          used2[x][y] = min(used2[x][y], s[a][b] + 1);
        if (s[x][y] > s[a][b] + 1) {
          s[x][y] = s[a][b] + 1;
          q.push(PP(s[x][y], P(x, y)));
        }
      }
    }
  }
  /*
  REP(i, h){
      REP(j, w){
          cout << (s[i][j] >= INF ? -1 : s[i][j]) << " ";

      }
      cout << endl;
  }
  */
  cout << (s[gx][gy] >= INF ? -1 : s[gx][gy]) << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define FORR(i, m, n) for (ll i = m; i >= n; i--)
#define REPO(i, n) for (ll i = 1; i <= n; i++)
#define ll long long
#define INF (ll)1 << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(), n.end()
#define MOD 1000000007
#define P pair<ll, ll>

ll h, w, l, sx, sy, gx, gy;
string t[1100000];
ll v1[4] = {0, 0, 1, -1}, v2[4] = {1, -1, 0, 0};

#define PP pair<ll, P>
int main() {
  cin >> h >> w >> l;
  vector<vector<ll>> s(h, vector<ll>(w, INF));
  vector<vector<bool>> used(h, vector<bool>(w));
  vector<vector<ll>> used1(h, vector<ll>(w, INF));
  vector<vector<ll>> used2(h, vector<ll>(w, INF));
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  REP(i, h) { cin >> t[i]; }
  priority_queue<PP> q;
  s[sx][sy] = 0;
  q.push(PP(0, P(sx, sy)));
  while (!q.empty()) {
    PP now = q.top();
    q.pop();
    ll a = now.second.first, b = now.second.second;
    if (s[a][b] < now.first)
      continue;
    // cout << a << " ! " << b << endl;
    REP(nxt, 4) {
      REP(j, l + 1) {
        // cout << nxt << " " << j << endl;
        ll x = now.second.first + v1[nxt] * j,
           y = now.second.second + v2[nxt] * j;
        if (0 > x or x >= h or 0 > y or y >= w)
          break;
        used[a][b] = true;
        if (t[x][y] == '@')
          break;
        // cout << nxt << " !! " << j << endl;
        if (nxt <= 1 and j >= 1 and used1[x][y] <= s[a][b] + 1)
          break;
        if (nxt >= 2 and j >= 1 and used2[x][y] <= s[a][b] + 1)
          break;
        if (nxt <= 1)
          used1[x][y] = min(used1[x][y], s[a][b] + 1);
        if (nxt >= 2)
          used2[x][y] = min(used2[x][y], s[a][b] + 1);
        if (s[x][y] > s[a][b] + 1) {
          s[x][y] = s[a][b] + 1;
          q.push(PP(s[x][y], P(x, y)));
        }
      }
    }
  }
  cout << (s[gx][gy] == INF ? -1 : s[gx][gy]) << endl;
}