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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int INF = 1e9;
const ll LINF = 1e18;
const int mod = 1e9 + 7;

#define pi pair<int, int>
#define pl pair<lld, lld>

#define dump(x) cout << #x << " = " << (x) << endl
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define SANKOU(n, a, b) cout << ((n) ? (#a) : (#b)) << endl

#define mem0(x) memset(x, 0, sizeof(x))
#define fillnum(x, n) fill(begin(x), end(x), n)
#define asort(x) sort(x.begin(), x.end())
#define dsort(x, t) sort(x.begin(), x.end(), greater<t>())
#define vuniq(x) x.erase(unique(x.begin(), x.end()), x.end())

#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)

int H, W;
int Ch, Cw, Dh, Dw;
char mas[1010][1010];
int flg[1010][1010];
vector<vector<pi>> Node;
int start_num = -1;
int goal_num = -1;
int node_num = 0;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};

void create_Node(int x, int y, int num) {
  if (x == Cw && y == Ch) {
    start_num = num;
  }
  if (x == Dw && y == Dh) {
    goal_num = num;
  }
  flg[y][x] = num;
  Node[num].pb(mp(x, y));
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx >= 0 && nx < W && ny >= 0 && ny < H && mas[ny][nx] == '.' &&
        flg[ny][nx] == -1) {
      create_Node(nx, ny, num);
    }
  }
}

int main() {
  cin >> H >> W;
  cin >> Ch >> Cw;
  cin >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> mas[i][j];
      flg[i][j] = -1;
    }
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (mas[i][j] == '.' && flg[i][j] == -1) {
        Node.pb(vector<pi>());
        create_Node(j, i, node_num);
        node_num++;
      }
    }
  }

  vector<int> edge[node_num];

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (flg[i][j] != -1) {
        for (int k = -2; k <= 2; k++) {
          for (int l = -2; l <= 2; l++) {
            int nx = j + l;
            int ny = i + k;
            if (nx >= 0 && nx < W && ny >= 0 && ny < H && flg[ny][nx] != -1) {
              edge[flg[i][j]].pb(flg[ny][nx]);
            }
          }
        }
      }
    }
  }

  for (int i = 0; i < node_num; i++) {
    vuniq(edge[i]);
  }

  bool f[node_num];
  mem0(f);
  queue<pi> que;
  que.push(mp(0, start_num));
  f[start_num] = true;
  while (!que.empty()) {
    pi n = que.front();
    que.pop();
    if (n.second == goal_num) {
      cout << n.first << endl;
      exit(0);
    }
    for (int i = 0; i < edge[n.second].size(); i++) {
      if (f[edge[n.second][i]] == false) {
        f[edge[n.second][i]] = true;
        que.push(mp(n.first + 1, edge[n.second][i]));
      }
    }
  }
  cout << "-1" << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int INF = 1e9;
const ll LINF = 1e18;
const int mod = 1e9 + 7;

#define pi pair<int, int>
#define pl pair<lld, lld>

#define dump(x) cout << #x << " = " << (x) << endl
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define SANKOU(n, a, b) cout << ((n) ? (#a) : (#b)) << endl

#define mem0(x) memset(x, 0, sizeof(x))
#define fillnum(x, n) fill(begin(x), end(x), n)
#define asort(x) sort(x.begin(), x.end())
#define dsort(x, t) sort(x.begin(), x.end(), greater<t>())
#define vuniq(x) x.erase(unique(x.begin(), x.end()), x.end())

#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)

int H, W;
int Ch, Cw, Dh, Dw;
char mas[1010][1010];
int flg[1010][1010];
vector<vector<pi>> Node;
int start_num = -1;
int goal_num = -1;
int node_num = 0;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};

void create_Node(int x, int y, int num) {
  if (x == Cw && y == Ch) {
    start_num = num;
  }
  if (x == Dw && y == Dh) {
    goal_num = num;
  }
  flg[y][x] = num;
  Node[num].pb(mp(x, y));
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx >= 0 && nx < W && ny >= 0 && ny < H && mas[ny][nx] == '.' &&
        flg[ny][nx] == -1) {
      create_Node(nx, ny, num);
    }
  }
}

int main() {
  cin >> H >> W;
  cin >> Ch >> Cw;
  cin >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> mas[i][j];
      flg[i][j] = -1;
    }
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (mas[i][j] == '.' && flg[i][j] == -1) {
        Node.pb(vector<pi>());
        create_Node(j, i, node_num);
        node_num++;
      }
    }
  }

  vector<int> edge[node_num];
  for (int i = 0; i < node_num; i++) {
    for (int j = i + 1; j < node_num; j++) {
      for (int k = 0; k < Node[i].size(); k++) {
        for (int l = 0; l < Node[j].size(); l++) {
          if (abs(Node[i][k].first - Node[j][l].first) <= 2 &&
              abs(Node[i][k].second - Node[j][l].second) <= 2) {
            edge[i].pb(j);
            edge[j].pb(i);
            k = Node[i].size();
            l = Node[j].size();
          }
        }
      }
    }
  }

  bool f[node_num];
  mem0(f);
  queue<pi> que;
  que.push(mp(0, start_num));
  f[start_num] = true;
  while (!que.empty()) {
    pi n = que.front();
    que.pop();
    if (n.second == goal_num) {
      cout << n.first << endl;
      exit(0);
    }
    for (int i = 0; i < edge[n.second].size(); i++) {
      if (f[edge[n.second][i]] == false) {
        que.push(mp(n.first + 1, edge[n.second][i]));
      }
    }
  }
  cout << "-1" << endl;
}