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

#include <iostream>
#include <vector>
using namespace std;

#define MAX_N 40
#define MAX_C 27
#define INF 10000

int A[MAX_N][MAX_N];
int X[MAX_N][MAX_N];
int Y[MAX_N][MAX_N];
int Z[MAX_N][MAX_N];
int H, W, N;
char C;
char T[MAX_C + 1] = ".ABCDEFGHIJKLMNOPQRSTUVWXYZ";

void _memset() {
  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_N; j++) {
      A[i][j] = 0;
      X[i][j] = 0;
      Y[i][j] = 0;
      Z[i][j] = 0;
    }
  }
}

bool keisan() {
  // jumbi.
  vector<int> U;
  for (int j = 0; j < W; j++) {
    U.clear();
    for (int i = H - 1; i >= 0; i--) {
      if (X[i][j] >= 1) {
        U.push_back(X[i][j]);
      }
    }

    for (int i = 0; i <= H - 1; i++) {
      X[i][j] = 0;
    }
    int pos = U.size();
    for (int i = H - 1; i >= H - pos; i--) {
      int cnt3 = (H - 1) - i;
      X[i][j] = U[cnt3];
    }
  }

  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_N; j++) {
      Z[i][j] = X[i][j];
    }
  }
  int cnt = INF;

  while (cnt > 0) {
    cnt = 0;
    for (int i = 0; i < MAX_N; i++) {
      for (int j = 0; j < MAX_N; j++) {
        Y[i][j] = 0;
      }
    }
    // tate.
    int chain = 1;
    for (int j = 0; j < W; j++) {
      chain = 1;
      for (int i = 1; i < H; i++) {
        if (Z[i][j] == Z[i - 1][j] && Z[i][j] >= 1 && Z[i - 1][j] >= 1) {
          chain++;
        } else {
          chain = 1;
        }
        if (chain == N) {
          for (int k = i; k > i - chain; k--) {
            Y[k][j] = 1;
            cnt++;
          }
        } else if (chain >= N + 1) {
          Y[i][j] = 1;
          cnt++;
        }
      }
    }
    chain = 1;
    // yoko.
    for (int i = 0; i < H; i++) {
      chain = 1;
      for (int j = 1; j < W; j++) {
        if (Z[i][j] == Z[i][j - 1] && Z[i][j] >= 1 && Z[i][j - 1] >= 1) {
          chain++;
        } else {
          chain = 1;
        }
        if (chain == N) {
          for (int k = j; k > j - chain; k--) {
            Y[i][k] = 1;
            cnt++;
          }
        } else if (chain >= N + 1) {
          Y[i][j] = 1;
          cnt++;
        }
      }
    }

    // koushin.
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (Y[i][j] == 1) {
          Z[i][j] = 0;
        }
      }
    }
    vector<int> V;
    for (int j = 0; j < W; j++) {
      V.clear();
      for (int i = H - 1; i >= 0; i--) {
        if (Z[i][j] >= 1) {
          V.push_back(Z[i][j]);
        }
      }
      for (int i = 0; i <= H - 1; i++) {
        Z[i][j] = 0;
      }
      int pos = V.size();
      for (int i = H - 1; i >= H - pos; i--) {
        int cnt2 = (H - 1) - i;
        Z[i][j] = V[cnt2];
      }
    }
  }

  bool res2 = true;
  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_N; j++) {
      if (Z[i][j] >= 1) {
        res2 = false;
      }
    }
  }
  return res2;
}

int main() {
  _memset();
  cin >> H >> W >> N;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> C;
      for (int k = 0; k < MAX_C; k++) {
        if (C == T[k]) {
          A[i][j] = k;
        }
      }
    }
  }

  bool res = false;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (j - 1 >= 0) {
        for (int k = 0; k < MAX_N; k++) {
          for (int l = 0; l < MAX_N; l++) {
            X[k][l] = A[k][l];
          }
        }
        int p1 = X[i][j];
        int p2 = X[i][j - 1];

        X[i][j] = p2;
        X[i][j - 1] = p1;
        bool Q = keisan();
        if (Q == true) {
          res = true;
        }
        X[i][j] = p1;
        X[i][j - 1] = p2;
      }
      if (j + 1 < W) {
        for (int k = 0; k < MAX_N; k++) {
          for (int l = 0; l < MAX_N; l++) {
            X[k][l] = A[k][l];
          }
        }
        int p1 = X[i][j];
        int p2 = X[i][j + 1];

        X[i][j] = p2;
        X[i][j + 1] = p1;
        bool Q = keisan();
        if (Q == true) {
          res = true;
        }
        X[i][j] = p1;
        X[i][j + 1] = p2;
      }
    }
  }

  if (res == true) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

