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

#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vec;
typedef vector<vec> mat;
int dy[] = {-1, 0, 1, 0};
int dx[] = {0, 1, 0, -1};
int N, M;
mat mazes[1000];

void rotate(mat &t) {
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N / 2; j++)
      swap(t[i][j], t[i][N - 1 - j]);
  for (int i = 0; i < N; i++)
    for (int j = 0; j < i; j++)
      swap(t[i][j], t[j][i]);
}

void dfs(vec &t, mat &m, mat &d, int y, int x, int root) {
  d[y][x] = root;
  for (int i = 0; i < 4; i++) {
    int ny = y + dy[i];
    int nx = x + dx[i];
    if (ny < 0 || nx < 0 || ny >= N || nx >= N)
      continue;
    if (d[ny][nx] != -1 || m[ny][nx] == 1)
      continue;
    dfs(t, m, d, ny, nx, root);
  }

  if (x > 0)
    return;
  if (t[y] == -1)
    return;
  for (int i = 0; i < N; i++) {
    if (t[i] != t[y])
      continue;
    if (d[i][0] != -1)
      continue;
    if (m[i][0] == 1)
      continue;
    dfs(t, m, d, i, 0, root);
  }
}

vec reflesh(vec t) {
  vec res = t;

  map<int, int> mp;
  for (int i = 0; i < N; i++) {
    if (res[i] == -1)
      continue;
    if (mp.count(res[i])) {
      res[i] = mp[res[i]];
    } else if (res[i] == 0) {

    } else {
      mp[res[i]] = 0;
      int f = mp.size();
      mp[res[i]] = f;
      res[i] = f;
    }
  }
  return res;
}

vec merge(vec t, mat m) {
  mat d(N, vec(N, -1));
  for (int si = 0; si < N; si++) {
    if (d[si][0] != -1)
      continue;
    if (m[si][0] == 1)
      continue;
    if (t[si] == -1)
      continue;
    if (t[si] != 0)
      continue;
    dfs(t, m, d, si, 0, t[si]);
  }

  for (int si = 0; si < N; si++) {
    if (d[si][N - 1] != -1)
      continue;
    if (m[si][N - 1] == 1)
      continue;
    dfs(t, m, d, si, N - 1, N + si);
  }

  vec res(N, -1);
  for (int i = 0; i < N; i++) {
    res[i] = d[i][N - 1];
  }
  res = reflesh(res);
  return res;
}

bool solve() {
  set<vec> dp;
  dp.insert(vec(N, 0));
  for (int i = 0; i < M; i++) {
    set<vec> nex;

    mat edge = mazes[i];
    for (int i = 0; i < 4; i++) {
      rotate(edge);
      for (vec v : dp) {
        nex.insert(merge(v, edge));
      }
    }

    dp.clear();
    for (vec v : nex) {
      dp.insert(v);
    }
  }

  for (const vec &v : dp)
    for (int i = 0; i < (int)v.size(); i++)
      if (v[i] == 0)
        return true;
  return false;
}

void input(int id) {
  mazes[id].clear();
  mat A(N, vec(N, 0));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      char ch;
      cin >> ch;
      A[i][j] = (ch == '#' ? 1 : 0);
    }
  }
  mazes[id] = A;
}

int main() {
  while (1) {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    for (int i = 0; i < M; i++) {
      input(i);
    }
    cout << (solve() ? "Yes" : "No") << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vec;
typedef vector<vec> mat;
int dy[] = {-1, 0, 1, 0};
int dx[] = {0, 1, 0, -1};
int N, M;
mat mazes[1000];

void rotate(mat &t) {
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N / 2; j++)
      swap(t[i][j], t[i][N - 1 - j]);
  for (int i = 0; i < N; i++)
    for (int j = 0; j < i; j++)
      swap(t[i][j], t[j][i]);
}

void dfs(mat &m, mat &d, int y, int x, int root) {
  d[y][x] = root;
  for (int i = 0; i < 4; i++) {
    int ny = y + dy[i];
    int nx = x + dx[i];
    if (ny < 0 || nx < 0 || ny >= N || nx >= N)
      continue;
    if (d[ny][nx] != -1 || m[ny][nx] == 1)
      continue;
    dfs(m, d, ny, nx, root);
  }
}

vec reflesh(vec t) {
  vec res = t;
  map<int, int> mp;
  for (int i = 0; i < N; i++) {
    if (res[i] == -1)
      continue;
    if (mp.count(res[i])) {
      res[i] = mp[res[i]];
    } else {
      int f = mp.size();
      mp[res[i]] = f;
      res[i] = f;
    }
  }
  return res;
}

vec merge(vec t, mat m) {
  mat d(N, vec(N, -1));
  for (int si = 0; si < N; si++) {
    if (d[si][0] != -1)
      continue;
    if (m[si][0] == 1)
      continue;
    if (t[si] == -1)
      continue;
    dfs(m, d, si, 0, t[si]);
  }
  vec res(N, -1);
  for (int i = 0; i < N; i++) {
    if (d[i][N - 1] == -1)
      continue;
    res[i] = d[i][N - 1];
  }
  res = reflesh(res);
  return res;
}

bool solve() {
  set<vec> dp;
  dp.insert(vec(N, 0));
  for (int i = 0; i < M; i++) {
    set<vec> nex;

    mat edge = mazes[i];
    for (int i = 0; i < 4; i++) {
      rotate(edge);
      for (vec v : dp) {
        nex.insert(merge(v, edge));
      }
    }
    dp = nex;
  }

  for (const vec &v : dp)
    for (int i = 0; i < (int)v.size(); i++)
      if (v[i] != -1)
        return true;

  return false;
}

void input(int id) {
  mat A(N, vec(N, 0));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      char ch;
      cin >> ch;
      A[i][j] = (ch == '#' ? 1 : 0);
    }
  }
  mazes[id] = A;
}

int main() {

  while (1) {
    cin >> N >> M;
    if (N == 0 && M == 0)
      break;
    for (int i = 0; i < M; i++) {
      input(i);
    }
    cout << (solve() ? "Yes" : "No") << endl;
  }
  return 0;
}