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

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
double pi = 3.14159265358979323846264338;

int h, w, k;
int x1, x2, z, y2;
string arr[6000001];
int arr2[4];
int clf, crg;
int main() {
  clf = -1;
  crg = -1;
  cin >> h >> w >> k;

  cin >> x1 >> x2 >> z >> y2;
  for (int i = 1; i <= h; i++)
    cin >> arr[i];
  long long dis = 0;
  queue<pair<int, int>> que;
  map<pair<int, int>, int> map;
  map.insert(pair<pair<int, int>, int>(make_pair(x1, x2 - 1), 0));
  arr[x1][x2 - 1] = '@';
  que.push(make_pair(x1, x2 - 1));
  while (!que.empty()) {
    auto cur = que.front();
    que.pop();
    // int ind=1;
    int lf = cur.first;
    int rg = cur.second;
    arr[lf][rg] = '@';
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int llf = lf + j * dx[i];
        int rrg = rg + j * dy[i];
        if (llf < 1 || llf > h || rrg < 0 || rrg > w - 1)
          break;
        if (arr[llf][rrg] == '@')
          break;
        if (arr[llf][rrg] == 'b')
          continue;
        // else cout<<llf<<" "<<rrg<<" "<<arr[llf][rrg]<<endl;
        if (llf == z && rrg + 1 == y2) {
          //	cout<<lf<<" "<<rg<<" "<<llf<<" "<<rrg<<endl;

          cout << map[cur] + 1 << endl;
          return 0;
        }
        arr[llf][rrg] = 'b';
        que.push(make_pair(llf, rrg));
        map.insert(
            pair<pair<int, int>, int>(make_pair(llf, rrg), map[cur] + 1));
      }
    }
  }
  cout << "-1" << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
double pi = 3.14159265358979323846264338;

int h, w, k;
int x1, x2, z, y2;
string arr[6000001];

int main() {

  cin >> h >> w >> k;

  cin >> x1 >> x2 >> z >> y2;
  for (int i = 1; i <= h; i++)
    cin >> arr[i];
  long long dis = 0;
  queue<pair<int, int>> que;
  map<pair<int, int>, int> map;
  map.insert(pair<pair<int, int>, int>(make_pair(x1, x2 - 1), 0));
  arr[x1][x2 - 1] = 'b';
  que.push(make_pair(x1, x2 - 1));
  while (!que.empty()) {
    auto cur = que.front();
    que.pop();
    int lf = cur.first;
    int rg = cur.second;
    for (int i = 0; i < 4; i++) {
      for (int j = 1; j <= k; j++) {
        int llf = lf + j * dx[i];
        int rrg = rg + j * dy[i];
        if (llf < 1 || llf > h || rrg < 0 || rrg > w - 1)
          break;
        if (arr[llf][rrg] == '@')
          break;
        if (arr[llf][rrg] == 'b')
          continue;
        // else cout<<llf<<" "<<rrg<<" "<<arr[llf][rrg]<<endl;
        if (llf == z && rrg + 1 == y2) {
          //	cout<<lf<<" "<<rg<<" "<<llf<<" "<<rrg<<endl;

          cout << map[cur] + 1 << endl;
          return 0;
        }
        arr[llf][rrg] = 'b';
        que.push(make_pair(llf, rrg));
        map.insert(
            pair<pair<int, int>, int>(make_pair(llf, rrg), map[cur] + 1));
      }
    }
  }
  cout << "-1" << endl;

  return 0;
}