- 拼图
- 2009-03-08 14:47:26 @
var
ch:char;
sum,n,m,i,j,k,k1,k2:longint;
g:array[1..125,1..125]of integer;
f:array[1..5,1..5,1..5]of boolean;
up,f1,f2:array[1..5]of integer;
vis:array[1..5]of boolean;
left,right1,right2,max1,max2:array[1..5,1..5]of integer;
procedure out1;
begin writeln('No solution possible');halt;end;
procedure out2;
var j,k:longint;
begin
for j:=1 to n do
begin
for k:=1 to n do
write(g[j,k]);
writeln;
end;
halt;
end;
function check(i,j:integer):boolean;
var k1,k2,l,o:integer;
begin
o:=right1[j,1];
for k1:=2 to f1[i] do
if right1[j,k1]>o then o:=right1[j,k1];
if up[i]+o-right1[j,1]+max1[j,1]-1>n then exit(false);
o:=right2[j,1];
for k2:=1 to f2[i] do
if right2[j,k2]>o then o:=right2[j,k2];
if i+o-right2[j,left[j,1]]+max2[j,left[j,1]]-1>n then exit(false);
l:=left[j,1];o:=up[i];
for k1:=1 to f1[j] do
for k2:=1 to f2[j] do
if f[j,k1,k2]
then
if g0
then exit(false);
exit(true);
end;
procedure dfs(i:integer);
var j,k1,k2,l,o:integer;
down:array[1..5]of integer;
begin
if i=n+1 then out2;
for j:=1 to m do
if not vis[j]
then
if check(i,j)
then
begin
l:=left[j,1];o:=up[i];
down:=up;
for k1:=1 to f1[j] do
for k2:=1 to f2[j] do
if f[j,k1,k2]
then
g:=j;
vis[j]:=true;
k1:=i;
for k1:=i to n do
for k2:=up[k1] to n+1 do
if g[k1,k2]=0 then
begin
up[k1]:=k2;break;
end;
k1:=i;
while up[k1]=n+1 do inc(k1);
dfs(k1);
vis[j]:=false;
up:=down;
l:=left[j,1];o:=up[i];
for k1:=1 to f1[j] do
for k2:=1 to f2[j] do
if f[j,k1,k2]
then
g:=0;
end;
end;
begin
readln(m);
sum:=0;
for i:=1 to m do
begin
readln(f1[i],f2[i]);
for j:=1 to f1[i] do
begin
for k:=1 to f2[i] do
begin
read(ch);
if ch='1'
then
begin
f:=true;
inc(sum);
right1:=k;
if left=0
then left:=k;
end;
end;
readln;
end;
end;
for i:=1 to m do
for j:=1 to f2[i] do
begin
k:=f1[i];
while not f do dec(k);
right2:=k;
end;
n:=trunc(sqrt(sum));
if sum mod n0 then out1;
for i:=1 to m do
begin
for j:=1 to f1[i] do
begin
for k1:=1 to f2[i] do
if f then break;
for k2:=f2[i] downto 1 do
if f then break;
max1:=k2-k1;
if max1>n then out1;
end;
for j:=1 to f2[i] do
begin
for k1:=1 to f1[i] do
if f then break;
for k2:=f1[i] downto 1 do
if f then break;
max2:=k2-k1;
if max2>n then out1;
end;
end;
fillchar(vis,sizeof(vis),false);
for i:=1 to m do
begin
vis[i]:=true;
for j:=1 to f1[i] do
for k:=1 to f2[i] do
if f
then g[j,k]:=i;
for j:=1 to n do
for k:=1 to n do
if g[j,k]=0 then begin up[j]:=k;break;end;
j:=1;
while up[j]=0 do inc(j);
dfs(j);
vis[i]:=false;
for j:=1 to f1[i] do
for k:=1 to f2[i] do
g[j,k]:=0;
for j:=1 to f1[i] do
up[j]:=0;
end;
out1;
end.