1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| #include <cstdio> #include <cstring> #include <algorithm> using namespace std;
const int maxn = 25; int n, a[5], dt[maxn], ans, cnt;
bool ok(int *p, int k) { for(int i = 0; i < 5; ++i) if(a[i] &(p[i] >> k)) return false; return true; }
void dfs(int *p, int d, int sum) { if(sum + dt[0] * (10 - d) >= ans) return; if(d == 10) {ans = min(ans, sum); return; } for(int i = 0; i < cnt; ++i) { if(ok(p, dt[i])) { int p2[5]; for(int j = 0; j < 5; ++j) p2[j] =(p[j] >> dt[i]) | a[j]; dfs(p2, d+1, sum + dt[i]); } } return; }
int main() { while(~scanf("%d", &n) && n) { memset(a, 0, sizeof(a)); memset(dt, 0, sizeof(dt)); for(int i = 0; i < 5; ++i) { char s[maxn]; scanf("%s", s); for(int j = 0; j < n; ++j) if(s[j] == 'X') a[i] |=(1 << j); } cnt = 0; for(int i = 1; i <= n; ++i) if(ok(a, i)) dt[cnt++]=i; ans = 10 * n; dfs(a, 1, n); printf("%d\n", ans); } return 0; }
|