Problem p02579 (C++) — buggy submission, verdict: Runtime Error



#include <bits/stdc++.h>
#include <stdio.h>

using namespace std;
typedef long long ll;

#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define mp make_pair
#define pb push_back
#define lp(i, s, f) for (ll i = s; i < ll(f); i++)
#define inF freopen("input.in", "r", stdin);
#define outF freopen("output.in", "w", stdout);
#define MOD ll(1000000007)
#define debug(x) cout << '[' << #x << " is: " << x << "] " << endl;
#define decimalpoint cout << std::fixed << setprecision(5)
char mat[1010][1010];
int component[1010][1010];
int xi, yi, xf, yf;
int n, m;
bool visited[1010][1010];
int now;
map<int, vector<int>> adj;
map<int, bool> visitedcomp;
map<int, int> depth;

int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, -1, 1, 0};
map<pair<int, int>, bool> connection;

void dfs(int x, int y) {
  visited[x][y] = true;
  component[x][y] = now;

  for (int f = 0; f < 4; f++) {
    int i = dx[f];
    int j = dy[f];
    if (x + i >= 0 && x + i < n && y + j >= 0 && y + j < m &&
        visited[x + i][y + j] == false && mat[x + i][y + j] == '.') {
      dfs(x + i, y + j);
    }
  }
}

void solve() {

  cin >> n >> m >> xi >> yi >> xf >> yf;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> mat[i][j];
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (visited[i][j] == false && mat[i][j] == '.') {
        now++;
        dfs(i, j);
      }
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if ((mat[i + 1][j] == '#' || mat[i][j + 1] == '#') && mat[i][j] == '.') {

        for (int f = i - 2; f < i + 3; f++) {
          for (int k = j - 2; k < j + 3; k++) {
            if (f < 0 || k < 0 || f >= n || k >= m)
              continue;
            if (component[f][k] > 0 && component[f][k] != component[i][j] &&
                connection[{component[f][k], component[i][j]}] == false) {
              connection[{component[f][k], component[i][j]}] =
                  connection[{component[i][j], component[f][k]}] = true;
              adj[component[f][k]].push_back(component[i][j]);
              adj[component[i][j]].push_back(component[f][k]);
            }
          }
        }
      }
    }
  }

  int start = component[xi - 1][yi - 1];
  int end = component[xf - 1][yf - 1];

  depth[start] = 1;

  // cout << start << " " << end  << "ff " << endl;

  queue<int> q;

  q.push(start);

  visitedcomp[start] = true;

  while (q.size()) {
    int p = q.front();
    q.pop();

    for (int i : adj[p]) {
      if (visitedcomp[i] == false) {
        visitedcomp[i] = true;
        depth[i] = depth[p] + 1;
        q.push(i);
      }
    }
  }

  cout << depth[end] - 1 << endl;
}

int main() {
  FAST;
  int t;
  t = 1;

  while (t--) {
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:



#include <bits/stdc++.h>
#include <stdio.h>

using namespace std;
typedef long long ll;

#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define mp make_pair
#define pb push_back
#define lp(i, s, f) for (ll i = s; i < ll(f); i++)
#define inF freopen("input.in", "r", stdin);
#define outF freopen("output.in", "w", stdout);
#define MOD ll(1000000007)
#define debug(x) cout << '[' << #x << " is: " << x << "] " << endl;
#define decimalpoint cout << std::fixed << setprecision(5)
char mat[1010][1010];
int component[1010][1010];
int xi, yi, xf, yf;
int n, m;
bool visited[1010][1010];
int now;
map<int, vector<int>> adj;
bool visitedcomp[1010];
int depth[1010];

int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, -1, 1, 0};
map<pair<int, int>, bool> connection;

void dfs(int x, int y) {
  visited[x][y] = true;
  component[x][y] = now;

  for (int f = 0; f < 4; f++) {
    int i = dx[f];
    int j = dy[f];
    if (x + i >= 0 && x + i < n && y + j >= 0 && y + j < m &&
        visited[x + i][y + j] == false && mat[x + i][y + j] == '.') {
      dfs(x + i, y + j);
    }
  }
}

void solve() {

  cin >> n >> m >> xi >> yi >> xf >> yf;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> mat[i][j];
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (visited[i][j] == false && mat[i][j] == '.') {
        now++;
        dfs(i, j);
      }
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if ((mat[i + 1][j] == '#' || mat[i][j + 1] == '#') && mat[i][j] == '.') {

        for (int f = i - 2; f < i + 3; f++) {
          for (int k = j - 2; k < j + 3; k++) {
            if (f < 0 || k < 0 || f >= n || k >= m)
              continue;
            if (component[f][k] > 0 && component[f][k] != component[i][j] &&
                connection[{component[f][k], component[i][j]}] == false) {
              connection[{component[f][k], component[i][j]}] =
                  connection[{component[i][j], component[f][k]}] = true;
              adj[component[f][k]].push_back(component[i][j]);
              adj[component[i][j]].push_back(component[f][k]);
            }
          }
        }
      }
    }
  }

  int start = component[xi - 1][yi - 1];
  int end = component[xf - 1][yf - 1];

  memset(depth, -1, sizeof depth);

  depth[start] = 0;

  // cout << start << " " << end  << "ff " << endl;

  queue<int> q;

  q.push(start);

  visitedcomp[start] = true;

  while (q.size()) {
    int p = q.front();
    q.pop();

    for (int i : adj[p]) {
      if (visitedcomp[i] == false) {
        visitedcomp[i] = true;
        depth[i] = depth[p] + 1;
        q.push(i);
      }
    }
  }

  cout << depth[end] << endl;
}

int main() {
  FAST;
  int t;
  t = 1;

  while (t--) {
    solve();
  }
  return 0;
}