Here is an Example of recursion.
The codes are incentive to calculate all possible results if you have N tasks being delievered.
for more detail, please contact jimworks@gmail.com.
package forfun;
import java.util.ArrayList;
import java.util.Stack;
/**
*
* @author raymond
*/
public class Tsp {
private Stack<Integer> sp;
private Stack<Integer> sd = new Stack<Integer>();
private Stack<Integer> sr = new Stack<Integer>();
private int counter = 0;
private ArrayList<ArrayList> alro = new ArrayList<ArrayList>();
public ArrayList<ArrayList> getAlro() {
return alro;
}
public Tsp(Stack<Integer> sp)
{
this.sp = sp;
this.sr.add(0);
searchp(this.sp,sd,sr,sp.size());
}
private void searchp(Stack<Integer> p,Stack<Integer> d,Stack<Integer> r,int n)
{
int all = p.size()+d.size();
for(int i=0;i<all;i++)
{
if(i<p.size())
{
int pp = p.get(i);
int v1 = pp + n;
d.push(v1);
r.push(pp);
p.remove(i);
searchp(p,d,r,n);
r.pop();
d.pop();
p.add(i, pp);
}
else
{
int dp = d.get(i-p.size());
r.push(dp);
d.remove(i-p.size());
searchp(p,d,r,n);
r.pop();
d.add(i-p.size(),dp);
}
}
if(d.isEmpty())
{
ArrayList<Integer> alr = new ArrayList<Integer>();
for(int j=0;j<r.size();j++)
{
alr.add(r.get(j));
}
// alro.add(alr);
counter ++;
}
}
public int getCounter() {
return counter;
}
public static void main(String[] args) {
Stack<Integer> sp = new Stack<Integer>();
sp.push(1);
sp.push(2);
sp.push(3);
sp.push(4);
sp.push(5);
sp.push(6);
sp.push(7);
Tsp t = new Tsp(sp);
System.out.println("c="+(t.getCounter()-1));
// ArrayList<ArrayList> alro = t.getAlro();
// System.out.println("size="+(alro.size()-1));
// for(int i=0;i<alro.size();i++)
// {
// ArrayList<Integer> s = alro.get(i);
// for(int j=0;j<s.size();j++)
// {
// System.out.print(s.get(j) +" ");
// }
// System.out.println("");
// }
}
}