青蛙过河问题

package coding;

import java.util.ArrayList;
import java.util.List;

  class FrogOverRiver {
    public List<Frog>initializeFrogQueue(){
    List<Frog>frogQueue=new ArrayList<Frog>();
    frogQueue.add(new Frog(0,"左1",Frog.frogDirection.向右,false));
    frogQueue.add(new Frog(1,"左2",Frog.frogDirection.向右,true));
    frogQueue.add(new Frog(2,"左3",Frog.frogDirection.向右,true));
    frogQueue.add((new Frog(3)));
    frogQueue.add(new Frog(3,"右1",Frog.frogDirection.向左,true));
    frogQueue.add(new Frog(4,"右1",Frog.frogDirection.向左,true));
    frogQueue.add(new Frog(5,"右1",Frog.frogDirection.向左,false));
    return frogQueue;
    }
    private List<Frog>editFrogQueue(List<Frog>frogQueue,String frogName,int oldEmptyPositionID,int newEmptyPositionID){
    List<Frog>newFrogQueue=new ArrayList<Frog>();
    for(int i=0;i<frogQueue.size();i++){
        Frog frog=(Frog)frogQueue.get(i);
        Frog newFrog=new Frog(frog);
        if(newFrog.isEmpty)
            newFrog.position=newEmptyPositionID;
        if(newFrog.frogName==frogName){
            newFrog.position=oldEmptyPositionID;
        }
        newFrog.canJump=false;
        if((newEmptyPositionID-newFrog.position)>0&&(newEmptyPositionID-newFrog.position)<3&&newFrog.direction==Frog.frogDirection.向右)
            newFrog.canJump=true;
        if((newFrog.position-newEmptyPositionID)>0&&(newFrog.position-newEmptyPositionID)<3&&newFrog.direction==Frog.frogDirection.向左)
            newFrogQueue.add(newFrog);
    }
    return newFrogQueue;
    }
    private boolean isComplete(List<Frog>frogQueue){
        return(frogQueue.get(0).position>3&&frogQueue.get(1).position>3&&frogQueue.get(2).position>3);
    }
    private boolean canFrogJump(List<Frog>frogQueue){
        for(int i=0;i<frogQueue.size();i++){
            Frog frog=(Frog)frogQueue.get(i);
            if(frog.canJump)
                return true;
        }
        return false;
    }

    public String frogJump(List<Frog>frogQueue,int emptyPositionId){
        String frogJumpInfo="";
        for(int i=0;i<frogQueue.size();i++){
            Frog frog=(Frog)frogQueue.get(i);
            if(frog.isEmpty)
                continue;
            if(!frog.canJump)
                continue;
        frogJumpInfo="青蛙"+frog.frogName+" "+frog.direction+"跳到"+(emptyPositionId)+"\r\n";
        int newPositionId=frog.position;
        List<Frog>newFrogQueue=this.editFrogQueue(frogQueue,frog.frogName,emptyPositionId,newPositionId);
        if(this.canFrogJump(newFrogQueue)){
            frogJumpInfo+=this.frogJump(newFrogQueue,newPositionId);
        }else{
            if(this.isComplete(newFrogQueue)){
                frogJumpInfo=frogJumpInfo+"成功";
                break;
            }
        }
        if(frogJumpInfo.contains("成功"))
            break;
        }
        return frogJumpInfo;
    }
  }

  class Frog{
      static enum frogDirection{向左,向右};
      public String frogName;
      public int position;
      public frogDirection direction;
      public boolean canJump;
      public boolean isEmpty=false;

      public Frog(int position,String frogName,frogDirection direction,boolean canJump){
          this.position=position;
          this.frogName=frogName;
          this.direction=direction;
          this.canJump=canJump;
      }

      public Frog(int position){
          this.frogName="空";
          this.position=position;
          this.canJump=false;
          this.isEmpty=true;
      }

      public Frog(Frog frog){
          this.position=frog.position;
          this.frogName=frog.frogName;
          this.direction=frog.direction;
          this.canJump=frog.canJump;
          this.isEmpty=frog.isEmpty;
      }
  }

  public class cha09_frog{
    public static void main(String[] args) {
        // TODO Auto-generated method stub
      FrogOverRiver f=new FrogOverRiver();
      List frogQueue=f.initializeFrogQueue();
      System.out.println(f.frogJump(frogQueue,3));
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值