Boa tarde. Estou com muita dificuldade com esse algoritmo. Ele está em delphi e eu preciso passar para java. Será que alguém poderia me ajudar, principalmente com os ponteiros. Já tentei usar o modo unsafe mas não deu muito certo.
este é o código em delphi:
uses
SysUtils;
type apontador = ^No;
No = record
Jarra1: integer;
Jarra2: integer;
no_pai: apontador;
proximo_no: apontador;
end;
var
primeiro_aberto: apontador;
ultimo_aberto: apontador;
primeiro_visitado: apontador;
no_atual: apontador;
bolEncontrou: boolean;
function nao_encontra(jarra1, jarra2: integer) : boolean;
var no_atual: apontador;
begin
Result:= False;
no_atual := Primeiro_aberto;
while no_atual <> nil do begin
if (no_atual^.jarra1 = jarra1) and (no_atual^.Jarra2 = jarra2) then begin
Result := True;
break;
end;
no_atual := no_atual.proximo_no;
end;
end;
function nao_visitado(jarra1, jarra2: integer) : boolean;
var no_atual: apontador;
begin
Result:= False;
no_atual := Primeiro_visitado;
while no_atual <> nil do begin
if (no_atual^.jarra1 = jarra1) and (no_atual^.Jarra2 = jarra2) then begin
Result := True;
break;
end;
no_atual := no_atual.proximo_no;
end;
end;
procedure Novo_no ( jarra1, jarra2: integer; No_pai: apontador);
var
novoNo: apontador;
begin
if (not nao_visitado (jarra1, jarra2)) and (not nao_encontra(jarra1, jarra2)) then begin
new(novoNo);
novoNo.Jarra1 := jarra1;
novoNo.Jarra2 := jarra2;
novoNo.no_pai := No_pai;
if ultimo_aberto <> nil then
ultimo_aberto.proximo_no := novoNo;
ultimo_aberto := novoNo;
if primeiro_aberto = nil then
primeiro_aberto := novoNo;
end;
end;
function seleciona_primeiro: apontador;
begin
if primeiro_aberto <> nil then
Result:= primeiro_aberto;
end;
function seleciona_ultimo: apontador;
begin
if ultimo_aberto <> nil then
Result:= ultimo_aberto;
end;
procedure expande_no(No_atual: apontador);
begin
if No_atual.jarra1 < 4 then
novo_no(4, No_atual.jarra2, no_atual);
if no_atual.Jarra2 < 3 then
novo_no(No_atual.Jarra1, 3, no_atual);
if no_atual.Jarra1 > 0 then
novo_no(0, no_atual.jarra2, no_atual);
if no_atual.Jarra2 > 0 then
novo_no(no_atual.Jarra1, 0, no_atual);
if (no_atual.jarra1 < 4) and (no_atual.jarra2 > 0) then begin
if no_atual.jarra1 + no_atual.jarra2 <= 4 then
novo_no(no_atual.jarra1 + no_atual.jarra2, 0, no_atual)
else
novo_no(4, no_atual.jarra1 + no_atual.jarra2 - 4, no_atual);
end;
if (no_atual.jarra2 < 3) and (no_atual.Jarra1 >0) then begin
if no_atual.jarra1 + no_atual.jarra2 <= 3 then
novo_no(0, no_atual.jarra1 + no_atual.jarra2, no_atual)
else
novo_no(no_atual.jarra1 + no_atual.jarra2 - 3, 3, no_atual)
end;
primeiro_aberto := no_atual.proximo_no;
no_atual.proximo_no := primeiro_visitado;
primeiro_visitado := no_atual;
end;
begin
novo_no(0,0,nil);
bolEncontrou := False;
no_atual := primeiro_aberto;
while (not bolEncontrou) and (primeiro_aberto <> nil) do begin
// (no_atual.jarra1 <> 2) or (no_atual.jarra2 <> 0) do begin
bolEncontrou := (no_atual.jarra1 = 2) and (no_atual.jarra2 = 0);
if not bolEncontrou then begin
expande_no(no_atual);
no_atual := seleciona_primeiro;
end else begin
while no_atual.no_pai <> nil do begin
writeln(no_atual.jarra1 , ',' , no_atual.jarra2);
no_atual := no_atual.no_pai;
end;
end;
{ TODO -oUser -cConsole Main : Insert code here }
end;
sleep(100000);
end.