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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

int n, h, w;

string s[30];

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

void print() {
  REP(i, h) {
    REP(j, w) { cout << s[i][j] << " "; }
    cout << endl;
  }
}

bool solve() {
  bool F = 1;
  string table[30];
  REP(i, h) table[i] = s[i];
  REP(j, w) {
    string t = "";
    for (int i = h - 1; i >= 0; i--) {
      if (table[i][j] != '.')
        t += table[i][j];
    }
    for (int i = h - 1; i >= 0; i--) {
      if (h - 1 - i < t.size())
        table[i][j] = t[h - 1 - i];
      else
        table[i][j] = '.';
    }
  }
  while (F) {
    bool f[30][30] = {};
    REP(i, h) {
      REP(j, w) {
        char c = table[i][j];
        if (c == '.')
          continue;
        REP(k, 4) {
          bool g = 1;
          REP(l, n) {
            int ny = i + dy[k] * l, nx = j + dx[k] * l;
            if (ny >= 0 && ny < h && nx >= 0 && nx < w) {
              if (c != table[ny][nx])
                g = 0;
            } else
              g = 0;
          }
          if (g) {
            REP(l, n) { f[i + dy[k] * l][j + dx[k] * l] = 1; }
          }
        }
      }
    }

    F = 0;
    REP(i, h) {
      REP(j, w) {
        if (f[i][j]) {
          F = 1;
          table[i][j] = '.';
        }
      }
    }

    REP(j, w) {
      string t = "";
      for (int i = h - 1; i >= 0; i--) {
        if (table[i][j] != '.')
          t += table[i][j];
      }
      for (int i = h - 1; i >= 0; i--) {
        if (h - 1 - i < t.size())
          table[i][j] = t[h - 1 - i];
        else
          table[i][j] = '.';
      }
    }
    // REP(i, h) {
    //	REP(j, w) {
    //		cout << table[i][j] << " ";
    //	}
    //	cout << endl;
    // }
    // cout << endl;
  }
  bool res = 1;
  REP(i, h) {
    REP(j, w) {
      if (table[i][j] != '.')
        res = 0;
    }
  }
  return res;
}

int main() {
  cin >> h >> w >> n;
  REP(i, h) cin >> s[i];
  bool ok = 0;
  REP(i, h) {
    REP(j, w - 1) {
      swap(s[i][j], s[i][j + 1]);
      // cout << "!" << endl;
      // print();
      // cout << endl;
      ok |= solve();
      // cout << endl;
      swap(s[i][j], s[i][j + 1]);
    }
  }
  REP(i, h - 1) {
    REP(j, w) {
      swap(s[i][j], s[i][j + 1]);
      // cout << "!" << endl;
      // print();
      // cout << endl;
      ok |= solve();
      // cout << endl;
      swap(s[i][j], s[i][j + 1]);
    }
  }
  if (ok)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, N) for (int i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define EQV(a, b) (EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()))
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

int n, h, w;

string s[30];

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

void print() {
  REP(i, h) {
    REP(j, w) { cout << s[i][j] << " "; }
    cout << endl;
  }
}

bool solve() {
  bool F = 1;
  string table[30];
  REP(i, h) table[i] = s[i];
  while (F) {
    bool f[30][30] = {};
    REP(i, h) {
      REP(j, w) {
        char c = table[i][j];
        if (c == '.')
          continue;
        REP(k, 4) {
          bool g = 1;
          REP(l, n) {
            int ny = i + dy[k] * l, nx = j + dx[k] * l;
            if (ny >= 0 && ny < h && nx >= 0 && nx < w) {
              if (c != table[ny][nx])
                g = 0;
            } else
              g = 0;
          }
          if (g) {
            FOR(l, 1, n + 1) { f[i + dy[k] * l][j + dx[k] * l] = 1; }
          }
        }
      }
    }

    F = 0;
    REP(i, h) {
      REP(j, w) {
        if (f[i][j]) {
          F = 1;
          table[i][j] = '.';
        }
      }
    }

    REP(j, w) {
      string t = "";
      for (int i = h - 1; i >= 0; i--) {
        if (table[i][j] != '.')
          t += table[i][j];
      }
      for (int i = h - 1; i >= 0; i--) {
        if (h - 1 - i < t.size())
          table[i][j] = t[h - 1 - i];
        else
          table[i][j] = '.';
      }
    }
    // REP(i, h) {
    //	REP(j, w) {
    //		cout << table[i][j] << " ";
    //	}
    //	cout << endl;
    // }
  }
  bool res = 1;
  REP(i, h) {
    REP(j, w) {
      if (table[i][j] != '.')
        res = 0;
    }
  }
  return res;
}

int main() {
  cin >> h >> w >> n;
  REP(i, h) cin >> s[i];
  bool ok = 0;
  REP(i, h) {
    REP(j, w - 1) {
      swap(s[i][j], s[i][j + 1]);
      // cout << "!" << endl;
      // print();
      ok |= solve();
      // cout << endl;
      swap(s[i][j], s[i][j + 1]);
    }
  }
  REP(i, h - 1) {
    REP(j, w) {
      swap(s[i][j], s[i][j + 1]);
      /*cout << "!" << endl;
      print();*/
      ok |= solve();
      // cout << endl;
      swap(s[i][j], s[i][j + 1]);
    }
  }
  if (ok)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}