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

/*                         _
                        _ooOoo_
                       o8888888o
                       88" . "88
                       (| -_- |)
                  .'  \\\\|     |//  `.
                 /  \\\\|||  :  |||//  \\
                /  _||||| -:- |||||_  \\
                |   | \\\\\\  -  /'| |   |
                | \\_|  `\\`---'//  |_/ |
                \\  .-\\__ `-. -'__/-.  /
              ___`. .'  /--.--\\  `. .'___
           ."" '<  `.___\\_<|>_/___.' _> \\"".
          | | :  `- \\`. ;`. _/; .'/ /  .' ; |
          \\  \\ `-.   \\_\\_`. _.'_/_/  -' _.' /
===========`-.`___`-.__\\ \\___  /__.-'_.'_.-'================

                  Please give me AC.
*/
#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define INF (~(1 << 31))
#define eps 1e-6
#define PI 3.141592653589793238462643383
#define lowbit(x) ((x) & (-x))
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define all(v) (v).begin(), (v).end()
#define dbg(x, y) cout << x << " = " << y << endl;
#define fi first
#define se second
#define ls rt << 1
#define rs rt << 1 | 1
#define ll long long
#define int ll
typedef bitset<40000> bt;
typedef pair<int, int> pii;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  if (c == '-')
    f = -1, c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return f * x;
}

const int N = 1e6 + 7;
const int M = 2e6 + 7;
const ll MAXN = 4e18 + 7;
const ll Mod = 998244353;

int _, i, j, k, n, m, p, s, T, l, r, o, u, v, w, x, y, z, st, en, sx, sy, tx,
    ty, ans, len, th, ma, mi, mod, sum, num, res, flag, cas, ret, mid, cnt, now,
    nex, tmp;
int a[N], c[N], mp[N][4], d[] = {-1, 1, -1, 1};
char ch;
queue<int> q;
vector<int> g, h;
string s1, s2, s3;

void bfs() {
  queue<pii> q;
  for (int i = 0; i < 4; i++)
    mp[(sx - 1) * m + sy][i] = 0, q.push(pii((sx - 1) * m + sy, i));
  // dbg(sx,sy)
  while (!q.empty()) {
    u = q.front().fi, th = q.front().se;
    q.pop();
    ans = mp[u][th];
    res = ans / k * k + (ans % k ? k : 0) + 1;
    y = (u - 1) % m + 1;
    x = (u - y) / m + 1;
    for (int i = 0; i < 2; i++) {
      v = u + d[i];
      if (y + d[i] < 1 || y + d[i] > m || !a[v])
        continue;
      if (i == th) {
        if (ans + 1 < mp[v][i])
          mp[v][i] = ans + 1, q.push(pii(v, i));
      } else if (mp[v][i] > res)
        mp[v][i] = res, q.push(pii(v, i));
    }
    for (int i = 2; i < 4; i++) {
      v = u + d[i] * m;
      if (x + d[i] < 1 || x + d[i] > n || !a[v])
        continue;
      if (i == th) {
        if (ans + 1 < mp[v][i])
          mp[v][i] = ans + 1, q.push(pii(v, i));
      } else if (mp[v][i] > res)
        mp[v][i] = res, q.push(pii(v, i));
    }
  }
}

signed main() {
  ios::sync_with_stdio(false);
  int T = 1;
  double x1, x2, x3, y1, y2, y3, z1, z2, z3;
  // cin >> T;
  while (T--) {
    cin >> n >> m >> k;
    cin >> sx >> sy >> tx >> ty;
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= m; j++) {
        cin >> ch;
        if (ch == '.')
          a[(i - 1) * m + j] = 1;
        else
          a[(i - 1) * m + j] = 0;
        for (l = 0; l < 4; l++)
          mp[(i - 1) * m + j][l] = MAXN;
      }
    }
    bfs();

    ans = MAXN;
    for (int i = 0; i < 4; i++)
      ans = min(ans, mp[(tx - 1) * m + ty][i]);
    if (ans == MAXN)
      cout << -1 << endl;
    else
      cout << (ans / k * k + (ans % k ? k : 0)) / k << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

