Algoritmo em delphi - passar para java

4 respostas
M

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.

4 Respostas

F

véi… cara q fez isso tinha que ganhar um premio.

tebosoftware

Você não precisa utilizar ponteiros, no caso ai é uma lista encadeada, onde um objeto aponta para o próximo. O que você precisaria é montar um algorítimo para ler a estrutura indo de nó em nó.

G

o codigo em delphi, agora um pouco melhorado.

veja se agora vc consegue:

type

  { TNo }

  TNo = class
    Jarra1: integer;
    Jarra2: integer;
    no_pai: TNo;
    proximo_no: TNo;
  public
    constructor Create(const AJarra1, AJarra2: Integer; aNopai: TNo);
  end;

  { TTronco }

  TTronco = class
  strict private
    Fprimeiro_aberto: TNo;
    Fultimo_aberto: TNo;
    Fprimeiro_visitado: TNo;
    Fno_atual: TNo;
    function FindNo(StartNo: TNo; const jarra1, jarra2 : integer) : Boolean;
  public
    property NoAtual: TNo read Fno_atual write Fno_atual;
    property primeiro_aberto : TNo read Fprimeiro_aberto;

    function nao_encontra(jarra1, jarra2: integer) : boolean;
    function nao_visitado(jarra1, jarra2: integer) : boolean;
    procedure Novo_no ( jarra1, jarra2: integer; No_pai: TNo);
    function seleciona_ultimo: TNo;
    procedure expande_no(atual: TNo);
  end;

{ apontador }

constructor TNo.Create(const AJarra1, AJarra2 : Integer; aNopai : TNo);
begin
  inherited Create;
  Self.Jarra1 := AJarra1;
  Self.Jarra2 := AJarra2;
  Self.no_pai := aNopai;
end;

{ TTronco }

function TTronco.FindNo(StartNo: TNo; const jarra1, jarra2 : integer) : Boolean;
begin
  while StartNo <> nil do
  begin
    if (StartNo.jarra1 = jarra1) and (StartNo.Jarra2 = jarra2) then
    begin
      Result := True;
      Exit;
    end;
    StartNo := StartNo.proximo_no;
  end;

  Result := False;
end;

function TTronco.nao_encontra(jarra1, jarra2 : integer) : boolean;
begin
  Result := FindNo(FPrimeiro_aberto, jarra1, jarra2);
end;

function TTronco.nao_visitado(jarra1, jarra2: integer) : boolean;
begin
  Result:= FindNo(FPrimeiro_visitado, jarra1, jarra2);
end;

procedure TTronco.Novo_no ( jarra1, jarra2: integer; No_pai: TNo);
var
  novoNo: TNo;
begin
  if (not nao_visitado (jarra1, jarra2)) and (not nao_encontra(jarra1, jarra2)) then
  begin
    novoNo := TNo.Create(jarra1, jarra2, No_pai);

    if Fultimo_aberto <> nil then
      Fultimo_aberto.proximo_no := novoNo;

    Fultimo_aberto := novoNo;

    if Fprimeiro_aberto = nil then
       Fprimeiro_aberto := novoNo;
  end;
end;

function TTronco.seleciona_ultimo: TNo;
begin
  //if ultimo_aberto <> nil then ///???????????
    Result := Fultimo_aberto;
end;

procedure TTronco.expande_no(atual: TNo);
begin
   if atual.jarra1 < 4 then
     novo_no(4, atual.jarra2, atual);

   if atual.Jarra2 < 3 then
     novo_no(atual.Jarra1, 3, atual);

   if atual.Jarra1 > 0 then
     novo_no(0, atual.jarra2,  atual);

   if  atual.Jarra2 > 0 then
     novo_no(atual.Jarra1, 0,  atual);

   if (atual.jarra1 < 4) and (atual.jarra2 > 0) then
   begin
      if atual.jarra1 + atual.jarra2 <= 4 then
        novo_no(atual.jarra1 + atual.jarra2, 0, atual)
      else
        novo_no(4, atual.jarra1 + atual.jarra2 - 4, atual);
   end;

   if (atual.jarra2 < 3) and (atual.Jarra1 >0) then
   begin
     if atual.jarra1 + atual.jarra2 <= 3 then
       novo_no(0, atual.jarra1 + atual.jarra2, atual)
     else
       novo_no(atual.jarra1 + atual.jarra2 - 3, 3, atual)
   end;

   Fprimeiro_aberto := atual.proximo_no;
   Fno_atual.proximo_no := Fprimeiro_visitado;
   Fprimeiro_visitado := atual;
