C 翻译 delphi 算法 这个功能用delphi 7 VCL怎么实现?翻译:C/C++ codeint canmove(int x,int y,int to_x,i
C 翻译 delphi 算法 这个功能用delphi 7 VCL怎么实现?
翻译:
C/C++ codeint canmove(int x,int y,int to_x,int to_y) { if(x==to_x && y==to_y) return 1; qp[x][y]=0; if(qp[x+1][y] && x<8) if(canmove(x+1,y,to_x,to_y)) { qp[x][y]=1; return 1; } if(qp[x-1][y] && x>0) if(canmove(x-1,y,to_x,to_y)) { qp[x][y]=1; return 1; } if(qp[x][y+1] && y<8) if(canmove(x,y+1,to_x,to_y)) { qp[x][y]=1; return 1; } if(qp[x][y-1] && y>0) if(canmove(x,y-1,to_x,to_y)) { qp[x][y]=1; return 1; } qp[x][y]=1; return 0;}
delphi 7 怎么实现这个功能呀??
[解决办法]function canmove(x, y, to_x, to_y:integer):integer;
var
qp:array[0..100]of array[0..100] of integer;
begin
if(x=to_x) and ( y=to_y) then
begin
result:= 1;
exit;
end;
qp[x][y]:=0; //这个数组是全局的吧?
if(qp[x+1][y]<>0) and ( x<8) then
if(canmove(x+1,y,to_x,to_y)<>0) then
begin
qp[x][y]:=1;
result:= 1;
exit;
end;
if(qp[x-1][y]<>0) and ( x>0) then
if(canmove(x-1,y,to_x,to_y)<>0) then
begin
qp[x][y]:=1;
result:= 1;
exit;
end;
if(qp[x][y+1]<>0) and ( y<8) then
if(canmove(x,y+1,to_x,to_y)<>0) then
begin
qp[x][y]:=1;
result:= 1;
exit;
end;
if(qp[x][y-1]<>0) and ( y>0) then
if(canmove(x,y-1,to_x,to_y)<>0) then
begin
qp[x][y]:=1;
result:= 1;
exit;
end;
qp[x][y]:=1;
result:=0;
end;