過橋問題的JAVA實現

來源:互聯網
上載者:User

標籤:

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<num; i++){
v_source.addElement(array[i]);
}
}

public Vector getSource(){
return v_source;
}

public Vector getDestination(){
return v_destination;
}

/**
* the recursive algorithm.
* @param src : the set of persons in A-side
* @param des : the set of persons in B-side
* @param size : the number of persons in A-side
* @param totalTime : the totalTime has used
*/
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){
System.out.println("A->B:"+src.elementAt(0)+" AND "+ src.elementAt(1));
System.out.println("*****Total Time: "+(totalTime + Math.max((Integer)src.elementAt(0),(Integer)src.elementAt(1)))+"****");
} else if(size >= 3){

// if more than 2 persons in A-Side, use the recursive algorithm.
for(int i=0; i<size; i++){
for(int j=i+1; j<size; j++){

//Pass, A->B

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);

System.out.print("A->B:"+ time1);
System.out.println(" AND "+ 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);

System.out.println("B->A:"+minValue);

passMethod(_src, _des, _src.size(), totalTime + Math.max(time1, time2) + minValue);

}

}
}
}


public static void main(String[] cmd)
{
BridgePass test = new BridgePass();

//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);
}
}

概述:橋兩邊當做集合,遞迴的表示出過去兩個回來一個。

過橋問題的JAVA實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.