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

#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

#define MOD 1000000007LL

int h, w, k;
int xx1, yy1, xx2, yy2;

vector<vector<char>> c;
vector<vector<ll>> m;
queue<pair<int, int>> q;

ll move(int px, int py, ll mn) {
  if (px == xx2 && py == yy2)
    return mn;
  // printf("  move(%d,%d): %c,%lld\\n", px, py, c[px][py], mn);
  if (c[px][py] == '@')
    return -1;
  if (m[px][py] == mn)
    return -2;
  if (m[px][py] != 0 && m[px][py] < mn)
    return -1;
  // printf("push(%d,%d): %lld\\n", px, py, mn);
  m[px][py] = mn;
  return 0;
}
ll move() {
  pair<int, int> p;
  int px, py;
  ll mret;

  q.push(make_pair(xx1, yy1));

  while (!q.empty()) {
    p = q.front();
    q.pop();

    // printf("(%d,%d)=%lld\\n", p.first, p.second, m[p.first][p.second]);
    if (p.first == xx2 && p.second == yy2) {
      return m[p.first][p.second];
    }

    ll mn = m[p.first][p.second] + 1;

    // 上に移動
    py = p.second;
    for (px = p.first - 1; px >= 0 && p.first - px <= k; px--) {
      mret = move(px, py, mn);
      if (mret == -1)
        break;
      if (mret == -2)
        continue;
      if (mret > 0)
        return mret;
      q.push(make_pair(px, py));
    }
    // 下に移動
    for (px = p.first + 1; px < h && px - p.first <= k; px++) {
      mret = move(px, py, mn);
      if (mret == -1)
        break;
      if (mret == -2)
        continue;
      if (mret > 0)
        return mret;
      q.push(make_pair(px, py));
    }
    // 左に移動
    px = p.first;
    for (py = p.second - 1; py >= 0 && p.second - py <= k; py--) {
      mret = move(px, py, mn);
      if (mret == -1)
        break;
      if (mret == -2)
        continue;
      if (mret > 0)
        return mret;
      q.push(make_pair(px, py));
    }
    // 右に移動
    for (py = p.second + 1; py < w && py - p.second <= k; py++) {
      mret = move(px, py, mn);
      if (mret == -1)
        break;
      if (mret == -2)
        continue;
      if (mret > 0)
        return mret;
      q.push(make_pair(px, py));
    }
  }
  return -1;
}

int main() {
  ll ans = 0;

  cin >> h >> w >> k;
  cin >> xx1 >> yy1 >> xx2 >> yy2;
  xx1--;
  yy1--;
  xx2--;
  yy2--;

  c.resize(h);
  m.resize(h);
  for (int i = 0; i < h; i++) {
    c[i].resize(w);
    m[i].resize(w);
    for (int j = 0; j < w; j++) {
      cin >> c[i][j];
    }
  }
  /*
  for (int i=0; i<h; i++) {
    for (int j=0; j<w; j++) {
      printf("%c", c[i][j]);
    }
    printf("\\n");
  }
    */

  m[xx1][yy1] = 0;

  ans = move();

  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

#define MOD 1000000007LL

int h, w, k;
int xx1, yy1, xx2, yy2;

vector<vector<char>> c;
vector<vector<ll>> m;
queue<pair<int, int>> q;

ll move() {
  pair<int, int> p;
  int px, py;

  q.push(make_pair(xx1, yy1));

  while (!q.empty()) {
    p = q.front();
    q.pop();

    // printf("(%d,%d)=%lld\\n", p.first, p.second, m[p.first][p.second]);
    if (p.first == xx2 && p.second == yy2) {
      return m[p.first][p.second];
    }

    ll mn = m[p.first][p.second] + 1;

    // 上に移動
    for (px = p.first - 1; px >= 0 && p.first - px <= k; px--) {
      if (px == xx2 && p.second == yy2)
        return mn;
      // printf("  move(%d,%d): %c,%lld\\n", px, p.second, c[px][p.second], mn);
      if (c[px][p.second] == '@')
        break;
      if (m[px][p.second] == mn)
        continue;
      if (m[px][p.second] != 0 && m[px][p.second] < mn)
        break;
      // printf("push(%d,%d): %lld\\n", px, p.second, mn);
      m[px][p.second] = mn;
      q.push(make_pair(px, p.second));
    }
    // 下に移動
    for (px = p.first + 1; px < h && px - p.first <= k; px++) {
      if (px == xx2 && p.second == yy2)
        return mn;
      // printf("  move(%d,%d): %c,%lld\\n", px, p.second, c[px][p.second], mn);
      if (c[px][p.second] == '@')
        break;
      if (m[px][p.second] == mn)
        continue;
      if (m[px][p.second] != 0 && m[px][p.second] < mn)
        break;
      // printf("push(%d,%d): %lld\\n", px, p.second, mn);
      m[px][p.second] = mn;
      q.push(make_pair(px, p.second));
    }
    // 左に移動
    for (py = p.second - 1; py >= 0 && p.second - py <= k; py--) {
      if (p.first == xx2 && py == yy2)
        return mn;
      // printf("  move(%d,%d): %c,%lld\\n", p.first, py, c[p.first][py], mn);
      if (c[p.first][py] == '@')
        break;
      if (m[p.first][py] == mn)
        continue;
      if (m[p.first][py] != 0 && m[p.first][py] < mn)
        break;
      // printf("push(%d,%d): %lld\\n", p.first, py, mn);
      m[p.first][py] = mn;
      q.push(make_pair(p.first, py));
    }
    // 右に移動
    for (py = p.second + 1; py < w && py - p.second <= k; py++) {
      if (p.first == xx2 && py == yy2)
        return mn;
      // printf("  move(%d,%d): %c,%lld\\n", p.first, py, c[p.first][py], mn);
      if (c[p.first][py] == '@')
        break;
      if (m[p.first][py] == mn)
        continue;
      if (m[p.first][py] < mn && m[p.first][py] != 0)
        continue;
      // printf("push(%d,%d): %lld\\n", p.first, py, mn);
      m[p.first][py] = mn;
      q.push(make_pair(p.first, py));
    }
  }
  return -1;
}

int main() {
  ll ans = 0;

  cin >> h >> w >> k;
  cin >> xx1 >> yy1 >> xx2 >> yy2;
  xx1--;
  yy1--;
  xx2--;
  yy2--;

  c.resize(h);
  m.resize(h);
  for (int i = 0; i < h; i++) {
    c[i].resize(w);
    m[i].resize(w);
    for (int j = 0; j < w; j++) {
      cin >> c[i][j];
    }
  }
  /*
  for (int i=0; i<h; i++) {
    for (int j=0; j<w; j++) {
      printf("%c", c[i][j]);
    }
    printf("\\n");
  }
    */

  m[xx1][yy1] = 0;

  ans = move();

  cout << ans << endl;
  return 0;
}