智慧珠游戏
【问题描述】
智慧珠游戏拼盘由一个三角形盘件和12个形态各异的零件组成。拼盘的盘件如图1所示:
图1 |
12个零件按珠子数分3大类:
第1大类,有三个珠子,只有一种形状。
符号为A,形状为 |
第2大类,有4个珠子,有3种形状。
符号为B,形状为 |
|
符号为C,形状为 |
|
符号为D,形状为 |
第3大类,有5个珠子,有8种形状。
符号为E,形状为 |
|
符号为F,形状为 |
|
符号为G,形状为 |
|
符号为H,形状为 |
|
符号为I,形状为 |
|
符号为J,形状为 |
|
符号为K,形状为 |
|
符号为L,形状为 |
图2 |
图2示出了一种拼盘方案。为便于描述可将图2抽象为图3,就可以用一个数据为字符的二维数组来表示了。
B |
|
|
|
|
|
|
|
|
|
B |
K |
|
|
|
|
|
|
|
|
B |
K |
K |
|
|
|
|
|
|
|
B |
J |
K |
K |
|
|
|
|
|
|
J |
J |
J |
D |
D |
|
|
|
|
|
G |
J |
G |
D |
D |
C |
|
|
|
|
G |
G |
G |
C |
C |
C |
I |
|
|
|
E |
E |
E |
H |
H |
I |
I |
A |
|
|
E |
L |
H |
H |
H |
I |
A |
A |
F |
|
E |
L |
L |
L |
L |
I |
F |
F |
F |
F |
|
|
|
|
图3 |
|
|
|
|
对于由珠子构成的零件,可以放到盘件的任一位置,条件是能有地方放,且尺寸合适,所有的零件都允许旋转(0º、90º、180º、270º)和翻转(水平、竖直)。
现给出一个盘件的初始布局,求一种可行的智慧珠摆放方案,使所有的零件都能放进盘件中。
【输入格式】
文件中包含初始的盘件描述,一共有10行,第i行有i个字符。如果第i行的第j个字符是字母”A”至”L”中的一个,则表示第i行第j列的格子上已经放了零件,零件的编号为对应的字母。如果第i行的第j个字符是”.”,则表示第i行第j列的格子上没有放零件。
输入保证预放的零件已摆放在盘件中。
【输出格式】
如果能找到解,向输出文件打印10行,为放完全部12个零件后的布局。其中,第i行应包含i个字符,第i行的第j个字符表示第i行第j列的格子上放的是哪个零件。
如果无解,输出单独的一个字符串‘No solution’(不要引号,请注意大小写)。
所有的数据保证最多只有一组解。
【输入样例】
.
..
...
....
.....
.....C
...CCC.
EEEHH...
E.HHH....
E.........
【输出样例】
B
BK
BKK
BJKK
JJJDD
GJGDDC
GGGCCCI
EEEHHIIA
ELHHHIAAF
ELLLLIFFFF
const
trynum=1750000;
pnum:array ['A'..'L'] of longint=(3,4,4,4,5,5,5,5,5,5,5,5);//每种零件共多少个珠子
snum:array ['A'..'L'] of longint=(4,2,8,1,4,8,4,8,8,1,4,8);//每种零件共多少种放置方法
shape:array ['A'..'L',1..8,1..5,1..2] of longint=(
//A
(
{1}((0,0),(0,1),(1,0),(0,0),(0,0)),
{2}((0,0),(0,1),(1,1),(0,0),(0,0)),
{3}((0,0),(1,0),(1,-1),(0,0),(0,0)),
{4}((0,0),(1,0),(1,1),(0,0),(0,0)),
{5}((0,0),(0,0),(0,0),(0,0),(0,0)),
{6}((0,0),(0,0),(0,0),(0,0),(0,0)),
{7}((0,0),(0,0),(0,0),(0,0),(0,0)),
{8}((0,0),(0,0),(0,0),(0,0),(0,0))
),
//B
(
{1}((0,0),(0,1),(0,2),(0,3),(0,0)),
{2}((0,0),(1,0),(2,0),(3,0),(0,0)),
{3}((0,0),(0,0),(0,0),(0,0),(0,0)),
{4}((0,0),(0,0),(0,0),(0,0),(0,0)),
{5}((0,0),(0,0),(0,0),(0,0),(0,0)),
{6}((0,0),(0,0),(0,0),(0,0),(0,0)),
{7}((0,0),(0,0),(0,0),(0,0),(0,0)),
{8}((0,0),(0,0),(0,0),(0,0),(0,0))
),
//C
(
{1}((0,0),(0,1),(0,2),(1,0),(0,0)),
{2}((0,0),(0,1),(1,1),(2,1),(0,0)),
{3}((0,0),(1,0),(1,-1),(1,-2),(0,0)),
{4}((0,0),(1,0),(2,0),(2,1),(0,0)),
{5}((0,0),(0,1),(0,2),(1,2),(0,0)),
{6}((0,0),(1,0),(2,0),(2,-1),(0,0)),
{7}((0,0),(1,0),(1,1),(1,2),(0,0)),
{8}((0,0),(0,1),(1,0),(2,0),(0,0))
),
//D
(
{1}((0,0),(1,0),(1,1),(0,1),(0,0)),
{2}((0,0),(0,0),(0,0),(0,0),(0,0)),
{3}((0,0),(0,0),(0,0),(0,0),(0,0)),
{4}((0,0),(0,0),(0,0),(0,0),(0,0)),
{5}((0,0),(0,0),(0,0),(0,0),(0,0)),
{6}((0,0),(0,0),(0,0),(0,0),(0,0)),
{7}((0,0),(0,0),(0,0),(0,0),(0,0)),
{8}((0,0),(0,0),(0,0),(0,0),(0,0))
),
//E
(
{1}((0,0),(1,0),(2,0),(2,1),(2,2)),
{2}((0,0),(1,0),(2,0),(0,1),(0,2)),
{3}((0,0),(0,1),(0,2),(1,2),(2,2)),
{4}((0,0),(1,0),(2,0),(2,-1),(2,-2)),
{5}((0,0),(0,0),(0,0),(0,0),(0,0)),
{6}((0,0),(0,0),(0,0),(0,0),(0,0)),
{7}((0,0),(0,0),(0,0),(0,0),(0,0)),
{8}((0,0),(0,0),(0,0),(0,0),(0,0))
),
//F
(
{1}((0,0),(0,1),(1,1),(0,2),(0,3)),
{2}((0,0),(0,1),(0,2),(0,3),(1,2)),
{3}((0,0),(1,0),(2,0),(3,0),(1,-1)),
{4}((0,0),(1,0),(2,0),(3,0),(2,-1)),
{5}((0,0),(1,0),(2,0),(3,0),(1,1)),
{6}((0,0),(1,0),(2,0),(3,0),(2,1)),
{7}((0,0),(1,-1),(1,0),(1,1),(1,2)),
{8}((0,0),(1,-2),(1,-1),(1,0),(1,1))
),
//G
(
{1}((0,0),(0,1),(0,2),(1,0),(1,2)),
{2}((0,0),(0,1),(1,1),(2,1),(2,0)),
{3}((0,0),(0,1),(1,0),(2,0),(2,1)),
{4}((0,0),(1,0),(1,1),(1,2),(0,2)),
{5}((0,0),(0,0),(0,0),(0,0),(0,0)),
{6}((0,0),(0,0),(0,0),(0,0),(0,0)),
{7}((0,0),(0,0),(0,0),(0,0),(0,0)),
{8}((0,0),(0,0),(0,0),(0,0),(0,0))
),
//H
(
{1}((0,0),(1,0),(1,1),(0,1),(0,2)),
{2}((0,0),(0,1),(0,2),(1,2),(1,1)),
{3}((0,0),(1,0),(1,1),(0,1),(2,1)),
{4}((0,0),(1,0),(1,1),(0,1),(2,0)),
{5}((0,0),(1,0),(1,1),(0,1),(1,-1)),
{6}((0,0),(1,0),(1,1),(0,1),(1,2)),
{7}((0,0),(1,0),(2,0),(2,-1),(1,-1)),
{8}((0,0),(1,0),(2,0),(2,1),(1,1))
),
//I
(
{1}((0,0),(0,1),(0,2),(1,2),(1,3)),
{2}((0,0),(0,1),(0,2),(1,0),(1,-1)),
{3}((0,0),(0,1),(1,0),(1,-1),(1,-2)),
{4}((0,0),(0,1),(1,1),(1,2),(1,3)),
{5}((0,0),(1,0),(2,0),(2,1),(3,1)),
{6}((0,0),(1,0),(2,0),(2,-1),(3,-1)),
{7}((0,0),(1,0),(1,1),(2,1),(3,1)),
{8}((0,0),(1,0),(1,-1),(2,-1),(3,-1))
),
//J
(
{1}((0,0),(1,0),(1,-1),(1,1),(2,0)),
{2}((0,0),(0,0),(0,0),(0,0),(0,0)),
{3}((0,0),(0,0),(0,0),(0,0),(0,0)),
{4}((0,0),(0,0),(0,0),(0,0),(0,0)),
{5}((0,0),(0,0),(0,0),(0,0),(0,0)),
{6}((0,0),(0,0),(0,0),(0,0),(0,0)),
{7}((0,0),(0,0),(0,0),(0,0),(0,0)),
{8}((0,0),(0,0),(0,0),(0,0),(0,0))
),
//K
(
{1}((0,0),(1,0),(1,1),(2,1),(2,2)),
{2}((0,0),(1,0),(1,-1),(2,-1),(2,-2)),
{3}((0,0),(0,1),(1,0),(1,-1),(2,-1)),
{4}((0,0),(0,1),(1,1),(1,2),(2,2)),
{5}((0,0),(0,0),(0,0),(0,0),(0,0)),
{6}((0,0),(0,0),(0,0),(0,0),(0,0)),
{7}((0,0),(0,0),(0,0),(0,0),(0,0)),
{8}((0,0),(0,0),(0,0),(0,0),(0,0))
),
//L
(
{1}((0,0),(0,1),(0,2),(0,3),(1,0)),
{2}((0,0),(0,1),(0,2),(0,3),(1,3)),
{3}((0,0),(1,0),(2,0),(3,0),(0,1)),
{4}((0,0),(0,1),(1,1),(2,1),(3,1)),
{5}((0,0),(1,0),(2,0),(3,0),(3,-1)),
{6}((0,0),(1,0),(2,0),(3,0),(3,1)),
{7}((0,0),(1,0),(1,-1),(1,-2),(1,-3)),
{8}((0,0),(1,0),(1,1),(1,2),(1,3))
));
var
map:array [1..10,1..10] of char;
t:array ['A'..'L'] of boolean;
i,j,ntry:longint;
procedure print;
var
i,j:longint;
begin
for i:=1 to 10 do
begin
for j:=1 to i do write (map[i,j]);
writeln;
end;
close (input); close (output);
halt;
end;
procedure printno;
begin
writeln ('No solution');
close (input); close (output);
halt;
end;
procedure work(l,r:longint);
var
c:char;
i,j,x,y:longint;
flag:boolean;
begin
inc (ntry);
if ntry>trynum then printno;
if l>10 then print;
if r>l then begin work(l+1,1); exit; end;
if map[l,r]<>'.' then begin work(l,r+1); exit; end;
for c:='A' to 'L' do
if not t[c] then
for i:=1 to snum[c] do
begin
flag:=true;
for j:=1 to pnum[c] do
begin
x:=l+shape[c,i,j,1]; y:=r+shape[c,i,j,2];
if (x>10) or (y>x) or (y<0) or (map[x,y]<>'.') then begin flag:=false; break; end;
end;
if flag then
begin
t[c]:=true;
for j:=1 to pnum[c] do
begin
x:=l+shape[c,i,j,1];
y:=r+shape[c,i,j,2];
map[x,y]:=c;
end;
work(l,r+1);
t[c]:=false;
for j:=1 to pnum[c] do
begin
x:=l+shape[c,i,j,1];
y:=r+shape[c,i,j,2];
map[x,y]:='.';
end;
end;
end;
end;
begin
// assign (input,'zhzyx.in'); reset (input);
// assign (output,'zhzyx.out'); rewrite (output);
for i:=1 to 10 do
begin
for j:=1 to i do
begin
read (map[i,j]);
if map[i,j]<>'.' then t[map[i,j]]:=true;
end;
readln;
end;
work(1,1);
printno;
end.