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


#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define eb emplace_back
#define el '\\n'
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<bool> vb;

const int mod = 1000000007;
// const int mod=998244353;
ll n, m, k;
vector<string> v;

vvi mark;
vvi dist;

bool isvalid(int i, int j) {
  if (i < 0 || j < 0 || i > n - 1 || j > m - 1 || v[i][j] == '@') {
    return false;
  }
  return true;
}

void bfs(int u, int v) {
  queue<pii> q;
  q.push({u, v});
  vi di({0, 0, -1, 1}), dj({-1, 1, 0, 0});
  while (!q.empty()) {
    pii f = q.front();
    q.pop();
    mark[f.fi][f.se] = 1;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int nx = f.fi + di[i] * j, ny = f.se + dj[i] * j;
        if (!isvalid(nx, ny))
          break;
        if (mark[nx][ny] && dist[f.fi][f.se] + 1 > dist[nx][ny])
          break;
        if (mark[nx][ny])
          continue;
        mark[nx][ny] = 1;
        dist[nx][ny] = dist[f.fi][f.se] + 1;
        q.push({nx, ny});
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t, q, x, a, b, c, d, y, l, r, p;
  cin >> n >> m >> k;
  cin >> a >> b >> c >> d;
  a--;
  b--;
  c--;
  d--;
  v.resize(n);
  mark.resize(n, vi(m, 0));
  dist.resize(n, vi(m, 1e9));
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  dist[a][b] = 0;
  bfs((int)a, (int)b);
  if (dist[c][d] != 1e9) {
    cout << dist[c][d] << el;
  } else {
    cout << -1 << el;
  }
  return 0;
}


Error: TLE

Fixed submission:



#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define eb emplace_back
#define el '\\n'
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<bool> vb;

const int mod = 1000000007;
// const int mod=998244353;
int n, m, k;
vvi dist;
vvi mark;
vector<string> vv;

bool isvalid(int u, int v) {
  if (u < 0 || v < 0 || u > n - 1 || v > m - 1 || vv[u][v] == '@')
    return false;
  return true;
}

void bfs(int u, int v) {
  queue<pii> q;
  q.push({u, v});
  while (!q.empty()) {
    pii f = q.front();
    q.pop();
    mark[f.fi][f.se] = 1;
    for (int i = 1; i <= k; i++) {
      if (isvalid(f.fi, f.se + i)) {
        if (!mark[f.fi][f.se + i]) {
          mark[f.fi][f.se + i] = 1;
          dist[f.fi][f.se + i] = dist[f.fi][f.se] + 1;
          q.push({f.fi, f.se + i});
        }
      } else
        break;
    }
    for (int i = 1; i <= k; i++) {
      if (isvalid(f.fi + i, f.se)) {
        if (!mark[f.fi + i][f.se]) {
          mark[f.fi + i][f.se] = 1;
          dist[f.fi + i][f.se] = dist[f.fi][f.se] + 1;
          q.push({f.fi + i, f.se});
        }
      } else
        break;
    }
    for (int i = 1; i <= k; i++) {
      if (isvalid(f.fi, f.se - i)) {
        if (!mark[f.fi][f.se - i]) {
          mark[f.fi][f.se - i] = 1;
          dist[f.fi][f.se - i] = dist[f.fi][f.se] + 1;
          q.push({f.fi, f.se - i});
        }
      } else
        break;
    }
    for (int i = 1; i <= k; i++) {
      if (isvalid(f.fi - i, f.se)) {
        if (!mark[f.fi - i][f.se]) {
          mark[f.fi - i][f.se] = 1;
          dist[f.fi - i][f.se] = dist[f.fi][f.se] + 1;
          q.push({f.fi - i, f.se});
        }
      } else
        break;
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t, q, x, a, b, c, d, y, l, r, p;
  cin >> n >> m >> k;
  mark.resize(n, vi(m));
  dist.assign(n, vi(m, 1e9));
  cin >> a >> b >> c >> d;
  a--;
  b--;
  c--;
  d--;
  dist[a][b] = 0;
  vv.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> vv[i];
  }
  bfs(a, b);
  if (dist[c][d] == 1e9) {
    cout << -1 << el;
  } else {
    cout << dist[c][d] << el;
  }

  return 0;
}