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));
}
}
青蛙过河问题
最新推荐文章于 2023-04-02 21:28:30 发布