我正在用Java进行动态编程,结果被卡住了: 在任务中,我们获得了一系列带有随机数量内部岩石的桶,两个玩家从一开始就知道它们的数量。玩家进行巡回演出,并从侧面选择一个边防桶:
桶:(3)(3)(8)(2)(10)(4)
P1:还剩下(3)个存储桶:(3)(8)(2)(10)(4),
P2:还剩下(4)个存储桶:(3)(8)(2)(10),
P1:还剩(10)个存储桶:(3)(8)(2),
剩余P2(3)个存储桶:(8)(2),
P1:(8)个桶剩下(2)个,
P2:(2)结尾
最终得分是通过(玩家1的摇滚)-(玩家2的摇滚)
计算的得分=(3 + 10 + 8)-(4 + 3 + 2)= 12
我们玩玩家1,我们的目标是找到得分最高的最佳选择顺序。
我知道DP的概念,但是我不知道可以节省些什么以节省时间。 我使用minmax算法编写的代码的主要部分有效,但是我不知道如何将其与动态编程结合使用
我尝试使用一个矩阵,其中行作为左侧的存储桶,而列作为右侧的存储桶,因此当我们使用数组的同一“部分”时,我可以在其中保存答案,但是我有一些问题...
EDIT1:添加了我的代码
`public int maxGain(int[] values)
{
this.moves = new int[values.length+1][values.length+1];
return _maxGain(values,0,values.length-1,0,0,values.length,true,0,0);
}`
public int _maxGain(int[] values, int leftBowl, int rightBowl, int valuePlayer1, int valuePlayer2,int leftBowls, boolean ifFirstPlayer, int leftMoves, int rightMoves){
//Check if end of the game
if(leftBowls == 0) {
//Calculate the final score
return valuePlayer1 - valuePlayer2;
}
//System.out.println("Left:"+values[leftBowl]+", right: "+values[rightBowl]);
// If first player
if(ifFirstPlayer){
int maxEval = Integer.MIN_VALUE;
int eval;
for(int i=0;i<2;i++){
if(i==0){
//Do move
valuePlayer1 = valuePlayer1+values[leftBowl];
leftBowls--;
leftMoves++;
if(moves[leftMoves][rightMoves] != 0){
eval = moves[leftMoves][rightMoves];
System.out.println("USED! Left:"+leftMoves+",right: "+rightMoves+" Moves: " + moves[leftMoves][rightMoves] );
}else {
eval = _maxGain(values, leftBowl + 1, rightBowl, valuePlayer1, valuePlayer2, leftBowls, false, leftMoves, rightMoves);
moves[leftMoves][rightMoves] = eval;
System.out.println("Left:"+leftMoves+",right: "+rightMoves+" Moves: " + moves[leftMoves][rightMoves] );
for(int x=0;x<this.moves.length;x++){
for(int j=0;j<this.moves.length;j++){
System.out.print(this.moves[x][j]+" ");
}
System.out.println();
}
}
leftMoves--;
maxEval = Math.max(maxEval,eval);
//Undo move
valuePlayer1 = valuePlayer1-values[leftBowl];
leftBowls++;
}else{
//Do move
valuePlayer1 = valuePlayer1+values[rightBowl];
leftBowls--;
rightMoves++;
if(moves[leftMoves][rightMoves] != 0){
eval = moves[leftMoves][rightMoves];
System.out.println("USED! Left:"+leftMoves+",right: "+rightMoves+" Moves: " + moves[leftMoves][rightMoves] );
}else {
eval = _maxGain(values, leftBowl, rightBowl - 1, valuePlayer1, valuePlayer2, leftBowls, false, leftMoves, rightMoves);
moves[leftMoves][rightMoves] = eval;
System.out.println("Left:"+leftMoves+",right: "+rightMoves+" Moves: " + moves[leftMoves][rightMoves] );
for(int x=0;x<this.moves.length;x++){
for(int j=0;j<this.moves.length;j++){
System.out.print(this.moves[x][j]+" ");
}
System.out.println();
}
}
rightMoves--;
maxEval = Math.max(maxEval,eval);
//Undo move
valuePlayer1 = valuePlayer1-values[rightBowl];
leftBowls++;
}
}
return maxEval;
//If second player
}else{
int minEval = Integer.MAX_VALUE;
int eval;
for(int i=0;i<2;i++){
if(i==0){
//Do move
valuePlayer2 = valuePlayer2+values[leftBowl];
leftBowls--;
leftMoves++;
if(moves[leftMoves][rightMoves] != 0){
eval = moves[leftMoves][rightMoves];
System.out.println("USED! Left:"+leftMoves+",right: "+rightMoves+" Moves: " + moves[leftMoves][rightMoves] );
}else {
eval = _maxGain(values, leftBowl + 1, rightBowl, valuePlayer1, valuePlayer2, leftBowls, true, leftMoves, rightMoves);
moves[leftMoves][rightMoves] = eval;
System.out.println("Left:"+leftMoves+",right: "+rightMoves+" Moves: " + moves[leftMoves][rightMoves] );
for(int x=0;x<this.moves.length;x++){
for(int j=0;j<this.moves.length;j++){
System.out.print(this.moves[x][j]+" ");
}
System.out.println();
}
}
leftMoves--;
minEval = Math.min(minEval,eval);
//Undo move
valuePlayer2 = valuePlayer2-values[leftBowl];
leftBowls++;
}else{
//Do move
valuePlayer2 = valuePlayer2+values[rightBowl];
leftBowls--;
rightMoves++;
if(moves[leftMoves][rightMoves] != 0){
eval = moves[leftMoves][rightMoves];
System.out.println("USED! Left:"+leftMoves+",right: "+rightMoves+" Moves: " + moves[leftMoves][rightMoves] );
}else {
eval = _maxGain(values, leftBowl, rightBowl - 1, valuePlayer1, valuePlayer2, leftBowls, true, leftMoves, rightMoves);
moves[leftMoves][rightMoves] = eval;
System.out.println("Left:"+leftMoves+",right: "+rightMoves+" Moves: " + moves[leftMoves][rightMoves] );
for(int x=0;x<this.moves.length;x++){
for(int j=0;j<this.moves.length;j++){
System.out.print(this.moves[x][j]+" ");
}
System.out.println();
}
}
rightMoves--;
minEval = Math.min(minEval,eval);
//Undo move
valuePlayer2 = valuePlayer2-values[rightBowl];
leftBowls++;
}
}
return minEval;
}
}
答案 0 :(得分:0)
好吧,所以我在Github上找到了正是这种任务的代码, 如果以后需要,我将其留给其他人使用: