Function
思路
记忆化搜索,除了边界条件,把其他结果存入数组中再进行返回
import java.util.Scanner;
public class Main {
static long[][][] arr = new long[25][25][25];
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while(true){
long a = cin.nextLong();
long b = cin.nextLong();
long c = cin.nextLong();
if(a==-1 && b==-1 && c==-1) break;
System.out.printf("w(%d, %d, %d) = %d\n",a,b,c,fun(a,b,c));
}
}
static long fun(long a, long b, long c){
if(a<=0 || b<=0 || c<=0)
return 1;
if(a>20 || b>20 || c>20)
return 1048576;
if(arr[(int) a][(int) b][(int) c] != 0)
return arr[(int) a][(int) b][(int) c];
long temp = 0;
if(a<b && b<c){
temp = (fun(a, b, c-1) + fun(a, b-1, c-1) -
fun(a, b-1, c));
}else{
temp = fun(a-1, b, c) + fun(a-1, b-1, c) +
fun(a-1, b, c-1) - fun(a-1,b-1,c-1);
}
return arr[(int) a][(int) b][(int) c] = temp;
}
}