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

#include <bits/stdc++.h>

using namespace std;

#define LL long long
#define ULL unsigned LL
#define LD long double

#define pi pair<int, int>

const int N = 1e3 + 10;
string s[N];
int h, w;
const int INF = 1e9 + 2137;
unordered_map<int, int> dist;

int koszt(pi ruch) {
  if (ruch == make_pair(-1, 0) || ruch == make_pair(1, 0) ||
      ruch == make_pair(0, -1) || ruch == make_pair(0, 1))
    return 0;
  else
    return 1;
}

bool check(pi v) {
  if (v.first < 0 || v.second < 0 || v.first >= h || v.second >= w)
    return false;
  if (s[v.first][v.second] == '#')
    return false;
  return true;
}

int create(pi v) {
  int numer = v.first * w + v.second + 1;
  return numer;
}

void djikstra(pi src) {
  set<pair<int, pi>> setds;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      dist[create({i, j})] = INF;
  }
  setds.insert({0, src});
  dist[create(src)] = 0;
  while (!setds.empty()) {
    pair<int, pi> tmp = *(setds.begin());
    setds.erase(setds.begin());
    pi u = tmp.second;

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (i == j && i == 0)
          continue;
        int weight = koszt({i, j});
        pi v = make_pair(u.first + i, u.second + j);
        if (!check(v))
          continue;
        if (dist[create(v)] > dist[create(u)] + weight) {
          if (dist[create(v)] != INF)
            setds.erase(make_pair(dist[create(v)], v));
          dist[create(v)] = dist[create(u)] + weight;
          setds.insert(make_pair(dist[create(v)], v));
        }
      }
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  for (int i = 0; i < h; i++)
    cin >> s[i];
  pi start = make_pair(ch - 1, cw - 1);
  pi meta = make_pair(dh - 1, dw - 1);
  djikstra(start);
  int wynik = -1;
  if (dist[create(meta)] != INF)
    wynik = dist[create(meta)];
  cout << wynik;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define LL long long
#define ULL unsigned LL
#define LD long double

#define pi pair<int, int>

const int N = 1e3 + 10;
string s[N];
int h, w;
const int INF = 1e9 + 2137;
map<pi, int> dist;

int koszt(pi ruch) {
  if (ruch == make_pair(-1, 0) || ruch == make_pair(1, 0) ||
      ruch == make_pair(0, -1) || ruch == make_pair(0, 1))
    return 0;
  else
    return 1;
}

bool check(pi v) {
  if (v.first < 0 || v.second < 0 || v.first >= h || v.second >= w)
    return false;
  if (s[v.first][v.second] == '#')
    return false;
  return true;
}

void djikstra(pi src) {
  set<pair<int, pi>> setds;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++)
      dist[{i, j}] = INF;
  }
  setds.insert({0, src});
  dist[src] = 0;
  while (!setds.empty()) {
    pair<int, pi> tmp = *(setds.begin());
    setds.erase(setds.begin());
    pi u = tmp.second;

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (i == j && i == 0)
          continue;
        int weight = koszt({i, j});
        pi v = make_pair(u.first + i, u.second + j);
        if (!check(v))
          continue;
        if (dist[v] > dist[u] + weight) {
          if (dist[v] != INF)
            setds.erase(make_pair(dist[v], v));
          dist[v] = dist[u] + weight;
          setds.insert(make_pair(dist[v], v));
        }
      }
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  for (int i = 0; i < h; i++)
    cin >> s[i];
  pi start = make_pair(ch - 1, cw - 1);
  pi meta = make_pair(dh - 1, dw - 1);
  djikstra(start);
  int wynik = -1;
  if (dist[meta] != INF)
    wynik = dist[meta];
  cout << wynik;
}