讨论 / 做了无数遍的搜索原代码,栈溢出??
lxl 2012-07-01 21:44:00
点我顶贴 收藏 删除
吐血。。

const

fx:array[1..8] of integer=(-2,-1,1,2,2,1,-1,-2);

fy:array[1..8] of integer=(1,2,2,1,-1,-2,-2,-1);

var

x,y,i,j,k,l,m,n,min:longint;

procedure search(x,y,step:longint);

var

x1,y1,i,j,k,l:longint;

begin

if ((x=1) and (y=4)) or ((x=1) and (y=6)) or ((x=2) and (y=5)) or (x<1) or (x>10) or (y<1) or (y>9) then exit;

if ((x=1) and (y=5)) then begin if step<min then min:=step;exit;end

else

for i:=1 to 8 do

begin

x1:=x+fx[i];

y1:=y+fy[i];

search(x1,y1,step+1);

end;

end;

begin

read(x,y);

min:=maxlongint;

search(x,y,0);

writeln(min);

end.

#1 !@#$%@2010-09-07 22:41:00
回复 删除
...
#2 xiaoyangdi@2012-07-01 21:44:00
回复 删除
回复 楼主lxl 的帖子

program duoduoxq;

const maxn=15;

flag:array[1..8,0..1] of integer=((1,2),(-1,2),(2,1),(-2,1),(2,-1),(-2,-1),(-1,-2),(1,-2));

flag1:array[1..3,0..1] of integer=((1,4),(1,6),(2,5));

var ans,x1,y1,x2,y2:integer;

map:array[-3..maxn,-3..maxn] of boolean;

procedure init;

var i,j:integer;

begin

assign(input,'duoduoxq.in');reset(input);

assign(output,'duoduoxq.out');rewrite(output);

read(x1,y1);x2:=1;y2:=5;

fillchar(map,sizeof(map),false);

for i:=1 to 10 do

for j:=1 to 9 do

map[i,j]:=true;

for i:=1 to 3 do map[flag1[i,0],flag1[i,1]]:=false;

end;

procedure try(x,y,step:integer);

var i:integer;

begin

if step >= ans then exit;

if (x = x2)and(y = y2) then begin

ans:=step;

exit;

end;

for i:=1 to 8 do

if map[x+flag[i,0],y+flag[i,1]] then begin

map[x+flag[i,0],y+flag[i,1]]:=false;

try(x+flag[i,0],y+flag[i,1],step+1);

map[x+flag[i,0],y+flag[i,1]]:=true;

end;

end;

procedure print;

begin

if ans = maxint then write('No Answer') else write(ans);

close(input);close(output);

end;

procedure main;

var i:integer;

pd:boolean;

begin

map[x1,y1]:=false;

ans:=maxint;

pd:=true;

for i:=1 to 3 do

if (x1 = flag1[i,0])and(y1 = flag1[i,1]) then pd:=false;

if pd then try(x1,y1,0) else begin print;halt;end;

end;

begin

init;

main;

print;

end.

查看更多回复
提交回复