7
BCABCCB
这道题目就是纯碎的bfs,数据不大,暴力判重,8^8次方轻轻松松,时间又不紧,一次AC
type
arr=array[1..8] of longint;
const
jia:array['A'..'C',1..8] of longint=((8,7,6,5,4,3,2,1),
(4,1,2,3,6,7,8,5),
(1,7,2,4,5,3,6,8));
var
head,tail,i:longint;
d:array[1..1000000] of record
tot:longint;
st:ansistring;
ch:arr;
end;
k:char;
bz:array[1..8,1..8,1..8,1..8,1..8,1..8,1..8,1..8] of boolean;
a,t:array[1..8] of longint;
function pd(t:arr):boolean;
begin
pd:=true;
for i:=1 to 8 do
if t[i]<>a[i] then exit(false);
end;
procedure print;
begin
writeln(d[tail].tot);
for i:=1 to d[tail].tot do
if i mod 60=0 then writeln(d[tail].st[i]) else write(d[tail].st[i]);
end;
begin
for i:=1 to 8 do
read(a[i]);
head:=0;
tail:=1;
d[1].tot:=0;
d[1].st:='';
for i:=1 to 8 do
d[1].ch[i]:=i;
if pd(d[1].ch) then
begin
writeln(0);
halt;
end;
bz[1,2,3,4,5,6,7,8]:=true;
while head<tail do
begin
inc(head);
for k:='A' to 'C' do
begin
inc(tail);
d[tail]:=d[head];
inc(d[tail].tot);
for i:=1 to 8 do
begin
d[tail].ch[i]:=d[head].ch[jia[k,i]];
t[i]:=d[tail].ch[i];
end;
if k='A' then d[tail].st:=d[tail].st+'A';
if k='B' then d[tail].st:=d[tail].st+'B';
if k='C' then d[tail].st:=d[tail].st+'C';
if not bz[t[1],t[2],t[3],t[4],t[5],t[6],t[7],t[8]] then
bz[t[1],t[2],t[3],t[4],t[5],t[6],t[7],t[8]]:=true
else
dec(tail);
if pd(d[tail].ch) then
begin
print;
halt;
end;
end;
end;
end.