柵格步行(得分50分): 您位於N維柵格位置(x_1,x2,...,x_N)
。網格的尺寸是(D_1,D_2,...D_N)
。在一個步驟中,您可以在任何一個N
維度中前後走一步。 (所以總有2N
可能的不同動作)。你有多少種方法可以採取M
步驟,以便在任何時候都不會離開電網?如果您有任何x_i
,或者x_i <= 0 or x_i > D_i
,請離開電網。柵格步行算法代碼校正
輸入: 第一行包含測試用例的數量T
。 T
測試用例如下。對於每個測試用例,第一行包含N
和M
,第二行包含x_1,x_2...,x_N
,第三行包含D_1,D_2,...,D_N
。
因此,在上面的解決方案,我試圖採取一維數組。 該網站聲稱38753340
是答案,但我沒有得到它。
public class GridWalking {
/**
* @param args
*/
public static void main(String[] args) {
try {
long arr[] = new long[78];
long pos = 44;
long totake = 287;
/*
* Double arr[] = new Double[3]; Double pos = 0; Double totake = 5;
*/
Double val = calculate(arr, pos, totake);
System.out.println(val % 1000000007);
} catch (Exception e) {
System.out.println(e);
e.printStackTrace();
}
}
public static HashMap<String, Double> calculated = new HashMap<String, Double>();
private static Double calculate(long[] arr, long pos, long totake) {
if (calculated.containsKey(pos + "" + totake)) {
return calculated.get(pos + "" + totake);
}
if (0 == totake) {
calculated.put(pos + "" + totake, new Double(1));
return new Double(1);
}
if (pos == arr.length - 1) {
Double b = calculate(arr, pos - 1, totake - 1);
Double ret = b;
calculated.put(pos + "" + totake, new Double(ret));
return ret;
}
if (pos == 0) {
Double b = calculate(arr, pos + 1, totake - 1);
Double ret = b;
calculated.put(pos + "" + totake, new Double(ret));
return ret;
}
Double a = calculate(arr, pos + 1, totake - 1);
Double b = calculate(arr, pos - 1, totake - 1);
Double ret = (a + b);
calculated.put(pos + "" + totake, ret);
return ret;
}
}
https://www.interviewstreet.com/challenges/dashboard/#problem/4e48bfab1bc3e – 2012-08-12 10:50:42
我認爲最好配合[codereview.SE(http://codereview.stackexchange.com/) – amit 2012-08-12 11:26:44