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

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ld long double
#define MOD 1000000007
#define endl "\\n"
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define MAXX 9000000000000000000
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vpi vector<pair<int, int>>
#define vvpi vector<vector<pair<int, int>>>

int h, w, k, x1, x2, yy1, y2;
vvi vis(1000006);

string s[1000006];

void dfs() {
  queue<pii> lis;
  lis.push(mp(x1, yy1));
  vis[x1][yy1] = 0;
  int i, j;
  while (!lis.empty()) {
    auto it = lis.front();

    int x = it.ff, y = it.ss;

    lis.pop();

    // cout << x << y << k << endl;

    for (i = 1; i <= k; i++) {
      // return;
      if (x + i >= h) {
        break;
      }
      if (s[x + i][y] == '@' ||
          (vis[x + i][y] <= vis[x][y] && vis[x + i][y] != -1)) {
        break;
      }
      if (vis[x + i][y] == -1) {
        lis.push(mp(x + i, y));
      }
      vis[x + i][y] = 1 + vis[x][y];
    }
    for (i = 1; i <= k; i++) {
      if (x - i < 0) {
        break;
      }
      if (s[x - i][y] == '@' ||
          (vis[x - i][y] <= vis[x][y] && vis[x - i][y] != -1)) {
        break;
      }
      if (vis[x - i][y] == -1) {
        lis.push(mp(x - i, y));
      }
      vis[x - i][y] = 1 + vis[x][y];

      // lis.push(mp(x-i, y));
    }
    for (i = 1; i <= k; i++) {
      if (y + i >= w) {
        break;
      }
      if (s[x][y + i] == '@' ||
          (vis[x][y + i] <= vis[x][y] && vis[x][y + i] != -1)) {
        break;
      }
      if (vis[x][y + i] == -1) {
        lis.push(mp(x, y + i));
      }
      vis[x][y + i] = 1 + vis[x][y];

      // lis.push(mp(x, y+i));
    }
    for (i = 1; i <= k; i++) {
      if (y - i < 0) {
        break;
      }
      if (s[x][y - i] == '@' ||
          (vis[x][y - i] <= vis[x][y] && vis[x][y - i] != -1)) {
        break;
      }
      if (vis[x][y - i] == -1) {
        lis.push(mp(x, y - i));
      }
      vis[x][y - i] = 1 + vis[x][y];

      // lis.push(mp(x, y-i));
    }
  }
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int i, j;

  cin >> h >> w >> k;
  cin >> x1 >> yy1 >> x2 >> y2;

  x1--;
  x2--;
  yy1--;
  y2--;

  for (i = 0; i < h; i++) {
    cin >> s[i];
    for (j = 0; j < w; j++) {
      vis[i].pb(-1);
    }
  }

  dfs();

  // for(i=0;i<h;i++)
  // {
  // 	for(j=0;j<w;j++)
  // 	{
  // 		cout << vis[i][j] << " ";
  // 	}
  // 	cout << endl;
  // }
  cout << vis[x2][y2];

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ld long double
#define MOD 1000000007
#define endl "\\n"
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define MAXX 9000000000000000000
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vpi vector<pair<int, int>>
#define vvpi vector<vector<pair<int, int>>>

int h, w, k, x1, x2, yy1, y2;
vvi vis(1000006);

string s[1000006];

void dfs() {
  queue<pii> lis;
  lis.push(mp(x1, yy1));
  vis[x1][yy1] = 0;
  int i, j;
  while (!lis.empty()) {
    auto it = lis.front();

    int x = it.ff, y = it.ss;

    lis.pop();

    // cout << x << y << k << endl;

    for (i = 1; i <= k; i++) {
      // return;
      if (x + i >= h) {
        break;
      }
      if (s[x + i][y] == '@' ||
          (vis[x + i][y] <= vis[x][y] && vis[x + i][y] != -1)) {
        break;
      }
      vis[x + i][y] = 1 + vis[x][y];

      lis.push(mp(x + i, y));
    }
    for (i = 1; i <= k; i++) {
      if (x - i < 0) {
        break;
      }
      if (s[x - i][y] == '@' ||
          (vis[x - i][y] <= vis[x][y] && vis[x - i][y] != -1)) {
        break;
      }
      vis[x - i][y] = 1 + vis[x][y];

      lis.push(mp(x - i, y));
    }
    for (i = 1; i <= k; i++) {
      if (y + i >= w) {
        break;
      }
      if (s[x][y + i] == '@' ||
          (vis[x][y + i] <= vis[x][y] && vis[x][y + i] != -1)) {
        break;
      }
      vis[x][y + i] = 1 + vis[x][y];

      lis.push(mp(x, y + i));
    }
    for (i = 1; i <= k; i++) {
      if (y - i < 0) {
        break;
      }
      if (s[x][y - i] == '@' ||
          (vis[x][y - i] <= vis[x][y] && vis[x][y - i] != -1)) {
        break;
      }
      vis[x][y - i] = 1 + vis[x][y];

      lis.push(mp(x, y - i));
    }
  }
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int i, j;

  cin >> h >> w >> k;
  cin >> x1 >> yy1 >> x2 >> y2;

  x1--;
  x2--;
  yy1--;
  y2--;

  for (i = 0; i < h; i++) {
    cin >> s[i];
    for (j = 0; j < w; j++) {
      vis[i].pb(-1);
    }
  }

  dfs();

  // for(i=0;i<h;i++)
  // {
  // 	for(j=0;j<w;j++)
  // 	{
  // 		cout << vis[i][j] << " ";
  // 	}
  // 	cout << endl;
  // }
  cout << vis[x2][y2];

  return 0;
}