嘟嘟嘟
一眼看上去像状压dp,然后越想复杂度越不对劲,最后发现和爆搜差不多,索性就写爆搜了,复杂度(O()能过())。
别忘了填拼图和回溯的时候只动拼图中是1的部分,不要把(n * m)的矩形全改了。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 17;
const int N = 4;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
int n;
struct Puzzle
{
int n, m, num;
char a[maxn][maxn];
}t[maxn];
In void calc(int x)
{
for(int i = 1; i <= t[x].n; ++i)
for(int j = 1; j <= t[x].m; ++j)
if(t[x].a[i][j] == '1') ++t[x].num;
}
int f[maxn][maxn], Ans[maxn][maxn], Flg = -1;
In void copy_ans()
{
for(int i = 1; i <= N; ++i)
for(int j = 1; j <= N; ++j) Ans[i][j] = f[i][j];
}
In bool check(int now, int x, int y)
{
for(int i = x; i <= x + t[now].n - 1; ++i)
for(int j = y; j <= y + t[now].m - 1; ++j)
if(f[i][j] && t[now].a[i - x + 1][j - y + 1] == '1') return 0;
return 1;
}
In void copy(int now, int x, int y, bool flg)
{
for(int i = 1; i <= t[now].n; ++i)
for(int j = 1; j <= t[now].m; ++j)
if(t[now].a[i][j] == '1')
f[i + x - 1][j + y - 1] = flg ? now : 0;
}
In void dfs(int now)
{
if(Flg > 0) return;
if(now == n + 1) {++Flg; copy_ans(); return;}
for(int i = 1; i <= N - t[now].n + 1; ++i)
for(int j = 1; j <= N - t[now].m + 1; ++j)
if(check(now, i, j))
{
copy(now, i, j, 1);
dfs(now + 1);
copy(now, i, j, 0);
}
}
int main()
{
while(scanf("%d", &n) != EOF)
{
Flg = -1; Mem(f, 0);
int sum = 0;
for(int i = 1; i <= n; ++i)
{
t[i].n = read(), t[i].m = read(); t[i].num = 0;
for(int j = 1; j <= t[i].n; ++j) scanf("%s", t[i].a[j] + 1);
calc(i); sum += t[i].num;
}
if(sum ^ 16) {puts("No solution"); continue;}
dfs(1);
if(Flg == -1) puts("No solution");
else if(Flg > 0) puts("Yes, many!");
else
{
puts("Yes, only one!");
for(int i = 1; i <= N; ++i)
{
for(int j = 1; j <= N; ++j) write(Ans[i][j]);
enter;
}
}
}
return 0;
}