度小满后台笔试第二题求解
class ttt3{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int N = scanner.nextInt(); int A = scanner.nextInt(); int B = scanner.nextInt(); int C = scanner.nextInt(); int[] map = new int[N+1]; for (int i = 0; i < N; i++) { map[i+1] = scanner.nextInt(); } System.out.println(so(1,map,N,A,B,C)); } static int so(int curr,int[] map,int n,int a,int b,int c){ //System.out.println(curr+":"+"->"+n+Arrays.toString(map)); if (curr == n){ return 0; } int r1=Integer.MAX_VALUE; if (map[curr]>1) { map[curr]--; r1= b + so(curr, map, n, a, b, c); //System.out.println("r1:"+r1); map[curr]++; } int r2=Integer.MAX_VALUE; if (map[curr]<n) { map[curr]++; r2 =c + so(curr, map, n, a, b, c); //System.out.println("r2:"+r2); map[curr]--; } int r3 = a+so(map[curr],map,n,a,b,c); return Math.min(r3,Math.min(r1,r2)); } }
暴力递归,提示堆栈溢出,请大牛看看哪里有问题?