그리드 워킹 (점수 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
이 포함됩니다.
따라서 위의 솔루션에서 1 차원 배열을 사용하려고합니다. 웹 사이트에 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 –
내 생각은 더 잘 맞는 [codereview.SE] (http://codereview.stackexchange.com/) – amit