C++源程序转换delphi代码(著名A*算法最短路径)(100分)

  • 主题发起人 主题发起人 zfine
  • 开始时间 开始时间
Z

zfine

Unregistered / Unconfirmed
GUEST, unregistred user!
下面是本人转换的,但总不能编译,有懂C++的帮忙看看!
C++源代码///////////////////////////////////////////////////////////////
#define MAPMAXSIZE 100
#define MAXINT 8192
#define STACKSIZE 65536
#define tile_num(x,y) ((y)*map_w+(x))
#define tile_x(n) ((n)%map_w)
#define tile_y(n) ((n)/map_w)
typedef struct node *TREE;
struct node {
int h;
int tile;
TREE father;
} ;
typedef struct node2 *LINK;
struct node2 {
TREE node;
int f;
LINK next;
};
LINK queue

TREE stack[STACKSIZE]

int stacktop;
unsigned char map[MAPMAXSIZE][MAPMAXSIZE]

int dis_map[MAPMAXSIZE][MAPMAXSIZE]

int map_w,map_h

int start_x,start_y,end_x,end_y

void init_queue()
{
queue=(LINK)malloc(sizeof(*queue));
queue->node=NULL;
queue->f=-1;
queue->next=(LINK)malloc(sizeof(*queue));
queue->next->f=MAXINT;
queue->next->node=NULL;
queue->next->next=NULL;
}
void enter_queue(TREE node,int f)
{
LINK p=queue,father,q;
while(f>p->f) {
father=p;
p=p->next;
assert(p);
}
q=(LINK)malloc(sizeof(*q));
assert(queue);
q->f=f,q->node=node,q->next=p;
father->next=q;
}
TREE get_from_queue()
{
TREE bestchoice=queue->next->node;
LINK next=queue->next->next;
free(queue->next);
queue->next=next;
stack[stacktop++]=bestchoice;
assert(stacktop<STACKSIZE);
return bestchoice;
}
void pop_stack()
{
free(stack[--stacktop]);
}
void freetree()
{
int i;
LINK p;
for (i=0;i<stacktop;i++)
free(stack);
while (queue) {
p=queue;
free(p->node);
queue=queue->next;
free(p);
}
}
int judge(int x,int y)
{
int distance;
distance=abs(end_x-x)+abs(end_y-y);
return distance;
}
int trytile(int x,int y,TREE father)
{
TREE p=father;
int h;
if (map[y][x]!=' ') return 1;
while (p) {
if (x==tile_x(p->tile) &amp;&amp
y==tile_y(p->tile)) return 1;
p=p->father;
}
h=father->h+1;
if (h>=dis_map[y][x]) return 1;
dis_map[y][x]=h;
p=(TREE)malloc(sizeof(*p));
p->father=father;
p->h=father->h+1;
p->tile=tile_num(x,y);
enter_queue(p,p->h+judge(x,y));
return 0;
}
void findpath(int *path)
{
TREE root;
int i,j;
stacktop=0;
for (i=0;i<map_h;i++)
for (j=0;j<map_w;j++)
dis_map[j]=MAXINT;
init_queue();
root=(TREE)malloc(sizeof(*root));
root->tile=tile_num(start_x,start_y);
root->h=0;
root->father=NULL;
enter_queue(root,judge(start_x,start_y));
for (;;) {
int x,y,child;
TREE p;
root=get_from_queue();
if (root==NULL) {
*path=-1;
return;
}
x=tile_x(root->tile);
y=tile_y(root->tile);
if (x==end_x &amp;&amp
y==end_y) break;
child=trytile(x,y-1,root)

child&amp;=trytile(x,y+1,root);
child&amp;=trytile(x-1,y,root);
child&amp;=trytile(x+1,y,root)

if (child!=0)
pop_stack();
}
for (i=0;root;i++) {
path=root->tile;
root=root->father;
}
path=-1;
freetree();
}

本人转换的Delphi代码/////////////////////////////////////////////////
unit FindPath;

interface

uses
Windows,SysUtils;

const MAPMAXSIZE=100;
const MAXINT=8192;
const STACKSIZE=65536;