/*                         _
                        _ooOoo_
                       o8888888o
                       88" . "88
                       (| -_- |)
                  .'  \\\\|     |//  `.
                 /  \\\\|||  :  |||//  \\
                /  _||||| -:- |||||_  \\
                |   | \\\\\\  -  /'| |   |
                | \\_|  `\\`---'//  |_/ |
                \\  .-\\__ `-. -'__/-.  /
              ___`. .'  /--.--\\  `. .'___
           ."" '<  `.___\\_<|>_/___.' _> \\"".
          | | :  `- \\`. ;`. _/; .'/ /  .' ; |
          \\  \\ `-.   \\_\\_`. _.'_/_/  -' _.' /
===========`-.`___`-.__\\ \\___  /__.-'_.'_.-'================

                  Please give me AC.
*/
#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define INF (~(1 << 31))
#define eps 1e-6
#define PI 3.141592653589793238462643383
#define lowbit(x) ((x) & (-x))
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define all(v) (v).begin(), (v).end()
#define dbg(x, y) cout << x << " = " << y << endl;
#define fi first
#define se second
#define ls rt << 1
#define rs rt << 1 | 1
#define ll long long
// #define int ll
typedef bitset<40000> bt;
typedef pair<int, int> pii;
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  if (c == '-')
    f = -1, c = getchar();
  while (c >= '0' && c <= '9')
    x = x * 10 + c - '0', c = getchar();
  return f * x;
}

const int N = 1e6 + 7;
const int M = 2e6 + 7;
const ll MAXN = 4e18 + 7;
const ll Mod = 998244353;

int _, i, j, k, n, m, p, s, T, l, r, o, u, v, w, x, y, z, st, en, sx, sy, tx,
    ty, ans, len, th, ma, mi, mod, sum, num, res, flag, cas, ret, mid, cnt, now,
    nex, tmp;
int a[N], mp[N], c[N];
char ch;
queue<int> q;
vector<int> g, h;
string s1, s2, s3;

void bfs() {
  q.push((sx - 1) * m + sy);
  mp[(sx - 1) * m + sy] = 0;
  // dbg(sx,sy)
  while (!q.empty()) {
    u = q.front(), q.pop();
    y = (u - 1) % m + 1;
    x = (u - y) / m + 1;
    for (int i = 1; i <= k; i++) {
      if (y - i < 1 || !a[u - i])
        break;
      if (mp[u - i] > mp[u])
        mp[u - i] = mp[u] + 1, q.push(u - i);
      else if (mp[u - i] < mp[u])
        break;
    }
    for (int i = 1; i <= k; i++) {
      if (y + i > m || !a[u + i])
        break;
      if (mp[u + i] > mp[u])
        mp[u + i] = mp[u] + 1, q.push(u + i);
      else if (mp[u + i] < mp[u])
        break;
    }
    for (int i = 1; i <= k; i++) {
      v = u - i * m;
      if (x - i < 1 || !a[v])
        break;
      if (mp[v] > mp[u])
        mp[v] = mp[u] + 1, q.push(v);
      else if (mp[v] < mp[u])
        break;
    }
    for (int i = 1; i <= k; i++) {
      v = u + i * m;
      if (x + i > n || !a[v])
        break;
      if (mp[v] > mp[u])
        mp[v] = mp[u] + 1, q.push(v);
      else if (mp[v] < mp[u])
        break;
    }
  }
}

signed main() {
  ios::sync_with_stdio(false);
  int T = 1;
  double x1, x2, x3, y1, y2, y3, z1, z2, z3;
  // cin >> T;
  while (T--) {
    cin >> n >> m >> k;
    cin >> sx >> sy >> tx >> ty;
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= m; j++) {
        cin >> ch;
        if (ch == '.')
          a[(i - 1) * m + j] = 1;
        else
          a[(i - 1) * m + j] = 0;
        mp[(i - 1) * m + j] = INF;
      }
    }
    bfs();
    //        for(i = 1;i <= n;i++){
    //            for(j = 1;j <= m;j++){
    //                cout << mp[(i-1)*m+j] << " ";
    //            }
    //            cout << endl;
    //        }
    if (mp[(tx - 1) * m + ty] == INF)
      cout << -1 << endl;
    else
      cout << mp[(tx - 1) * m + ty] << endl;
  }
  return 0;
}