讨论 / 求大牛看下这个八数码难题哪错了
huangxin 2013-02-03 22:32:00
点我顶贴 收藏 删除
type

queue=record

vec:array[1..10000]of string[9];

a:array[1..10000]of integer;

f,r:integer;

end;

var

q:queue;

i,k,j,t:integer;

bo:boolean;

st:string;

procedure jiaohuan(var a,b:char);

var

ch:char;

begin

ch:=a; a:=b; b:=ch;

end;

function check:boolean;

var i:integer;

begin

for i:=1 to q.r-1 do

if q.vec[i]=q.vec[q.r] then exit(true);

exit(false);

end;

function panduan:boolean;

var i:integer;

begin

for i:=1 to 9 do if q.vec[q.r][i]<>st[i] then exit(false);

exit(true);

end;

begin

q.f:=1; q.r:=1;

for i:=1 to 9 do

begin

read(k);

q.vec[q.f]:=q.vec[q.f]+chr(k+48);

end;

for i:=1 to 9 do

begin

read(k);

st:=st+chr(k+48);

end;

q.f:=0;

repeat

inc(q.f);

for i:=1 to 9 do if q.vec[q.f][i]='0' then t:=i;

if t mod 3<>0 then begin

inc(q.r);inc(q.a[q.f]);

q.vec[q.r]:=q.vec[q.f];

jiaohuan(q.vec[q.r][t],q.vec[q.r][t+1]);

if panduan then begin bo:=true; break; end;

if check then begin q.vec[q.r]:=''; dec(q.r);dec(q.a[q.f]); end;

end;

if t mod 3<>1 then begin

inc(q.r);inc(q.a[q.f]);

q.vec[q.r]:=q.vec[q.f];

jiaohuan(q.vec[q.r][t],q.vec[q.r][t-1]);

if panduan then begin bo:=true; break; end;

if check then begin q.vec[q.r]:='';dec(q.r);dec(q.a[q.f]); end;

end;

if t>3 then begin

inc(q.r);inc(q.a[q.f]);

q.vec[q.r]:=q.vec[q.f];

jiaohuan(q.vec[q.r][t],q.vec[q.r][t-3]);

if panduan then begin bo:=true; break; end;

if check then begin q.vec[q.r]:='';dec(q.r);dec(q.a[q.f]); end;

end;

if t<7 then begin

inc(q.r);inc(q.a[q.f]);

q.vec[q.r]:=q.vec[q.f];

jiaohuan(q.vec[q.r][t],q.vec[q.r][t+3]);

if panduan then begin bo:=true; break; end;

if check then begin q.vec[q.r]:='';dec(q.r);dec(q.a[q.f]); end;

end;

until bo;

if q.f>=q.r then writeln('no solution') else begin

i:=q.r; j:=q.r; t:=q.r+1; k:=1;

repeat

dec(i);

dec(t,q.a[i]);

if t<=j then begin inc(k); j:=i; end;

until j=3;

writeln(k);

end;

end.

#1 huangxin@2013-02-03 22:32:00
回复 删除
求大牛啊

真心不知道怎么错的

查看更多回复
提交回复