问题1:1-N,从cur开始,到aim结束,共rest步,共多少种方法
1.1递归
public static int process1(int cur,int rest,int aim,int N)
{
if(rest==0)
return cur==aim?1:0;
if(cur==1)
return process1(2,rest-1,aim,N);
if(cur==N)
return process1(cur-1,rest-1,aim,N);
return process1(cur-1,rest-1,aim,N)+process1(cur+1,rest-1,aim,N);
}
1.2递归+傻缓存
public static int process2(int cur,int rest,int aim,int N,int dp[][])
{
if(dp[cur][rest]!=-1)
return dp[cur][rest];
int ans;
if(rest==0)
ans= cur==aim?1:0;
else if(cur==1)
ans= process2(2,rest-1,aim,N,dp);
else if(cur==N)
ans= process2(cur-1,rest-1,aim,N,dp);
else
ans= process2(cur-1,rest-1,aim,N,dp)+process2(cur+1,rest-1,aim,N,dp);
dp[cur][rest]=ans;
return ans;
}
1.3动态规划
状态转移方程
rest=0时,cur=aim,dp[aim][0]=1;
cur!=aim,cur=dp[aim][0]=0;
rest!=0时,cur=1,dp[cur][rest]=dp[cur+1][rest-1]
1<cur<N,dp[cur][rest]=dp[cur-1][rest-1]+dp[cur+1][rest+1]
cur=N,dp[cur][rest]=dp[cur-1][rest-1];
public static int process3(int cur,int rest,int aim,int N)
{
if(N<2||cur<1||cur>N||aim<1||aim>N||rest<1)
return -1;
int dp[][]=new int[N+1][rest+1];
dp[aim][0]=1;
for(int i=1;i<=rest;i++)
{
dp[1][i]=dp[2][i-1];
dp[N][i]=dp[N-1][i-1];
for(int j=2;j<N;j++)
dp[j][i]=dp[j-1][i-1]+dp[j+1][i-1];
}
for(int i=0;i<=cur;i++)
{
for(int j=0;j<=rest;j++)
{
System.out.print(dp[i][j]+"\t");
}
System.out.println();
}
return dp[cur][rest];
}
问题二
2.1递归
//后手
public static int g(int left,int right,int arr[])
{
if(left==right)
return 0;
return Math.min(f(left+1,right,arr),f(left,right-1,arr));
}
//先手
public static int f(int left, int right, int arr[])
{
if(left==right)
return arr[left];
else
return Math.max(g(left+1,right,arr)+arr[left],g(left,right-1,arr)+arr[right]);
}
public static void main(String[] args) {
// write your code here
int arr[]={12,23,23,24,3,4,354,35};
if(arr!=null&&arr.length>0)
//先手后手谁赢?
System.out.println(Math.max(f(0,arr.length-1,arr),g(0,arr.length-1,arr)));
}
2.2递归+啥内存
//后手
public static int g(int left,int right,int arr[],int [][] dpf,int [][]dpg)
{
if(dpg[left][right]!=-1)
return dpg[left][right];
int ans;
if(left==right)
ans= 0;
else
ans= Math.min(f(left+1,right,arr,dpf,dpg),f(left,right-1,arr,dpf,dpg));
dpg[left][right]=ans;
return ans;
}
//先手
public static int f(int left, int right, int arr[],int [][] dpf,int [][]dpg)
{
if(dpf[left][right]!=-1)
return dpf[left][right];
int ans;
if(left==right)
ans=arr[left];
else
ans= Math.max(g(left+1,right,arr,dpf,dpg)+arr[left],g(left,right-1,arr,dpf,dpg)+arr[right]);
dpf[left][right]=ans;
return ans;
}
public static void main(String[] args) {
// write your code here
int arr[]={12,23,23,24,3,4,354,35};
int dpf[][]=new int[arr.length+1][arr.length+1];
int dpg[][]=new int[arr.length+1][arr.length+1];
for(int i=0;i<=arr.length;i++)
for(int j=0;j<=arr.length;j++)
{
dpf[i][j]=-1;
dpg[i][j]=-1;
}
if(arr!=null&&arr.length>0)
//先手后手谁赢?
System.out.println(Math.max(f(0,arr.length-1,arr,dpf,dpg),g(0,arr.length-1,arr,dpf,dpg)));
}
2.3动态规划
public static void main(String[] args) {
// write your code here
int arr[] = {12, 23, 23, 24, 3, 4, 354, 35};
int dpf[][] = new int[arr.length][arr.length];
int dpg[][] = new int[arr.length][arr.length];
for (int i = 0; i < arr.length; i++)
dpf[i][i] = arr[i];
for (int i = 1; i < arr.length; i++) {
int col = i, row = 0;
while (col < arr.length) {
dpf[row][col] = Math.max(dpg[row][col - 1]+arr[col],dpg[row + 1][col]+arr[row]);
dpg[row][col] = Math.min(dpf[row][col - 1],dpf[row + 1][col]);
col++;
row++;
}
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
System.out.print(dpf[i][j] + "\t");
}
System.out.println();
}
System.out.println(
);
System.out.println();
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
System.out.print(dpg[i][j] + "\t");
}
System.out.println();
}
System.out.println(Math.max(dpf[0][arr.length - 1], dpg[0][arr.length - 1]));
}