java--recursion (dynamic planning, backtracking)

Source: Internet
Author: User
Tags stub

Recently brush face question often brush to recursive aspects of the algorithm, always thought is recursive, and later found that all have specific names, so wrote this blog to keep in mind the following

1. Five most common algorithms

(1) Divide and conquer algorithm

Divide a complex problem into two or more identical or similar sub-problems, then subdivide it continuously until the final sub-problem can be solved very simply, the solution of the original problem is from the merger of the problem. such as the common fast sorting algorithm and the merging algorithm

The core idea of divide-and-conquer law is to divide and divide the problems of large and difficult solutions.

(2) Dynamic planning

Similar to the partition method, the problem of solving is divided into several small problems, and the solution of each small problem will affect the solution of the original problem.

The local solutions of each sub-problem are first obtained, and then the local solutions that can reach the optimal solution are preserved by decision.

Can be decomposed into several sub-problems, and there is a cross between sub-problems

(3) Backtracking algorithm

A process similar to an enumeration is, in fact, a process of accomplishment, a tree's depth-first search.

During the search attempt, when the current node is found to be unable to satisfy the solve condition, it returns to its parent node and continues to try another path

(4) Branch boundary method

Similar to the backtracking algorithm, but not the depth-first search, but the breadth-first search, usually use the queue (FIFO) to each node to judge.

(5) Greedy algorithm

When solving the problem, we always find the local optimal solution rather than the overall optimal.

The premise of greedy algorithm: local optimal strategy can eventually produce global optimal solution

2. Recursion (personal feeling this learned, algorithm just how to call recursion)

First, the simplest recursive code, the Fibonacci sequence.

 Public classFibo { Public int[] Array =New int[100]; {array[0]=0; array[1]=1; array[2]=1;}  Public intFibonacci (intN) {        if(n==1| | n==2){            return1; }Else{            return  This. Array[n]=fibonacci (n-1) +fibonacci (n-2); }            }     Public Static voidMain (string[] args) {//TODO auto-generated Method StubFibo fb =NewFibo (); SYSTEM.OUT.PRINTLN (FB. Fibonacci (3)); }}

Of course, can also be a pseudo-recursive, is to set up an array, the answer is stored first, and then directly return the results. Sword refers to the offer on many direct recursion is not pass, must be pseudo-recursive to

Another frog jumping step code, the title is also the sword refers to the offer on the

//a frog can jump up to 1 steps at a time, or jump up to level 2. //to ask the frog to jump on an n-level stair total number of hops Public classJumpfloor { Public intJump (inttarget) {        if(target==0){            return0; }Else if(target==1){            return1; }Else if(target==2){            return2; }Else{            int[] Array =New int[Target+1]; array[0]=0; array[1]=1; array[2]=2;  for(inti=3;i<array.length;i++) {Array[i]=array[i-1]+array[i-2]; }            returnArray[target]; }            }        //a frog can jump up to 1 steps at a time, you can jump up to 2 levels, jump 3, 4 ... n-1. //to ask the frog to jump on an n-level stair total number of hops     Public intJUMPII (inttarget) {        if(target==0){            return0; }Else if(target==1){            return1; }Else if(target==2){            return2; }Else{            int[] Array =New int[Target+1]; array[0]=0; array[1]=1; array[2]=2; intsum = 3;  for(inti=3;i<array.length;i++) {Array[i]=sum+1; Sum= sum+Array[i]; }            returnArray[target]; }            }         Public Static voidMain (string[] args) {//TODO auto-generated Method StubJumpfloor JF =NewJumpfloor (); SYSTEM.OUT.PRINTLN (JF. Jump (3)); SYSTEM.OUT.PRINTLN (JF. Jumpii (3)); }}

This is also implemented with pseudo-recursive return.

3. The nature of recursion

In fact, the essence of recursion is to find the relationship between the front and back, find the formula of recursion

such as f (n) = f (n-1) + f (n-2), etc.

The general situation of recursion is

(1). if (Meet recursive end condition), return value

(2). else, continue recursion

4. The nature of backtracking is actually improved on the basis of recursion

(1). if (a condition that does not satisfy the continuation of a recursive lookup, usually a boundary judgment), returns

(2). if (Meet search criteria), record down, continue down

(3). Join this node to update the conditions and values

(4). Recursion left

(5). Recursion to the right

(6). Delete this node (backtracking)

For example, an example is a question from ZTE, which I have improved. In fact, all this kind of energy supply, blood bar supply are the same solution.

ImportJava.util.ArrayList;
Input//Supply Station number, [Recharge Station distance],[recharge Station Energy], destination distance (total energy required), initial energy//3, [5, 7, ten], [2, 3, 5],, 5//5, [10, 20, 22, 23, 26], [10, 2, 5, 1, 1], 30, 10
Output
All the solutions that can be reached cannot reach the return-1
Importjava.util.Arrays;ImportJava.util.Scanner; Public classPowergain {PrivateArraylist<arraylist<integer>> Listall =NewArraylist<arraylist<integer>>(); Privatearraylist<integer> list =NewArraylist<integer>(); PublicArraylist<arraylist<integer>> Minnumber (intNint[] distince,int[] Judce,intLeftintRightintDestinationintPower) { //System.out.println (left+ "" +destination+ "" +power "); if(Left>right | | power<=0){ returnListall; } //if (Power>=distince[left]) {List.add (left); //} if(power>=destination) {Listall.add (NewArraylist<integer>(list)); //return listall; } intNewleft = left+1; //don't drink, go on, drink, move on.Minnumber (n, Distince, Judce, Newleft, N, Destination-distince[left], power-Distince[left]); Minnumber (n, Distince, Judce, Newleft, N, Destination-distince[left], power-distince[left]+Judce[left]); List.remove (List.size ()-1); returnListall; } Public voidOutprint (arraylist<arraylist<integer>>Tempall) { if(Tempall.size () ==0) {System.out.println (-1); return; } for(intI=0;i<tempall.size (); i++) {ArrayList<Integer> temp =Tempall.get (i); for(intJ=1;j<temp.size (); j + +) {System.out.print (Temp.get (j)); if(J!=temp.size ()-1) {System.out.print (" "); }} System.out.println (); } } Public Static voidMain (string[] args) {//TODO auto-generated Method StubScanner input =NewScanner (system.in); String St=Input.nextline (); String[] Sarray= St.split (","); intn = integer.parseint (sarray[0]); int[] Distince =New int[N+1]; int[] Judce =New int[N+1]; intindex = 1;//Sarray Subscript for(inti=0;i<n;i++){ if(i==0) {Distince[i]= Integer.parseint (sarray[index].substring (1)); Judce[i]= Integer.parseint (sarray[index+n].substring (1)); }Else if(i==n-1) {Distince[i]= Integer.parseint (sarray[index].substring (0, Sarray[index].length ()-1)); Judce[i]= Integer.parseint (sarray[index+n].substring (0, Sarray[index+n].length ()-1)); }Else{Distince[i]=Integer.parseint (Sarray[index]); Judce[i]= Integer.parseint (sarray[index+n]); } Index++; } Index= 2*n+1; intDestination =Integer.parseint (Sarray[index]); intPower = Integer.parseint (sarray[index+1]); //Length SegmentJudce[n] = 0; Distince[n]=destination-distince[n-1]; for(inti=n-1;i>0;i--) {Distince[i]=distince[i]-distince[i-1]; } powergain M=NewPowergain () {}; ArrayList<ArrayList<Integer>> result = M.minnumber (n, distince, JUDCE, 0, N, Destination, power); M.outprint (result); }}

java--recursion (dynamic planning, backtracking)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.