package datastruct; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class DPTest { //动态规划练习题 public static void main(String[] args) { //青蛙回家问题 frogBackHome(); } //青蛙可以一次跳一步二步三步,它不喜欢碰石子,石子在某几点上(会列出N点有石子)问要怎么以碰到最少的石子为代价家对岸 private static void frogBackHome(){ /*解: 如果i的位置上有石子 DP[i] = Min(DP[i-1],DP[i-2],DP[i-3]) + 1 如果i的位置上没有石子 DP[i] = Min(DP[i-1],DP[i-2],DP[i-3]) 应该就搞定了,O(n)的 */ int[] stone = {0,0,1,1,0,0,1,0,1,0,0}; int bu = 10; int ge = 10; int maxBu = 3; int[][] matrix = new int[bu+1][ge+1]; String[][] path = new String[bu+1][ge+1]; for(int i = 0 ; i < bu + 1 ; i++){ matrix[i][0] = i==0?0:10000; path[i][0]="0,0"; } for(int j = 0 ; j < ge + 1 ; j++){ matrix[0][j] = 0; path[0][j]="0,0"; } for(int i = 1 ; i < bu + 1 ; i++){ for(int j = 1 ; j < ge + 1 ; j++){ int limit = 0; if(ge+1 > maxBu * i){ limit = maxBu * i + 1; }else{ limit = ge + 1; } if(i <= j && j < limit){ int val1 = 0; int val2 = 0; int val3 = 0; Label label1 = new Label(); if(j-3 < 1){ val1 = 10000; label1.setV(val1); }else{ val1 = matrix[i-1][j-3]; label1.setV(val1); } label1.setI(i-1); label1.setJ(j-3<1?0:j-3); Label label2 = new Label(); if(j-2 < 1){ val2 = 10000; label2.setV(val2); }else{ val2 = matrix[i-1][j-2]; label2.setV(val2); } label2.setI(i-1); label2.setJ(j-2<1?0:j-2); Label label3 = new Label(); if(j-1 < 1){ val3 = 10000; label3.setV(val3); }else{ val3 = matrix[i-1][j-1]; label3.setV(val3); } label3.setI(i-1); label3.setJ(j-1<1?0:j-1); if(stone[j] == 1){ Label l = min(label1, label2, label3); if(l != null ){ if(l.i > 0 && l.j > 0) path[i][j]=l.i+","+l.j; matrix[i][j] = l.getV()+1; }else{ path[i][j]=null; matrix[i][j] = 0; } }else{ Label l = min(label1, label2, label3); if(l != null){ if(l.i > 0 && l.j > 0) path[i][j]=l.i+","+l.j; matrix[i][j] = l.getV(); }else{ path[i][j]=null; matrix[i][j] = 0; } } }else matrix[i][j]=10000; } } System.out.println("----------------青蛙跳步矩阵------------------------"); for(int i = 0 ; i < bu + 1 ; i++){ for(int j = 0 ; j < ge + 1 ; j++){ System.out.print(matrix[i][j] +"\t"); } System.out.println(); } System.out.println("----------------青蛙位置矩阵------------------------"); for(int i = 0 ; i < bu + 1 ; i++){ for(int j = 0 ; j < ge + 1 ; j++){ System.out.print(path[i][j] +"\t"); } System.out.println(); } System.out.println("----------------可能的最优结果------------------------"); } private static Label min(Label i,Label j, Label k){ List<Label> list = new ArrayList<Label>(); if(i.v != 10000){ list.add(i); } if(j.v != 10000){ list.add(j); } if(k.v != 10000){ list.add(k); } Collections.sort(list,new Comparator<Label>(){ @Override public int compare(Label o1, Label o2) { if((o1.v) > o2.v){ return 1; } return -1; } }); return list.size() > 0 ? list.get(0) : null; } static class Label{ private int i; private int j; private int v; public int getI() { return i; } public void setI(int i) { this.i = i; } public int getJ() { return j; } public void setJ(int j) { this.j = j; } public int getV() { return v; } public void setV(int v) { this.v = v; } } }