level 1
班靖琪0Gb
楼主
const
u:array[1..4]of longint=(-1,0,0,1);
w:array[1..4]of longint=(0,-1,1,0);var f:boolean;
i,j,h,t,x1,y1:longint; a:array[1..3,1..3]of integer; b:array[0..8,0..8,0..8,0..8,0..8,0..8,0..8,1..2]of boolean; c:array[1..3,1..3]of longint; d:array[1..9]of longint;
q:array[0..10000000]of record
x,y,step:longint;
k:array[1..3,1..3]of integer;
end; //-----------------
procedure take_notes;
var s:string; i,j:longint;
begin
s:='012345678';
for i:=1 to 2 do
for j:=1 to 3 do
delete(s,pos(chr(q[t].k[i,j]+48),s),1);
delete(s,pos(chr(q[t].k[3,1]+48),s),1);
if q[t].k[3,2]=(ord(s[1])-ord('0')) then b[q[t].k[1,1],q[t].k[1,2],q[t].k[1,3],q[t].k[2,1],q[t].k[2,2],q[t].k[2,3],q[t].k[3,1],1]:=false
else b[q[t].k[1,1],q[t].k[1,2],q[t].k[1,3],q[t].k[2,1],q[t].k[2,2],q[t].k[2,3],q[t].k[3,1],2]:=false; end;
//---------------------
function check:boolean;
var s:string; i,j:longint;
begin
check:=true;
s:='012345678';
for i:=1 to 2 do
for j:=1 to 3 do
delete(s,pos(chr(q[t].k[i,j]+48),s),1);
delete(s,pos(chr(q[t].k[3,1]+48),s),1);
if b[q[h].k[1,1],q[h].k[1,2],q[h].k[1,3],q[h].k[2,1],q[h].k[2,2],q[h].k[2,3],q[h].k[3,1],1]=false and (q[h].k[3,2]=ord(s[1])-ord('0')) then check:=false;
if b[q[h].k[1,1],q[h].k[1,2],q[h].k[1,3],q[h].k[2,1],q[h].k[2,2],q[h].k[2,3],q[h].k[3,1],2]=false and (q[h].k[3,2]=ord(s[2])-ord('0')) then check:=false;
end;
//--------------
procedure search;
var n,m,p,step:longint;
begin
h:=0; t:=1; q[1].x:=x1; q[1].y:=y1; q[1].k:=a; take_notes;
while h<t do
begin
inc(h);
for i:=1 to 4 do begin
n:=q[h].x+u[i]; m:=q[h].y+w[i]; step:=q[h].step;
if(n>0)and(n<4)and(m>0)and(m<4) then begin
p:=q[h].k[q[h].x,q[h].y]; q[h].k[q[h].x,q[h].y]:=q[h].k[n,m]; q[h].k[n,m]:=p;
if check then begin
if (q[h].k[1,1]=c[1,1])and(q[h].k[1,2]=c[1,2]) and(q[h].k[1,3]=c[1,3])and(q[h].k[2,1]=c[2,1]) and(q[h].k[2,2]=c[2,2])and(q[h].k[2,3]=c[2,3]) and(q[h].k[3,1]=c[3,1])and(q[h].k[3,2]=c[3,2])
then
begin
inc(step);writeln(step);f:=true;exit;
end;
inc(t); q[t].x:=n; q[t].y:=m; q[t].step:=q[h].step+1; q[t].k:=q[h].k; take_notes; end;
p:=q[h].k[q[h].x,q[h].y]; q[h].k[q[h].x,q[h].y]:=q[h].k[n,m];
q[h].k[n,m]:=p;
end;
end;
end;
end; //-------------
begin
fillchar(b,sizeof(b),true);
for i:=1 to 3 do
for j:=1 to 3 do begin
read(a[j,i]);
if a[j,i]=0 then begin
x1:=j; y1:=i;
end;
end;
for i:=1 to 3 do
for j:=1 to 3 do
read(c[j,i]);
search;
if not f then writeln('No Solution!');
end.
2015年08月23日 07点08分
1
u:array[1..4]of longint=(-1,0,0,1);
w:array[1..4]of longint=(0,-1,1,0);var f:boolean;
i,j,h,t,x1,y1:longint; a:array[1..3,1..3]of integer; b:array[0..8,0..8,0..8,0..8,0..8,0..8,0..8,1..2]of boolean; c:array[1..3,1..3]of longint; d:array[1..9]of longint;
q:array[0..10000000]of record
x,y,step:longint;
k:array[1..3,1..3]of integer;
end; //-----------------
procedure take_notes;
var s:string; i,j:longint;
begin
s:='012345678';
for i:=1 to 2 do
for j:=1 to 3 do
delete(s,pos(chr(q[t].k[i,j]+48),s),1);
delete(s,pos(chr(q[t].k[3,1]+48),s),1);
if q[t].k[3,2]=(ord(s[1])-ord('0')) then b[q[t].k[1,1],q[t].k[1,2],q[t].k[1,3],q[t].k[2,1],q[t].k[2,2],q[t].k[2,3],q[t].k[3,1],1]:=false
else b[q[t].k[1,1],q[t].k[1,2],q[t].k[1,3],q[t].k[2,1],q[t].k[2,2],q[t].k[2,3],q[t].k[3,1],2]:=false; end;
//---------------------
function check:boolean;
var s:string; i,j:longint;
begin
check:=true;
s:='012345678';
for i:=1 to 2 do
for j:=1 to 3 do
delete(s,pos(chr(q[t].k[i,j]+48),s),1);
delete(s,pos(chr(q[t].k[3,1]+48),s),1);
if b[q[h].k[1,1],q[h].k[1,2],q[h].k[1,3],q[h].k[2,1],q[h].k[2,2],q[h].k[2,3],q[h].k[3,1],1]=false and (q[h].k[3,2]=ord(s[1])-ord('0')) then check:=false;
if b[q[h].k[1,1],q[h].k[1,2],q[h].k[1,3],q[h].k[2,1],q[h].k[2,2],q[h].k[2,3],q[h].k[3,1],2]=false and (q[h].k[3,2]=ord(s[2])-ord('0')) then check:=false;
end;
//--------------
procedure search;
var n,m,p,step:longint;
begin
h:=0; t:=1; q[1].x:=x1; q[1].y:=y1; q[1].k:=a; take_notes;
while h<t do
begin
inc(h);
for i:=1 to 4 do begin
n:=q[h].x+u[i]; m:=q[h].y+w[i]; step:=q[h].step;
if(n>0)and(n<4)and(m>0)and(m<4) then begin
p:=q[h].k[q[h].x,q[h].y]; q[h].k[q[h].x,q[h].y]:=q[h].k[n,m]; q[h].k[n,m]:=p;
if check then begin
if (q[h].k[1,1]=c[1,1])and(q[h].k[1,2]=c[1,2]) and(q[h].k[1,3]=c[1,3])and(q[h].k[2,1]=c[2,1]) and(q[h].k[2,2]=c[2,2])and(q[h].k[2,3]=c[2,3]) and(q[h].k[3,1]=c[3,1])and(q[h].k[3,2]=c[3,2])
then
begin
inc(step);writeln(step);f:=true;exit;
end;
inc(t); q[t].x:=n; q[t].y:=m; q[t].step:=q[h].step+1; q[t].k:=q[h].k; take_notes; end;
p:=q[h].k[q[h].x,q[h].y]; q[h].k[q[h].x,q[h].y]:=q[h].k[n,m];
q[h].k[n,m]:=p;
end;
end;
end;
end; //-------------
begin
fillchar(b,sizeof(b),true);
for i:=1 to 3 do
for j:=1 to 3 do begin
read(a[j,i]);
if a[j,i]=0 then begin
x1:=j; y1:=i;
end;
end;
for i:=1 to 3 do
for j:=1 to 3 do
read(c[j,i]);
search;
if not f then writeln('No Solution!');
end.
