package 过桥问题;
import java.util.Vector;
public class BridgePass {
private Vector v_source = null;
private Vector v_destination = null;
private static int time_total = 0;
public BridgePass()
{
v_source = new Vector();
v_destination = new Vector();
}
public void setSource(int标致的香氛 array, int num){
for(int i=0; i
v_source.addElement(array[i]);
}
}
public Vector getSource(){
return v_source;
}
public Vector getDestination(){
return v_destination;
}
/**
public void passMethod(Vector src, Vector des, int size, int totalTime)
{
//If only 2 persons in A-side, just pass bridge together in one time.
if(size == 2){
} else if(size >= 3){
// if more than 2 persons in A-Side, use the recursive algorithm.
for(int i=0; i
for(int j=i+1; j
//Pass, A->dcb/p>
Vector _src = null;
Vector _des = null;
_src = (Vector)src.clone();
_des = (Vector)des.clone();
int time1 = 0;
int time2 = 0;
time1 = (Integer)_src.elementAt(i);
_des.addElement(time1);
time2 = (Integer)_src.elementAt(j);
_des.addElement(time2);
_src.removeElement(time1);
_src.removeElement(time2);
//BACK, B->A
int minValue = (Integer)_des.elementAt(0);
for(int k=0 ; k<_des.size k>
if(((Integer)_des.elementAt(k)).intValue() < minValue){
minValue = (Integer)_des.elementAt(k);
}
}
_src.addElement(minValue);
_des.removeElement(minValue);
passMethod(_src, _des, _src.size(), totalTime gtdhb(time1, time2) + minValue);
}
}
}
}
public static void main(String标致的香氛 cmd)
{
//the persons want to pass bridge:
int source标致的香氛 = {1,2,5,8,10};
test.setSource(source, source.length);
test.passMethod(test.getSource(), test.getDestination(), source.length, 0);
}
}
概述:桥两边当做集合,递归的表示出过去两个回来一个。