就输出而言,我的程序运行良好,但是对于我的一些测试用例来说,找到答案的时间太长(有时需要18秒)。我想知道如何改善代码的性能。

我的代码做什么:
这是对Pebble Solitaire的看法。用户输入n个游戏,然后输入长度为23的字符串,其中仅包含“ o”(卵石)和“-”(空白空间)的组合。如果有两个相邻的小卵石,并且在两侧都有一个空白空间,即(oo-或-oo),则将中间的小卵石移开,并将其他两块卵互换,例如“ oo-”将变成“-哦

我当前的方法几乎是一种详尽的方法,它尝试了所有可能的移动,并以最小的卵石数量产生了移动集。

我想知道如何在不使其成为多线程的情况下改进该解决方案。

这是我所拥有的:

package Pebble;

import java.util.Scanner;

public class PebbleSolitaire {

    public static void main(String[] args){

        Scanner input = new Scanner(System.in);
        int numOfGames = Integer.parseInt(input.nextLine());

        while (numOfGames > 0){

            char[] values = input.nextLine().toCharArray();
            long startTime = System.nanoTime();
            System.out.println(solve(values));
            System.out.println("Time to finish in ms: " + (System.nanoTime() - startTime) / 1000000);
            numOfGames--;
        }
        input.close();
    }

    private static int solve(char[] game){

        if(game != null && game.length == 0){
            return -1;
        }

        int result = 0;

        for (int i = 0; i < game.length; i++){
            if(game[i] == 'o'){
                result++;
            }
        }

        //print(game);

        for (int i = 0; i < game.length; i++ ){

            char[] temp = new char[game.length];
            copyArray(temp, game);

            if (i-2 >= 0 && temp[i] == '-' && temp[i-2] == 'o' && temp[i-1] == 'o'){//move pebble forwards
                temp[i-1] = temp[i-2] = '-';
                temp[i] = 'o';
                result = Math.min(result, solve(temp));
            }

            copyArray(temp, game);

            if(i+2 < temp.length && temp[i] == '-' && temp[i+1] == 'o' && temp[i+2] == 'o'){//move pebble backwards
                temp[i+1] = temp[i+2] = '-';
                temp[i] = 'o';
                result = Math.min(result, solve(temp));
            }
        }
        return result;
    }

    private static void copyArray(char[] copy, char[] og){
        for(int x = 0; x < copy.length; x++){
            copy[x] = og[x];
        }
    }
    private static void print(char[] c){
        for(char ch: c){
            System.out.print(ch);
        }
        System.out.println();
    }
}


我的样本输入和输出:

2
-o----ooo----o----ooo--
6
Time to finish in ms: 0
oooooooooo-ooooooooooo-
4
Time to finish in ms: 18149


编辑:进行此完全迭代会大大提高性能吗?

最佳答案

也许您可以改善这一方面:

  for (int i = 0; i < game.length; i++ ){

        char[] temp = new char[game.length];
        copyArray(temp, game);

        if (i-2 >= 0 && temp[i] == '-' && temp[i-2] == 'o' && temp[i-1] == 'o'){//move pebble forwards
            temp[i-1] = temp[i-2] = '-';
            temp[i] = 'o';
            result = Math.min(result, solve(temp));
        }

        copyArray(temp, game);

        if(i+2 < temp.length && temp[i] == '-' && temp[i+1] == 'o' && temp[i+2] == 'o'){//move pebble backwards
            temp[i+1] = temp[i+2] = '-';
            temp[i] = 'o';
            result = Math.min(result, solve(temp));
        }
    }


至:

    for (int i = 0; i < game.length; i++ ){
        char[] temp = null;

        if (i-2 >= 0 && game[i] == '-' && game[i-2] == 'o' && game[i-1] == 'o'){//move pebble forwards
            temp = new char[game.length];
            copyArray(temp, game);
            temp[i-1] = temp[i-2] = '-';
            temp[i] = 'o';
            result = Math.min(result, solve(temp));
        }



        if(i+2 < game.length && game[i] == '-' && game[i+1] == 'o' && game[i+2] == 'o'){//move pebble backwards

            if(temp == null) temp = new char[game.length];

            copyArray(temp, game);
            temp[i+1] = temp[i+2] = '-';
            temp[i] = 'o';
            result = Math.min(result, solve(temp));
        }
    }


基本上,仅创建和“ copyArray(temp,game);”严格必要时。

10-06 13:30