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


#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define INF 1e9
#define LINF (ll) INF *INF
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, a, n) for (int i = a; i < (n); i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

const double PI = acos(-1);
const double EPS = 1e-9; // うまくいかなかったらゆるめる

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef pair<int, pii> pip;
typedef vector<pii> vp;
typedef vector<vi> vvi;

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }

#define in(a, b, c) make_pair(a, pii(b, c))

signed main(void) {
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;
  vs s(h);
  rep(i, h) cin >> s[i];
  vvi d(h, vi(w, INF));
  queue<pip> q;
  rep(i, 4) {
    d[sx][sy] = 0;
    q.push(in(0, sx, sy));
  }
  int dx[] = {0, 1, 0, -1};
  int dy[] = {1, 0, -1, 0};
  while (q.size()) {
    pip p = q.front();
    int x = p.second.first;
    int y = p.second.second;
    q.pop();
    rep(i, 4) rep(j, k) {
      int nx = x + (j + 1) * dx[i];
      int ny = y + (j + 1) * dy[i];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w)
        continue;
      if (s[nx][ny] == '@')
        break;
      int ne = d[x][y] + 1;
      if (d[nx][ny] > ne) {
        d[nx][ny] = ne;
        q.push(in(d[nx][ny], nx, ny));
      } else if (d[nx][ny] < ne)
        break;
    }
  }

  int ans = d[gx][gy];
  if (ans == INF) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:


#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ll long long
#define ld long double
#define INF 1e9
#define LINF (ll) INF *INF
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, a, n) for (int i = a; i < (n); i++)
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

const double PI = acos(-1);
const double EPS = 1e-9; // うまくいかなかったらゆるめる

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef pair<int, pii> pip;
typedef vector<pii> vp;
typedef vector<vi> vvi;

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }

#define in(a, b, c) make_pair(a, pii(b, c))

signed main(void) {
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;
  vs s(h);
  rep(i, h) cin >> s[i];
  vvi d(h, vi(w, INF));
  queue<pip> q;
  rep(i, 4) {
    d[sx][sy] = 0;
    q.push(in(0, sx, sy));
  }
  int dx[] = {0, 1, 0, -1};
  int dy[] = {1, 0, -1, 0};
  while (q.size()) {
    pip p = q.front();
    int x = p.second.first;
    int y = p.second.second;
    if (x == gx && y == gy)
      break;
    if (d[x][y] != p.first)
      continue;
    q.pop();
    rep(i, 4) rep(j, k) {
      int nx = x + (j + 1) * dx[i];
      int ny = y + (j + 1) * dy[i];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w)
        continue;
      if (s[nx][ny] == '@')
        break;
      int ne = d[x][y] + 1;
      if (d[nx][ny] > ne) {
        d[nx][ny] = ne;
        q.push(in(d[nx][ny], nx, ny));
      }
    }
  }

  int ans = d[gx][gy];
  if (ans == INF) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}