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


#include <algorithm>
#include <chrono>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define forr(i, start, count) for (int i = (start); i < (start) + (count); ++i)
#define set_map_includes(set, elt) (set.find((elt)) != set.end())
#define readint(i)                                                             \\
  int i;                                                                       \\
  cin >> i
#define readll(i)                                                              \\
  ll i;                                                                        \\
  cin >> i
#define readdouble(i)                                                          \\
  double i;                                                                    \\
  cin >> i
#define readstring(s)                                                          \\
  string s;                                                                    \\
  cin >> s

typedef long long ll;

using namespace std;

int main() {
  cout.precision(17);
  ll modd = 1000 * 1000 * 1000 + 7;
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  uniform_int_distribution<int> rand_gen(
      0, modd); // rand_gen(rng) gets the rand no
  ll infinit = 10000000000000000;

  //    auto start = chrono::steady_clock::now();

  //    readint(test_cases);
  int test_cases = 1;
  forr(t, 1, test_cases) {
    readint(h);
    readint(w);
    readint(k);
    readint(x1);
    readint(y1);
    readint(x2);
    readint(y2);
    x1--;
    y1--;
    x2--;
    y2--;
    vector<string> grid;
    forr(i, 0, h) {
      readstring(s);
      grid.push_back(s);
    }
    vector<vector<int>> path(h, vector<int>(w, -1));
    path[x1][y1] = 0;
    queue<pair<int, int>> todo;
    todo.push(make_pair(x1, y1));
    int count = 0;
    while (!todo.empty()) {
      //              cout << count++ << "/" << w*h << endl;
      auto current = todo.front();
      todo.pop();
      int x_ = current.first, y_ = current.second;
      int dist = path[x_][y_];

      for (auto dir :
           vector<pair<int, int>>{make_pair(0, 1), make_pair(0, -1),
                                  make_pair(1, 0), make_pair(-1, 0)}) {
        forr(i, 1, k) {
          int x_n = x_ + i * dir.first, y_n = y_ + i * dir.second;
          if ((x_n < 0) || (y_n < 0) || (x_n >= h) || (y_n >= w)) {
            break;
          }
          if (grid[x_n][y_n] != '.') {
            break;
          }
          if (path[x_n][y_n] < 0) {
            todo.push(make_pair(x_n, y_n));
            path[x_n][y_n] = dist + 1;
          }
          if (path[x_n][y_n] < dist + 1) {
            break;
          }
        }
      }
    }

    cout << path[x2][y2] << endl;
  }

  //    auto end = chrono::steady_clock::now();
  //    cout << chrono::duration_cast<chrono::milliseconds>(end - start).count()
  //    << endl;

  return 0;
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <chrono>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define forr(i, start, count) for (int i = (start); i < (start) + (count); ++i)
#define set_map_includes(set, elt) (set.find((elt)) != set.end())
#define readint(i)                                                             \\
  int i;                                                                       \\
  cin >> i
#define readll(i)                                                              \\
  ll i;                                                                        \\
  cin >> i
#define readdouble(i)                                                          \\
  double i;                                                                    \\
  cin >> i
#define readstring(s)                                                          \\
  string s;                                                                    \\
  cin >> s

typedef long long ll;

using namespace std;

int main() {
  cout.precision(17);
  ll modd = 1000 * 1000 * 1000 + 7;
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  uniform_int_distribution<int> rand_gen(
      0, modd); // rand_gen(rng) gets the rand no
  ll infinit = 10000000000000000;

  //    auto start = chrono::steady_clock::now();

  //    readint(test_cases);
  int test_cases = 1;
  forr(t, 1, test_cases) {
    readint(h);
    readint(w);
    readint(k);
    readint(x1);
    readint(y1);
    readint(x2);
    readint(y2);
    x1--;
    y1--;
    x2--;
    y2--;
    vector<string> grid;
    forr(i, 0, h) {
      readstring(s);
      grid.push_back(s);
    }
    vector<vector<int>> path(h, vector<int>(w, -1));
    path[x1][y1] = 0;
    queue<pair<int, int>> todo;
    todo.push(make_pair(x1, y1));
    while (!todo.empty()) {
      auto current = todo.front();
      todo.pop();
      int x_ = current.first, y_ = current.second;
      int dist = path[x_][y_];

      for (auto dir :
           vector<pair<int, int>>{make_pair(0, 1), make_pair(0, -1),
                                  make_pair(1, 0), make_pair(-1, 0)}) {
        forr(i, 1, k) {
          int x_n = x_ + i * dir.first, y_n = y_ + i * dir.second;
          if ((x_n < 0) || (y_n < 0) || (x_n >= h) || (y_n >= w)) {
            break;
          }
          if (grid[x_n][y_n] != '.') {
            break;
          }
          if (path[x_n][y_n] < 0) {
            todo.push(make_pair(x_n, y_n));
            path[x_n][y_n] = dist + 1;
          }
        }
      }
    }

    cout << path[x2][y2] << endl;
  }

  //    auto end = chrono::steady_clock::now();
  //    cout << chrono::duration_cast<chrono::milliseconds>(end - start).count()
  //    << endl;

  return 0;
}