type
tree=^node;
node=record
h:integer;
tile:integer;
father:tree;
end;
type
LINK=^node2;
node2=record
node :tree;
f:integer;
next:LINK;
end;

TArrayType = array[0..10000] of Byte;
Tpath=^TArrayType;

var
queue:LINK;
stack:array [0..STACKSIZE]of tree;
stacktop:integer;
map:array [0..MAPMAXSIZE,0..MAPMAXSIZE] of string;
dis_map:array [0..MAPMAXSIZE,0..MAPMAXSIZE] of integer;
map_w,map_h:integer;
start_x,start_y,end_x,end_y:integer;


implementation

function tile_num(x,y:integer):integer;
begin
result:=y*map_w+x;
end;

function tile_x(n:integer):integer;
begin
result:=(n mod map_w);
end;

function tile_y(n:integer):integer;
begin
result:=(n div map_w);
end;

procedure init_queue;
begin
getmem(queue,sizeof(LINK));
queue.node :=nil;
queue.f :=-1;
getmem(queue.next,sizeof(LINK));
queue.next.f :=MAXINT;
queue.next.node :=nil;
queue.next.next :=nil;
end;

procedure enter_queue(node:tree;f:integer);
var
p,father,q:LINK;
begin
p:=queue;
while(f>p.f) do
begin
father:=p;
p:=p.next ;
Assert(p<>nil,'');
end;
getmem(q,sizeof(LINK));
assert(queue<>nil,'');
q.f :=f;
q.node :=node;
q.next :=p;
father.next :=q;
end;

function get_from_queue():tree;
var
bestchoice:tree;
next:LINK;
begin
bestchoice:=queue.next.node;
next:=queue.next.next ;
freemem(queue.next);
queue.next :=next;
stack[stacktop+1]:=bestchoice;
assert(stacktop<STACKSIZE,'');
result:=bestchoice;
end;

procedure pop_stack();
begin
freemem(stack[stacktop-1]);
end;

procedure freetree();
var
i:integer;
p:LINK;
begin
for i:=0 to stacktop-1 do
freemem(stack);
while (queue<>nil) do
begin
p:=queue;
freemem(p.node);
queue:=queue.next ;
freemem(p);
end

end;

function judge(x,y:integer):integer;
var
distance:integer;
begin
distance:=abs(end_x-x)+abs(end_y-y);
result:=distance;
end;

function trytile(x,y:integer;father:Tree):integer;
var
p:TREE;
h:integer;
begin
p:=father;
if map[Y,X]<>'' then result:=1;
while(p<>nil) do
begin
if ((x=tile_x(p.tile))) and ((y=tile_y(p.tile)))
then result:=1;
p:=p.father ;
end;
h:=father.h +1;
if h>=dis_map[y,x] then result:=1;
dis_map[y,x]:=h;
getmem(p,sizeof(TREE));
p.father :=father;
p.h :=father.h +1;
p.tile:=tile_num(x,y);
enter_queue(p,p.h+judge(x,y));
result:=0;
end;

procedure foundpath(path:TPath);
var
root :tree;
i,j :integer;
x,y,child :integer;
p :TREE ;
begin
stacktop:=0;
for i:=0 to map_h-1 do
for j:=0 to map_w-1 do
dis_map[i,j]:=MAXINT;
init_queue();
getmem(root,sizeof(tree));
root.tile:=tile_num(start_x,start_y);
root.h:=0;
root.father:=nil;
enter_queue(root,judge(start_x,start_y));
repeat
begin
root:=get_from_queue();
if root=nil then
path:=nil;
end;
until path=nil ;
if path=nil then exit;

x:=tile_x(root.tile);
y:=tile_y(root.tile);

if ((x=end_x) and (y=end_y)) then exit;
child:=trytile(x,y-1,root)

child:=trytile(x,y+1,root);
child:=trytile(x-1,y,root);
child:=trytile(x+1,y,root)


if (child<>0) then pop_stack;


for i:=0 to root.tile-1 do
begin
path^:=root.tile;
root:=root.father;
end;
path^:=-1;
freetree();
end;
end.
在此先谢了!


 
改成这样试试:
path^ := Byte(-1);
 
后退
顶部