end;

///*************************************************************************************

procedure Teste;
var
  bolEncontrou: boolean;
begin
  with TTronco.Create do
  try
    novo_no(0, 0, nil);
    bolEncontrou := False;
    NoAtual := primeiro_aberto;
    while (not bolEncontrou) and (primeiro_aberto <> nil) do
    begin
    //  (no_atual.jarra1 <> 2) or (no_atual.jarra2 <> 0) do begin
      bolEncontrou := (NoAtual.jarra1 = 2) and (NoAtual.jarra2 = 0);
      if not bolEncontrou then
      begin
        expande_no(NoAtual);
        NoAtual := primeiro_aberto;
      end else
      begin
        while NoAtual.no_pai <> nil do begin
          writeln(NoAtual.jarra1 , ',' , NoAtual.jarra2);
          NoAtual := NoAtual.no_pai;
        end;
      end;

    { TODO -oUser -cConsole Main : Insert code here }
    end;
    ///sleep(100000);
    ReadLn;
  finally
    Free;
  end;
end;

begin
  Teste;
end.
R
public class TTronco {

	private TNo fPrimeiro_aberto;
	private TNo fUltimo_aberto;
	private TNo fPrimeiro_visitado;
	private TNo fNo_atual;
	
	public TNo getfPrimeiro_aberto() {
		return fPrimeiro_aberto;
	}
	public void setfPrimeiro_aberto(TNo fPrimeiroAberto) {
		fPrimeiro_aberto = fPrimeiroAberto;
	}
	public TNo getfUltimo_aberto() {
		return fUltimo_aberto;
	}
	public void setfUltimo_aberto(TNo fUltimoAberto) {
		fUltimo_aberto = fUltimoAberto;
	}
	public TNo getfPrimeiro_visitado() {
		return fPrimeiro_visitado;
	}
	public void setfPrimeiro_visitado(TNo fPrimeiroVisitado) {
		fPrimeiro_visitado = fPrimeiroVisitado;
	}
	public TNo getfNo_atual() {
		return fNo_atual;
	}
	public void setfNo_atual(TNo fNoAtual) {
		fNo_atual = fNoAtual;
	}
	
	public boolean findNo(TNo startNo, Integer jarra1, Integer jarra2){
		
		TNo temp = new TNo();
		temp = startNo;
		
		while(temp != null){
			if(temp.getJarra1().equals(jarra1) && temp.getJarra2().equals(jarra2)){
				return true;
			}
			
			temp = temp.getProximo_no();
		}
				
		return false;
		
	}
	
	public boolean naoEnconta(Integer jarra1, Integer jarra2){
		return findNo(this.fPrimeiro_aberto, jarra1, jarra2);
	}
	
	public boolean naoVisitado(Integer jarra1, Integer jarra2){
		return findNo(this.fPrimeiro_visitado, jarra1, jarra2);
	}
	
	public void novoNo(Integer jarra1, Integer jarra2, TNo noPai){
		
		TNo novoNo = null;
		
		if(!naoVisitado(jarra1, jarra2) && !naoEnconta(jarra1, jarra2)){
			novoNo = new TNo(jarra1, jarra2, noPai);
			
			if(getfUltimo_aberto() != null){
				getfUltimo_aberto().setProximo_no(novoNo);
			}
			
			setfUltimo_aberto(novoNo);
			
			if(getfPrimeiro_aberto() == null){
				setfPrimeiro_aberto(novoNo);
			}
		}		
	}
	
