Sgu262: Symbol Recognition (pressure DP)
Question:
Given K Items N? M Of 01 Matrix Si , Find 1 As few as possible N? M Of 01 Matrix P , Meet K The interaction between a matrix and the matrix is different, that is, the matrix can represent the given K Matrix.
Analysis:
There are several pressure issues. DP Let's talk about it here.
Assume that Si And P Is defined Qi , Whose ID is Ti In the initial stage, the intersection is empty, that is T1 = T2 =... = Tk = 0, Q1 = Q2 =... = Qk =? .
Enumeration P Current Grid (X, y) , Suppose there are Si, x, y = Sj, x, y = 1 , And Qi = Qj , Then the current grid is placed 1 Then, the new Qi Still equal Qj And vice versa. Therefore, you can use the smallest representation to create the grid. 1 After T . However T Is the whole pressure DP . When Max {Ti} = k? 1 To prove all Ti Are not the same, that is, all Qi Are not the same. P That is, the request.
AC code:
#include
#include
#include
#define pii pair
#define mp make_pairusing namespace std;const int MAXK = 7;const int MAXN = 11;const int MAXS = 46660;const int INF = 0x3f;int n, m, k, s;int g[MAXK][MAXN][MAXN];int col[MAXN][MAXN];int f[MAXS], fr[MAXS], fx[MAXS], fy[MAXS];map
S;int ans[MAXN][MAXN];void upd(int s, int x, int y){ int ts = s, add = f[s], tot = 0, ns = 0; pii sta[MAXK];S.clear(); for(int i = 0, j = 1; i < k; ++i, ts /= k, j *= k) { sta[i] = mp(ts%k, g[i][x][y]); if(!S.count(sta[i])) S[sta[i]] = tot++; ns += S[sta[i]]*j; } if(f[ns] > add+1) { f[ns] = add+1, fr[ns] = s; fx[ns] = x, fy[ns] = y; }}int main(){ #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif scanf("%d%d%d", &n, &m, &k); for(int i = 0; i < k; ++i) for(int p = 0; p < n; ++p) for(int q = 0; q < m; ++q) { scanf("%1d", &g[i][p][q]); col[p][q] |= g[i][p][q]; } s = 1; for(int i = 0; i < k; ++i) s *= k; memset(f, INF, sizeof f); f[0] = 0; for(int i = 0; i < n; ++i) for(int j = 0; j < m; ++j) if(col[i][j]) for(int p = 0; p < s; ++p) if(f[p] < INF) upd(p, i, j); int end = 0; for(int i = k-1; i >= 0; --i) end = end*k+i; printf("%d\n", f[end]); int now = end; while(now) { ans[fx[now]][fy[now]] = 1; now = fr[now]; } for(int i = 0; i < n; ++i) { for(int j = 0; j < m; ++j) printf("%d", ans[i][j]); puts(""); } #ifndef ONLINE_JUDGE fclose(stdin); fclose(stdout); #endif return 0;}