虽说是简单题,但是处理花了很长时间,调试也花了很长时间。代码写的挫。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 30
struct piece
{
char map[N][N];
int u, l, b, r;
}data[105];
int seq[105], top;
bool mk[105];
bool Out(int num, const int d, const int n)
{
if(num == 1)
{
for(int i = 1; i <= n; i++)
{
if(mk[i]) continue;
if(!data[i].u && !data[i].l)
{
seq[top++] = i;
mk[i] = 1;
if(Out(num + 1, d, n)) return 1;
top--;
mk[i] = 0;
}
}
}
else if(num == d)
{
for(int i = 1; i <= n; i++)
{
if(mk[i]) continue;
if(!data[i].u && !data[i].r)
{
seq[top++] = i;
mk[i] = 1;
if(Out(num + 1, d, n)) return 1;
top--;
mk[i] = 0;
}
}
}
else if(num == d * d - d + 1)
{
for(int i = 1; i <= n; i++)
{
if(mk[i]) continue;
if(!data[i].l && !data[i].b)
{
seq[top++] = i;
mk[i] = 1;
if(Out(num + 1, d, n)) return 1;
top--;
mk[i] = 0;
}
}
}
else if(num == d * d)
{
for(int i = 1; i <= n; i++)
{
if(mk[i]) continue;
if(!data[i].b && !data[i].r)
{
seq[top++] = i;
mk[i] = 1;
return 1;
}
}
}
else
{
for(int i = 1; i <= n; i++)
{
if(mk[i]) continue;
mk[i] = 1;
if(num < d)
{
if(!data[i].u && data[i].l == -data[seq[num - 2]].r)
{
seq[top++] = i;
if(Out(num + 1, d, n)) return 1;
top--;
}
}
else if(num % d == 1)
{
if(!data[i].l && data[i].u == -data[seq[num - d - 1]].b)
{
seq[top++] = i;
if(Out(num + 1, d, n)) return 1;
top--;
}
}
else if(num % d == 0)
{
if(!data[i].r && data[i].u == -data[seq[num - d - 1]].b && data[i].l == -data[seq[num - 2]].r)
{
seq[top++] = i;
if(Out(num + 1, d, n)) return 1;
top--;
}
}
else if(num > d * d - d + 1)
{
if(!data[i].b && data[i].u == -data[seq[num - d - 1]].b && data[i].l == -data[seq[num - 2]].r)
{
seq[top++] = i;
if(Out(num + 1, d, n)) return 1;
top--;
}
}
else
{
if(data[i].u == -data[seq[num - d - 1]].b && data[i].l == -data[seq[num - 2]].r)
{
seq[top++] = i;
if(Out(num + 1, d, n)) return 1;
top--;
}
}
mk[i] = 0;
}
}
return 0;
}
int main()
{
// freopen("out.txt", "w", stdout);
int t, d, h, w;
char tmp[N];
scanf("%d", &t);
while(t--)
{
top = 0;
memset(mk, 0, sizeof(mk));
scanf("%d %d %d", &d, &h, &w);
for(int k = 1; k <= d * d; k++)
{
gets(tmp);
for(int i = 0; i < h; i++)
{
for(int j = 0; j < w; j++) data[k].map[i][j] = getchar();
data[k].map[i][w] = '\0';
gets(tmp);
}
scanf("%d %d %d %d", &data[k].u, &data[k].l, &data[k].b, &data[k].r);
gets(tmp);
}
Out(1, d, d * d);
// for(int i = 0; i < top; i++) printf("%d\n", seq[i]);
for(int i = 0; i < h * d; i++)
{
for(int j = 0; j < w * d; j++)
{
int num = (i / h) * d + j / w;
putchar(data[seq[num]].map[i % h][j % w]);
}
puts("");
}
if(t) puts("");
}
return 0;
}