Problem p03792 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

#define TRACE(x) cerr << #x << " = " << x << endl
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define _ << " " <<

#define TRACE(x)

typedef long long ll;
typedef pair<int, int> P;
#define X first
#define Y second

const int MAX = 5;

int n;
char s[MAX][MAX];
int ima[MAX];

void load() {
  scanf("%d", &n);
  REP(i, n) scanf("%s", s[i]);
}

void gen() {
  memset(s, 0, sizeof s);
  REP(i, n) REP(j, n) {
    if (rand() & 1)
      s[i][j] = '#';
    else
      s[i][j] = '.';
  }
}

int brute() {
  int rje = 3 * MAX;
  REP(st, 10000) {
    char tmp[MAX][MAX];
    memcpy(tmp, s, sizeof s);

    for (int br = 0; br < rje; br++) {
      int da = 1;
      REP(i, n) REP(j, n) if (tmp[i][j] != '#') da = 0;
      if (da) {
        rje = min(rje, br);
        break;
      }

      int red = rand() % n, stup = rand() % n;
      vector<int> T;
      REP(j, n) T.push_back(tmp[red][j]);
      REP(i, n) tmp[i][stup] = T[i];
    }
  }

  return rje < 3 * MAX ? rje : -1;
}

int solve() {
  memset(ima, 0, sizeof ima);
  int sum = 0;
  REP(i, n) REP(j, n) if (s[i][j] == '#') {
    ima[j] = 1;
    sum++;
  }

  if (!sum)
    return -1;

  int cijelih = 0;
  REP(j, n) {
    int da = 1;
    REP(i, n)
    if (s[i][j] != '#')
      da = 0;

    cijelih += da;
  }

  int ret = MAX * MAX;

  REP(i, n) {
    int kol = 0;
    REP(j, n) if (s[i][j] == '#') kol++;
    TRACE(i _ kol _ ima[i] _ cijelih);
    ret = min(ret, n - kol + 1 - ima[i] + n - cijelih);
  }

  return ret;
}

int main() {
  // for (;;) {
  //   gen();
  //   TRACE(n);
  //   REP(i, n) TRACE(s[i]);
  //   printf("\n");

  //   TRACE(brute() _ solve());
  //   assert(brute() == solve());
  // }

  load();
  cout << solve() << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

#define TRACE(x) cerr << #x << " = " << x << endl
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define _ << " " <<

#define TRACE(x)

typedef long long ll;
typedef pair<int, int> P;
#define X first
#define Y second

const int MAX = 505;

int n;
char s[MAX][MAX];
int ima[MAX];

void load() {
  scanf("%d", &n);
  REP(i, n) scanf("%s", s[i]);
}

void gen() {
  memset(s, 0, sizeof s);
  REP(i, n) REP(j, n) {
    if (rand() & 1)
      s[i][j] = '#';
    else
      s[i][j] = '.';
  }
}

int brute() {
  int rje = 3 * MAX;
  REP(st, 10000) {
    char tmp[MAX][MAX];
    memcpy(tmp, s, sizeof s);

    for (int br = 0; br < rje; br++) {
      int da = 1;
      REP(i, n) REP(j, n) if (tmp[i][j] != '#') da = 0;
      if (da) {
        rje = min(rje, br);
        break;
      }

      int red = rand() % n, stup = rand() % n;
      vector<int> T;
      REP(j, n) T.push_back(tmp[red][j]);
      REP(i, n) tmp[i][stup] = T[i];
    }
  }

  return rje < 3 * MAX ? rje : -1;
}

int solve() {
  memset(ima, 0, sizeof ima);
  int sum = 0;
  REP(i, n) REP(j, n) if (s[i][j] == '#') {
    ima[j] = 1;
    sum++;
  }

  if (!sum)
    return -1;

  int cijelih = 0;
  REP(j, n) {
    int da = 1;
    REP(i, n)
    if (s[i][j] != '#')
      da = 0;

    cijelih += da;
  }

  int ret = MAX * MAX;

  REP(i, n) {
    int kol = 0;
    REP(j, n) if (s[i][j] == '#') kol++;
    TRACE(i _ kol _ ima[i] _ cijelih);
    ret = min(ret, n - kol + 1 - ima[i] + n - cijelih);
  }

  return ret;
}

int main() {
  // for (;;) {
  //   gen();
  //   TRACE(n);
  //   REP(i, n) TRACE(s[i]);
  //   printf("\n");

  //   TRACE(brute() _ solve());
  //   assert(brute() == solve());
  // }

  load();
  cout << solve() << endl;

  return 0;
}