	public TNo selecionaUltimo(){
		return getfUltimo_aberto();
	}
	
	public void expandeNo(TNo atual){
		if(atual.getJarra1() < 4 ){
			novoNo(4, atual.getJarra2(), atual);
		}
		
		if(atual.getJarra1() < 3 ){
			novoNo(3, atual.getJarra1(), atual);
		}
		
		if(atual.getJarra1() > 0){  
			novoNo(0, atual.getJarra2(), atual);
		}
		
		if(atual.getJarra2() > 0){  
			novoNo(atual.getJarra1(), 0, atual);
		}
		
		if(atual.getJarra1() < 4 && atual.getJarra2() > 0){  
			if(atual.getJarra1() + atual.getJarra2() <= 4){  
		        novoNo(atual.getJarra1() + atual.getJarra2(), 0, atual);
			}
		    else{
		        novoNo(4, atual.getJarra1() + atual.getJarra2() - 4, atual); 
		    }
		}
		
		if (atual.getJarra2() < 3 && atual.getJarra1() > 0){  
			if(atual.getJarra1() + atual.getJarra2() <= 3){  
		       novoNo(0, atual.getJarra1() + atual.getJarra2(), atual);
			}
		     else  
		       novoNo(atual.getJarra1() + atual.getJarra2() - 3, 3, atual);
		}
		
		setfPrimeiro_aberto(atual.getProximo_no());
		getfNo_atual().setProximo_no(getfPrimeiro_visitado());
		setfPrimeiro_visitado(atual);		
	}
	
	
	public static void teste(){  
		
		boolean bolEncontrou;  
		
		TTronco tTronco = new TTronco();
		tTronco.novoNo(0, 0, null);
		bolEncontrou = false;
		tTronco.setfNo_atual(tTronco.getfPrimeiro_aberto());
		
		while(!bolEncontrou && tTronco.getfPrimeiro_aberto() != null){
			bolEncontrou = tTronco.getfNo_atual().getJarra1() == 2 && tTronco.getfNo_atual().getJarra2() == 0;
			
			if (!bolEncontrou){
				tTronco.expandeNo(tTronco.getfNo_atual());
				tTronco.setfNo_atual(tTronco.getfPrimeiro_aberto());
			}
			else{
				while(tTronco.getfNo_atual().getNo_pai() != null){
					System.out.println(tTronco.getfNo_atual().getJarra1() + ", " + tTronco.getfNo_atual().getJarra2());
					tTronco.setfNo_atual(tTronco.getfNo_atual().getNo_pai());
				}
			}
		}
	}
	
	public static void main(String[] args) {
		TTronco.teste();
	}	  
}
public class TNo {

	private Integer jarra1;
	private Integer jarra2;
	private TNo no_pai;
	private TNo proximo_no;
	
	public TNo() {
	}
	
	public TNo(Integer jarra1, Integer jarra2, TNo noPai) {
		this.jarra1 = jarra1;
		this.jarra2 = jarra2;
		no_pai = noPai;
	}

	public Integer getJarra1() {
		return jarra1;
	}

	public void setJarra1(Integer jarra1) {
		this.jarra1 = jarra1;
	}

	public Integer getJarra2() {
		return jarra2;
	}

	public void setJarra2(Integer jarra2) {
		this.jarra2 = jarra2;
	}

	public TNo getNo_pai() {
		return no_pai;
	}

	public void setNo_pai(TNo noPai) {
		no_pai = noPai;
	}

	public TNo getProximo_no() {
		return proximo_no;
	}

	public void setProximo_no(TNo proximoNo) {
		proximo_no = proximoNo;
	}	
}

Acho que deve ser isso!
Abraço…

Criado 2 de abril de 2012
Ultima resposta 3 de abr. de 2012
Respostas 4
Participantes 5