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

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

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;

#define fill(a, x) memset(a, x, sizeof(a))
#define pb push_back
#define sz(x) (int)x.size()
#define F first
#define S second
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define NFOR(i, a, b) for (int i = a; i >= b; --i)
#define fast ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
const ll INF = 1e18;
const ll mod = 1e9 + 7;
const int N = 1e6 + 10;

vector<string> a;
queue<pair<ii, int>> q;
vector<ii> temp;
int n, m;
bool isvalid(int x, int y) {
  if (x >= 0 and x < n and y >= 0 and y < m)
    return true;
  return false;
}

int main() {
  fast;
  int k;
  cin >> n >> m >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  a.clear();
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    a.push_back(s);
  }

  while (!q.empty()) {
    q.pop();
  }

  if (a[x1][y1] == '@' or a[x2][y2] == '@') {
    cout << -1 << endl;
    return 0;
  }

  q.push(make_pair(make_pair(x1, y1), 0));
  while (!q.empty()) {
    ii tp = q.front().first;
    int dist = q.front().second;
    q.pop();
    if (a[tp.first][tp.second] == '@') {
      continue;
    }

    int x = tp.first;
    int y = tp.second;
    // cout<<"reached "<<x<<" "<<y<<" with "<<dist<<endl;

    if (x == x2 and y == y2) {
      cout << dist << endl;
      return 0;
    }

    if (a[tp.first][tp.second] == '#') {
      continue;
    }

    a[x][y] = '#';

    temp.clear();
    for (int i = 1; i <= k; i++) {
      if (isvalid(x + i, y) and a[x + i][y] == '.') {
        a[x + i][y] = '*';
        temp.push_back(make_pair(x + i, y));
      } else if (isvalid(x + i, y) and a[x + i][y] == '*') {
        continue;
      } else
        break;
    }
    reverse(temp.begin(), temp.end());
    for (int i = 0; i < temp.size(); i++) {
      // cout<<"queing "<<temp[i].first<<" "<<temp[i].second<<" with
      // "<<dist+1<<endl;
      q.push(make_pair(temp[i], dist + 1));
    }

    temp.clear();
    for (int i = 1; i <= k; i++) {
      if (isvalid(x - i, y) and a[x - i][y] == '.') {
        a[x - i][y] = '*';
        temp.push_back(make_pair(x - i, y));
      } else if (isvalid(x - i, y) and a[x - i][y] == '*') {
        continue;
      } else
        break;
    }
    reverse(temp.begin(), temp.end());
    for (int i = 0; i < temp.size(); i++) {
      // cout<<"queing "<<temp[i].first<<" "<<temp[i].second<<" with
      // "<<dist+1<<endl;
      q.push(make_pair(temp[i], dist + 1));
    }

    temp.clear();
    for (int i = 1; i <= k; i++) {
      if (isvalid(x, y + i) and a[x][y + i] == '.') {
        a[x][y + i] = '*';
        temp.push_back(make_pair(x, y + i));
      } else if (isvalid(x, y + i) and a[x][y + i] == '*') {
        continue;
      } else
        break;
    }
    reverse(temp.begin(), temp.end());
    for (int i = 0; i < temp.size(); i++) {
      // cout<<"queing "<<temp[i].first<<" "<<temp[i].second<<" with
      // "<<dist+1<<endl;
      q.push(make_pair(temp[i], dist + 1));
    }

    temp.clear();
    for (int i = 1; i <= k; i++) {
      if (isvalid(x, y - i) and a[x][y - i] == '.') {
        a[x][y - i] = '*';
        temp.push_back(make_pair(x, y - i));
      } else if (isvalid(x, y - i) and a[x][y - i] == '*') {
        continue;
      } else
        break;
    }
    reverse(temp.begin(), temp.end());
    for (int i = 0; i < temp.size(); i++) {
      // cout<<"queing "<<temp[i].first<<" "<<temp[i].second<<" with
      // "<<dist+1<<endl;
      q.push(make_pair(temp[i], dist + 1));
    }
  }

  cout << -1 << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;

#define fill(a, x) memset(a, x, sizeof(a))
#define pb push_back
#define sz(x) (int)x.size()
#define F first
#define S second
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define NFOR(i, a, b) for (int i = a; i >= b; --i)
#define fast ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
const ll INF = 1e18;
const ll mod = 1e9 + 7;
const int N = 1e6 + 10;

vector<string> a;
queue<pair<ii, int>> q;
vector<ii> temp;
int n, m;
bool isvalid(int x, int y) {
  if (x >= 0 and x < n and y >= 0 and y < m)
    return true;
  return false;
}

int main() {
  fast;
  int k;
  cin >> n >> m >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  a.clear();
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    a.push_back(s);
  }

  while (!q.empty()) {
    q.pop();
  }

  if (a[x1][y1] == '@' or a[x2][y2] == '@') {
    cout << -1 << endl;
    return 0;
  }

  q.push(make_pair(make_pair(x1, y1), 0));
  while (!q.empty()) {
    ii tp = q.front().first;
    int dist = q.front().second;
    q.pop();
    if (a[tp.first][tp.second] == '@') {
      continue;
    }

    int x = tp.first;
    int y = tp.second;
    // cout<<"reached "<<x<<" "<<y<<" with "<<dist<<endl;

    if (x == x2 and y == y2) {
      cout << dist << endl;
      return 0;
    }

    if (a[tp.first][tp.second] == '#') {
      continue;
    }

    a[x][y] = '#';

    temp.clear();
    for (int i = 1; i <= k; i++) {
      if (isvalid(x + i, y) and a[x + i][y] == '.') {
        temp.push_back(make_pair(x + i, y));
      } else
        break;
    }
    reverse(temp.begin(), temp.end());
    for (int i = 0; i < temp.size(); i++) {
      // cout<<"queing "<<temp[i].first<<" "<<temp[i].second<<" with
      // "<<dist+1<<endl;
      q.push(make_pair(temp[i], dist + 1));
    }

    temp.clear();
    for (int i = 1; i <= k; i++) {
      if (isvalid(x - i, y) and a[x - i][y] == '.') {
        temp.push_back(make_pair(x - i, y));
      } else
        break;
    }
    reverse(temp.begin(), temp.end());
    for (int i = 0; i < temp.size(); i++) {
      // cout<<"queing "<<temp[i].first<<" "<<temp[i].second<<" with
      // "<<dist+1<<endl;
      q.push(make_pair(temp[i], dist + 1));
    }

    temp.clear();
    for (int i = 1; i <= k; i++) {
      if (isvalid(x, y + i) and a[x][y + i] == '.') {
        temp.push_back(make_pair(x, y + i));
      } else
        break;
    }
    reverse(temp.begin(), temp.end());
    for (int i = 0; i < temp.size(); i++) {
      // cout<<"queing "<<temp[i].first<<" "<<temp[i].second<<" with
      // "<<dist+1<<endl;
      q.push(make_pair(temp[i], dist + 1));
    }

    temp.clear();
    for (int i = 1; i <= k; i++) {
      if (isvalid(x, y - i) and a[x][y - i] == '.') {
        temp.push_back(make_pair(x, y - i));
      } else
        break;
    }
    reverse(temp.begin(), temp.end());
    for (int i = 0; i < temp.size(); i++) {
      // cout<<"queing "<<temp[i].first<<" "<<temp[i].second<<" with
      // "<<dist+1<<endl;
      q.push(make_pair(temp[i], dist + 1));
    }
  }

  cout << -1 << endl;

  return 0;
}