java中的双角硬币游戏

java中的双角硬币游戏,java,backtracking,recursive-backtracking,Java,Backtracking,Recursive Backtracking,2角硬币游戏接收阵列。游戏的目标是最大限度地累积点数(数组中元素的值)。只能从阵列的两个角点获取点 游戏有两个条件: amir took 16 tamara took 23 amira took 30 tamara took 15 amir took 19 tamara took 21 amir took 14 tamara took 13 Final Score: amir total 79 tamara total 72 Amir took 16 Tamara took 23 Amir t

2角硬币游戏接收阵列。游戏的目标是最大限度地累积点数(数组中元素的值)。只能从阵列的两个角点获取点

游戏有两个条件:

amir took 16
tamara took 23
amira took 30
tamara took 15
amir took 19
tamara took 21
amir took 14
tamara took 13
Final Score:
amir total 79
tamara total 72
Amir took 16
Tamara took 23
Amir took 30
Tamara took 15
Amir took 19
Tamara took 21
Amir took 13
Tamara took 14
Final Score:
Amir total 78
Tamara total 73
int[] array1 = {16,23,30,14,13,21,19,15};
TEST.coingame(array1);
System.out.println();
1) 第一名球员(阿米尔)永远不会输(他会赢或以平局结束),他也不一定会选择最大的优势

2) 第二名球员(塔马拉)总是在最高的角落得分

我的输出:

amir took 16
tamara took 23
amira took 30
tamara took 15
amir took 19
tamara took 21
amir took 14
tamara took 13
Final Score:
amir total 79
tamara total 72
Amir took 16
Tamara took 23
Amir took 30
Tamara took 15
Amir took 19
Tamara took 21
Amir took 13
Tamara took 14
Final Score:
Amir total 78
Tamara total 73
int[] array1 = {16,23,30,14,13,21,19,15};
TEST.coingame(array1);
System.out.println();
预期输出:

amir took 16
tamara took 23
amira took 30
tamara took 15
amir took 19
tamara took 21
amir took 14
tamara took 13
Final Score:
amir total 79
tamara total 72
Amir took 16
Tamara took 23
Amir took 30
Tamara took 15
Amir took 19
Tamara took 21
Amir took 13
Tamara took 14
Final Score:
Amir total 78
Tamara total 73
int[] array1 = {16,23,30,14,13,21,19,15};
TEST.coingame(array1);
System.out.println();
问题:

amir took 16
tamara took 23
amira took 30
tamara took 15
amir took 19
tamara took 21
amir took 14
tamara took 13
Final Score:
amir total 79
tamara total 72
Amir took 16
Tamara took 23
Amir took 30
Tamara took 15
Amir took 19
Tamara took 21
Amir took 13
Tamara took 14
Final Score:
Amir total 78
Tamara total 73
int[] array1 = {16,23,30,14,13,21,19,15};
TEST.coingame(array1);
System.out.println();
-阿米尔将选择16,所以塔玛拉将必须选择23,所以在下一轮他将选择30(因为塔玛拉将始终选择最大的角落)

-阿米尔在最后一回合将选择13而不是14,因为他已经赢了,所以他不在乎点数/硬币的价值

amir选择13而不是14的原因: 1.因为塔玛拉总是选择最大的“结束”2。因为他没有输掉这场比赛(65对59),所以他会选择13而不是14这是策略-只是不输球(可以打成平局或赢得比赛),他可以计划他的所有动作,因为他可以从乞讨中看到阵型,他不想用更少的动作输掉这场比赛

-阿米尔从第一个回合就知道下一步是什么,因为他不能在这场比赛中输球(他可以以赢家的身份结束比赛,或者以与塔马拉相同的分数结束比赛)

-阿米尔可以提前计算游戏的完整动作树,以及塔玛对每一个动作的反应,然后计算他对每一个动作的反应。这种解决方案的问题可能是一棵大树(阿米尔和塔马拉可以玩的不同游戏数量是2^K——如果K更大,那么强大的计算机将需要数万亿年)。 因此,在这个游戏中需要这样一个有效的解决方案。并要求阿米尔采取一些行动为自己制定战略

数组:

amir took 16
tamara took 23
amira took 30
tamara took 15
amir took 19
tamara took 21
amir took 14
tamara took 13
Final Score:
amir total 79
tamara total 72
Amir took 16
Tamara took 23
Amir took 30
Tamara took 15
Amir took 19
Tamara took 21
Amir took 13
Tamara took 14
Final Score:
Amir total 78
Tamara total 73
int[] array1 = {16,23,30,14,13,21,19,15};
TEST.coingame(array1);
System.out.println();
我的代码:

public static void coingame(int[] arr)
{ 
   int n = arr.length;  
   int i = 0, j = n-1,p1=0,p2=0,totalP2=0,totalP1=0; 

   while(j > i){

      if(arr[j]+arr[j-1]>arr[i]+arr[i+1]){
      p1 = arr[j];--j;
         if(arr[j]>arr[i]){
            p2=arr[j];--j;
         }else{  
            p2=arr[i];++i;
         } 
      }else{ 
         p1 = arr[i];++i;
         if(arr[j]>arr[i]){
            p2=arr[j];--j;
         }else{  
            p2=arr[i];++i;
         } 
      }

      System.out.println ("amir took "+p1);totalP1+=p1;
      System.out.println ("tamara took "+p2);totalP2+=p2;
   }

   System.out.println ("Final Score:");
   System.out.println ("amir total "+totalP1);
   System.out.println ("tamara total "+totalP2);
} 
编辑:(Akshay Batra的答案)

