P1464 Function

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

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值