左神_基础提升班_08_暴力递归到动态规划02

1.排成一条线的纸牌博弈问题
(1)暴力解法

	public static int win1(int[] arr) {
		if(arr==null||arr.length==0){
			return 0;
		}
		return Math.max(f(arr,0,arr.length-1),s(arr,0,arr.length-1));
	}
	//先手函数
	public static int f(int[] arr, int i, int j) {
		if(i==j){
			return arr[i];
		}
		return Math.max(arr[i]+s(arr,i+1,j),arr[j]+s(arr,i,j-1));
	}
	//后手函数
	public static int s(int[] arr, int i, int j) {
		if(i==j){
			return 0;
		}
		return Math.min(f(arr,i+1,j),f(arr,i,j-1));
	}

(2)严格表dp

	public static int win2(int[] arr) {
		if(arr==null||arr.length==0){
			return 0;
		}
		int [][] f=new int[arr.length][arr.length];
		int [][] s=new int[arr.length][arr.length];
		for(int j=0;j<arr.length;j++){
			f[j][j]=arr[j];
			s[j][j]=0;
			for(int i=j-1;i>=0;i--){
				f[i][j]=Math.max(arr[i]+s[i+1][j],arr[j]+s[i][j-1]);
				s[i][j]=Math.min(f[i+1][j],f[i][j-1]);
			}
		}
		return Math.max(f[0][arr.length-1],s[0][arr.length-1]);
	}

2.马踏棋盘(从(0,0)走到(x,y)需要多少步)
(1)暴力解法

	public static int getWays(int x, int y, int step) {
		return process(x,y,step);
	}

	public static int process(int x, int y, int step) {
		if(x<0||x>8||y<0||y>9){
			return 0;
		}
		if(step==0){
			return (x==0&&y==0)?1:0;
		}
		return process(x+1,y+2,step-1)
				+process(x+1,y-2,step-1)
				+process(x+2,y+1,step-1)
				+process(x+2,y-1,step-1)
				+process(x-1,y+2,step-1)
				+process(x-1,y-2,step-1)
				+process(x-2,y+1,step-1)
				+process(x-2,y-1,step-1);
	}

(2)严格表dp(三维立体)

	public static int dpWays(int x, int y, int step) {
		if(x<0||x>8||y<0||y>9){
			return 0;
		}
		int [][][] dp=new int[9][10][step+1];//注意这里是9,10
		dp[0][0][0]=1;
		for(int h=1;h<=step;h++){
			for (int i=0;i<=8;i++){
				for (int j=0;j<=9;j++){
					dp[i][j][h]=getValue(dp,i+1,j+2,h-1)      //getValue为了不越界
							+getValue(dp,i+1,j-2,h-1)
							+getValue(dp,i+2,j+1,h-1)
							+getValue(dp,i+2,j-1,h-1)
							+getValue(dp,i-1,j+2,h-1)
							+getValue(dp,i-1,j-2,h-1)
							+getValue(dp,i-2,j+1,h-1)
							+getValue(dp,i-2,j-1,h-1);
				}
			}
		}
		return dp[x][y][step];
	}

	public static int getValue(int[][][] dp, int row, int col, int step) {
		if(row<0||row>8||col<0||col>9){
			return 0;
		}
		return dp[row][col][step];
	}

3.Bob的生存概率
(1)暴力解法

	public static String bob1(int N, int M, int i, int j, int K) {
		long all = (long) Math.pow(4, K);
		long live = process(N, M, i, j, K);
		long gcd = gcd(all, live);
		return String.valueOf((live / gcd) + "/" + (all / gcd));
	}

	public static long process(int N, int M, int row, int col, int rest) {
		if (row < 0 || row == N || col < 0 || col == M) {
			return 0;
		}
		if (rest == 0) {
			return 1;
		}
		long live = process(N, M, row - 1, col, rest - 1);
		live += process(N, M, row + 1, col, rest - 1);
		live += process(N, M, row, col - 1, rest - 1);
		live += process(N, M, row, col + 1, rest - 1);
		return live;
	}

	public static long gcd(long m, long n) {
		return n == 0 ? m : gcd(n, m % n);
	}

(2)严格表dp

	public static String bob2(int N, int M, int i, int j, int K) {
		int[][][] dp = new int[N + 2][M + 2][K + 1];
		for (int row = 1; row <= N; row++) {
			for (int col = 1; col <= M; col++) {
				dp[row][col][0] = 1;
			}
		}
		for (int rest = 1; rest <= K; rest++) {
			for (int row = 1; row <= N; row++) {
				for (int col = 1; col <= M; col++) {
					dp[row][col][rest] = dp[row - 1][col][rest - 1];
					dp[row][col][rest] += dp[row + 1][col][rest - 1];
					dp[row][col][rest] += dp[row][col - 1][rest - 1];
					dp[row][col][rest] += dp[row][col + 1][rest - 1];
				}
			}
		}
		long all = (long) Math.pow(4, K);
		long live = dp[i + 1][j + 1][K];
		long gcd = gcd(all, live);
		return String.valueOf((live / gcd) + "/" + (all / gcd));
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值