【題目描述】
今天無意中看見一道微軟面試題,很有意思,大家一起來看一下:
【本題答案】
這個是一位其他的博主做的答案,我先分享給大家:
#define STATE char
#define PATH char
const int TimeLimit = 17;
STATE State[16] = { 1 };
PATH Path[16];
const int Cop[10] = { 1, 2, 4, 8, 3, 5, 9, 6, 10, 12 };
const char* Cops[10] =
{
"1,耗時 1 分",
"2,耗時 2 分",
"5,耗時 5 分",
"10,耗時 10 分",
"1 和 2,耗時 2 分",
"1 和 5,耗時 5 分",
"1 和 10,耗時 10 分",
"2 和 5,耗時 5 分",
"2 和 10,耗時 10 分",
"5 和 10,耗時 10 分"
};
const int Time[10] = { 1, 2, 5, 10, 2, 5, 10, 5, 10, 10 };
void Ferry ( int state, int dir, int p, int time )
{
int i, cop, j;
for ( i = 0; i < 10; i++ )
{
if ( Cop[i] != (cop = state & Cop[i] ) ) continue;
state &= ~cop;
time += Time[i];
if ( State[ dir ? ~state & 15 : state ] ||
time > TimeLimit )
{
state |= cop;
time -= Time[i];
}
else if ( dir && !state )
{
Path[p] = i;
printf ( "過橋過程,共往返 %d 次:/n", p + 1 );
for ( j = 0; j <= p; j++ )
printf ( "%s: %s/n", ( j & 1 ? "右往左" : "左往右" ), Cops[Path[j]] );
printf ( "/n" );
break;
}
else
{
State[ dir ? ~state & 15 : state ] = 1;
Path[p] = i;
Ferry ( ~state & 15, !dir, p + 1, time );
State[ dir ? ~state & 15 : state ] = 0;
time -= Time[i];
state |= cop;
}
}
}
// 調用時
Ferry ( 15, 1, 0, 0 );
今天無意中想起遞歸算法,突生靈感,如果用Java語言實現“n人過橋”問題,那就有意思了。
遞歸的出口是:“2人過橋”情況。2人過橋,不需要有人返回,所以非常簡單,總時間就是單人所需時間中的最大值。
如果是“n人過橋”(n>=3),那完全可以遞歸了。
假設是從橋頭A至橋頭B,橋頭A的人羣爲一個集合,橋頭B的人羣爲另一個集合。
那麼首先可以從A中任意選擇2個人從A到B;則A集合中減少2個人,B集合中增加2個人;
然後需要一個人從B返回A,這個可以分析出如果想要比較少的時間,一定是從B中選一個單獨需時最短的;此時B中減少一個人,A集合中增加一個人;
之後情況變成了“n-1人過橋”問題。
遞歸思想就開始起作用了。
但是需要注意一點,我在這裏的思想是每次返回的人都是從B集合中選出需時最少的;如果想找出需時最多的,就從B中選出一個需時最大的;如果想找到所有情況,那就需要遍歷B集合,那就比較複雜了,我沒有考慮。
如下是我的代碼,由於時間倉促,沒有規範化處理。。。比如passMethod中的參數列表中第三個參數其實是可以去掉的,因爲它就是橋頭A端得人數,可以從第一個參數中獲得。
由於時間有限,我在這裏就不改動了。
讀者可以自己個界面,允許人/機交互,如果深入思考,還是很有意思的。
package blog; import java.util.Vector; /** * @author yesr * @create 2018-04-22 上午12:05 * @desc **/ 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++) { System.out.println("i=" + i + "j=" + j); //Pass, A->B Vector _src = new Vector(); Vector _des = new Vector(); _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, 10}; test.setSource(source, source.length); test.passMethod(test.getSource(), test.getDestination(), source.length, 0); } }