gpt4 book ai didi

java - 如何在不使其成为多线程的情况下使其更快?

转载 作者:行者123 更新时间:2023-11-30 07:00:45 26 4
gpt4 key购买 nike

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

我的代码的作用:这是对 Pebble Solitaire 的看法。用户输入n个游戏,然后输入长度为23的字符串,其中仅包含“o”(卵石)和“-”(空白)的组合。如果有 2 个相邻的鹅卵石,并且两侧都有空位,即(oo- 或 -oo),那么您移除中间的鹅卵石并将另外两 block 彼此交换,ex​​ '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);”当绝对必要时。

关于java - 如何在不使其成为多线程的情况下使其更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40963852/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com