public static int[]pickByAmir(int[]coins,int amirTook,int tamaratake,int start,int end){
如果(开始>结束){
int[]res=新的int[2];
res[0]=amirTook;
res[1]=塔玛拉塔克;
返回res;
}
int[]a=新的int[2];
a[0]=阿米尔托克;
a[1]=塔玛拉塔克;
如果(硬币长度==0)
返回a;
阿米尔托克=硬币[开始];
硬币=pickByTamara(硬币+开始,结束);
塔玛拉塔克=硬币[开始];
a=匹克比阿米尔(硬币,阿米尔托克+a[0],塔玛拉塔克+a[1],+开始,结束);
int[]b=新的int[2];
b[0]=amirTook;
b[1]=塔玛拉塔克;

如果(a[0]此代码适用于您的输入,请尝试使用不同的输入,并查看它是否中断,如果中断,请优化您的解决方案

以这种方式从调用方方法调用
int[]a=pickByAmir(array1,0,0,array1.length-1);

a[0]
将有阿米尔的总数,
a[1]
将有塔玛拉的总数

int[] pickByAmir(int[] coins, int amirTook, int tamaraTook, int start, int end) {
    if(start>end) {
        int[] res = new int[2];
        res[0] = amirTook;
        res[1] = tamaraTook;
        return res;
    }
    int[] a = new int[2];
    a[0] = amirTook;
    a[1] = tamaraTook;
    if(coins.length==0)
        return a;
    amirTook = coins[start];
    coins = pickByTamara(coins, ++start , end);
    tamaraTook = coins[start];
    a = pickByAmir(coins, amirTook+a[0], tamaraTook+a[1], ++start, end);
    int[] b = new int[2];
    b[0] = amirTook;
    b[1] = tamaraTook;
    if(a[0]<a[1]){
        amirTook = coins[end];
        coins = pickByTamara(coins, start, --end);
        b = pickByAmir(coins, amirTook+b[0], tamaraTook+b[1], ++start, end);
        if(a[0]<b[0])
            return b;
    }
    return a;
}
int[] pickByTamara(int[] coins, int start, int end){
    return coins[start] > coins[end] ? coins : swapArray(coins, start, end);
}

int[] swapArray(int[] coins, int start, int end) {
    int temp = coins[start];
    coins[start] = coins[end];
    coins[end] = temp;
    return coins;
}
int[]pickByAmir(int[]coins,int amirTook,int tamaratake,int start,int end){
如果(开始>结束){
int[]res=新的int[2];
res[0]=amirTook;
res[1]=塔玛拉塔克;
返回res;
}
int[]a=新的int[2];
a[0]=阿米尔托克;
a[1]=塔玛拉塔克;
如果(硬币长度==0)
返回a;
阿米尔托克=硬币[开始];
硬币=pickByTamara(硬币+开始,结束);
塔玛拉塔克=硬币[开始];
a=匹克比阿米尔(硬币,阿米尔托克+a[0],塔玛拉塔克+a[1],+开始,结束);
int[]b=新的int[2];
b[0]=amirTook;
b[1]=塔玛拉塔克;

if(a[0]是什么让Amir选择13而不是14?不是都可以吗?至少你可以做的是正确地缩进和格式化代码。我不明白选择数字的标准是什么。如果23或30可用,为什么选择16?@AnnaLA我理解这个问题,但我认为这是一个实践问题,我可以给你一个代码,它可以工作,但你需要理解它背后的想法。如果你仍然想要代码,那么我可以发布一个answer@DawoodibnKareem考虑到这一点,我认为OP意味着整个阵列都是可见的,由选择者选择最有利于长期胜利的阵列。这可能导致在每一步都没有选择最大值。此外,我认为s被corner=end弄糊涂了。像线段一样,一维数组有端点,而不是角。谢谢你的回答……我试了你的答案,但总分很好……但顺序不好“结果:阿米尔拿了19分塔玛拉拿了14分阿米尔拿了13分塔玛拉拿了21分阿米尔拿了30分塔玛拉拿了15分阿米尔拿了16分塔玛拉拿了23分最终得分:阿米尔总数:78分塔玛尔总数:73”我刚刚在我的问题(最后一部分)中添加了你的代码和文本…如果你能看到这有什么错…那么使用偶数和奇数策略呢?@AnnaLa我认为你可以反转数组,而不是交换第一个和最后一个元素。最好使用列表,会更容易一些。你能编辑你的答案吗?使用偶数和奇数策略,这样顺序将是“埃米尔拿下16塔玛拉拿下23塔玛拉拿下30塔玛拉拿下15埃米尔拿下19塔玛拉拿下21埃米尔拿下13塔玛拉拿下14最终得分:埃米尔总共78塔玛拉总共73“TIA