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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
typedef pair<int, int> pp;
bool com(pp x, pp y) { return x.ff < y.ff; }
ll power(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1) {
      res = (a * res) % 10;
    }
    a = (a * a);
    b /= 2;
  }
  return res;
}
int n, m, k;
vector<string> s(1e6 + 7);
bool valid(int x, int y) {
  if (x < 1 || x > n || y < 1 || y > m)
    return false;
  if (s[x - 1][y - 1] == '@')
    return false;
  return true;
}
int dfs(int x1, int y1, int x2, int y2) {
  vector<vector<int>> dis(n + 1, vector<int>(m + 1, 1e7));
  queue<pp> q;
  dis[x1][y1] = 0;
  q.emplace(x1, y1);
  while (!q.empty()) {
    x1 = q.front().ff;
    y1 = q.front().ss;
    q.pop();
    // cout<<x1<<" "<<y1<<endl;
    for (int i = 1; i <= k && x1 + i <= n; i++) {
      // cout<<x1+i<<" "<<y1<<endl;
      if (!valid(x1 + i, y1))
        break;
      if (dis[x1 + i][y1] < dis[x1][y1] + 1)
        break;
      if (dis[x1 + i][y1] == 1e7) {
        dis[x1 + i][y1] = dis[x1][y1] + 1;
        q.emplace(x1 + i, y1);
      }
    }
    for (int i = 1; i <= k && x1 - i > 0; i++) {
      if (!valid(x1 - i, y1))
        break;
      if (dis[x1 - i][y1] < dis[x1][y1] + 1)
        break;
      if (dis[x1 - i][y1] == 1e7) {
        dis[x1 - i][y1] = dis[x1][y1] + 1;
        q.emplace(x1 - i, y1);
      }
    }
    for (int i = 1; i <= k && y1 + i <= m; i++) {
      if (!valid(x1, y1 + i))
        break;
      if (dis[x1][y1 + i] < dis[x1][y1] + 1)
        break;
      if (dis[x1][y1 + i] == 1e7) {
        dis[x1][y1 + i] = dis[x1][y1] + 1;
        q.emplace(x1, y1 + i);
      }
    }
    for (int i = 1; i <= k && y1 - i > 0; i++) {
      if (!valid(x1, y1 - i))
        break;
      if (dis[x1][y1 - i] < dis[x1][y1] + 1)
        break;
      if (dis[x1][y1 - i] == 1e7) {
        dis[x1][y1 - i] = dis[x1][y1] + 1;
        q.emplace(x1, y1 - i);
      }
    }
  }
  if (dis[x2][y2] == 1e7)
    return -1;
  return dis[x2][y2];
}
void solve() {
  cin >> n >> m >> k;
  int x1, x2, y1, y2;
  // vector<vector<int>> vis(n+1,vector<int> (m+1,0));
  cin >> x1 >> y1 >> x2 >> y2;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  cout << dfs(x1, y1, x2, y2);
}
int main() {
  // your code goes here
  // isprime();
  // prime();
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // int t;
  // cin>>t;
  // while(t--)
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
typedef pair<int, int> pp;
bool com(pp x, pp y) { return x.ff < y.ff; }
ll power(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1) {
      res = (a * res) % 10;
    }
    a = (a * a);
    b /= 2;
  }
  return res;
}
int n, m, k;
vector<string> s(1e6 + 7);
unordered_map<int, unordered_map<int, int>> vis;
bool valid(int x, int y) {
  if (x < 1 || x > n || y < 1 || y > m)
    return false;
  if (s[x - 1][y - 1] == '@')
    return false;
  return true;
}
int dfs(int x1, int y1, int x2, int y2) {
  vector<vector<int>> dis(n + 1, vector<int>(m + 1, 1e7));
  queue<pp> q;
  dis[x1][y1] = 0;
  q.emplace(x1, y1);
  while (!q.empty()) {
    x1 = q.front().ff;
    y1 = q.front().ss;
    q.pop();
    vis[x1][y1] = 1;
    for (int i = 1; i <= k && x1 + i <= n; i++) {
      // cout<<x1+i<<" "<<y1<<endl;
      if (valid(x1 + i, y1) && dis[x1][y1] + 1 < dis[x1 + i][y1]) {
        dis[x1 + i][y1] = min(dis[x1][y1] + 1, dis[x1 + i][y1]);
        q.emplace(x1 + i, y1);
      } else if (!valid(x1 + i, y1))
        break;
    }
    for (int i = 1; i <= k && x1 - i > 0; i++) {
      if (valid(x1 - i, y1) && dis[x1 - i][y1] > dis[x1][y1] + 1) {
        dis[x1 - i][y1] = min(dis[x1][y1] + 1, dis[x1 - i][y1]);
        q.emplace(x1 - i, y1);
      } else if (!valid(x1 - i, y1))
        break;
    }
    for (int i = 1; i <= k && y1 + i <= m; i++) {
      if (valid(x1, y1 + i) && dis[x1][y1 + i] > dis[x1][y1] + 1) {
        dis[x1][y1 + i] = dis[x1][y1] + 1;
        q.emplace(x1, y1 + i);
      } else if (!valid(x1, y1 + i))
        break;
    }
    for (int i = 1; i <= k && y1 - i > 0; i++) {
      if (valid(x1, y1 - i) && dis[x1][y1 - i] > dis[x1][y1] + 1) {
        dis[x1][y1 - i] = min(dis[x1][y1] + 1, dis[x1][y1 - i]);
        q.emplace(x1, y1 - i);
      } else if (!valid(x1, y1 - i))
        break;
    }
  }
  if (dis[x2][y2] == 1e7)
    return -1;
  return dis[x2][y2];
}
void solve() {
  cin >> n >> m >> k;
  int x1, x2, y1, y2;
  // vector<vector<int>> vis(n+1,vector<int> (m+1,0));
  cin >> x1 >> y1 >> x2 >> y2;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  cout << dfs(x1, y1, x2, y2);
}
int main() {
  // your code goes here
  // isprime();
  // prime();
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // int t;
  // cin>>t;
  // while(t--)
  solve();
  return 0;
}