#define MAX_N 40
#define MAX_C 27
#define INF 10000

int X[MAX_N][MAX_N];
int Y[MAX_N][MAX_N];
int Z[MAX_N][MAX_N];
int H, W, N;
char C;
char T[MAX_C + 1] = ".ABCDEFGHIJKLMNOPQRSTUVWXYZ";

void _memset() {
  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_N; j++) {
      X[i][j] = 0;
      Y[i][j] = 0;
      Z[i][j] = 0;
    }
  }
}

bool keisan() {
  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_N; j++) {
      Z[i][j] = X[i][j];
    }
  }
  int cnt = INF;
  while (cnt > 0) {
    cnt = 0;
    for (int i = 0; i < MAX_N; i++) {
      for (int j = 0; j < MAX_N; j++) {
        Y[i][j] = 0;
      }
    }
    // tate.
    int chain = 1;
    for (int j = 0; j < W; j++) {
      chain = 1;
      for (int i = 1; i < H; i++) {
        if (Z[i][j] == Z[i - 1][j] && Z[i][j] >= 1 && Z[i - 1][j] >= 1) {
          chain++;
        } else {
          chain = 1;
        }
        if (chain == N) {
          for (int k = i; k > i - chain; k--) {
            Y[k][j] = 1;
            cnt++;
          }
        } else if (chain >= N + 1) {
          Y[i][j] = 1;
          cnt++;
        }
      }
    }
    chain = 1;
    // yoko.
    for (int i = 0; i < H; i++) {
      chain = 1;
      for (int j = 1; j < W; j++) {
        if (Z[i][j] == Z[i][j - 1] && Z[i][j] >= 1 && Z[i][j - 1] >= 1) {
          chain++;
        } else {
          chain = 1;
        }
        if (chain == N) {
          for (int k = j; k > j - chain; k--) {
            Y[i][k] = 1;
            cnt++;
          }
        } else if (chain >= N + 1) {
          Y[i][j] = 1;
          cnt++;
        }
      }
    }

    // koushin.
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (Y[i][j] == 1) {
          Z[i][j] = 0;
        }
      }
    }
    vector<int> V;
    for (int j = 0; j < W; j++) {
      V.clear();
      for (int i = H - 1; i >= 0; i--) {
        if (Z[i][j] >= 1) {
          V.push_back(Z[i][j]);
        }
      }
      for (int i = 0; i <= H - 1; i++) {
        Z[i][j] = 0;
      }
      for (int i = H - 1; i >= H - V.size(); i--) {
        int cnt2 = (H - 1) - i;
        Z[i][j] = V[cnt2];
      }
    }
  }

  bool res2 = true;
  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_N; j++) {
      if (Z[i][j] >= 1) {
        res2 = false;
      }
    }
  }
  return res2;
}

int main() {
  _memset();
  cin >> H >> W >> N;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> C;
      for (int k = 0; k < MAX_C; k++) {
        if (C == T[k]) {
          X[i][j] = k;
        }
      }
    }
  }

  bool res = false;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {

      if (X[i][j] != 0 && j + 1 < W) {
        if (X[i][j + 1] != 0) {
          int p1 = X[i][j];
          int p2 = X[i][j + 1];
          X[i][j] = p2;
          X[i][j + 1] = p1;
          bool p3 = keisan();
          if (p3 == true) {
            res = true;
          }
          X[i][j] = p1;
          X[i][j + 1] = p2;
        }
      }

      if (X[i][j] != 0 && j - 1 >= 0) {
        if (X[i][j - 1] != 0) {
          int p1 = X[i][j];
          int p2 = X[i][j - 1];
          X[i][j] = p2;
          X[i][j - 1] = p1;
          bool p3 = keisan();
          if (p3 == true) {
            res = true;
          }
          X[i][j] = p1;
          X[i][j - 1] = p2;
        }
      }
    }
  }

  if (res == true) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
  return 0;
}