gpt4 book ai didi

java - 如何优化将访问状态存储在 HashSet 中的代码?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:29:55 25 4
gpt4 key购买 nike

我正在为一些 USACO 培训问题编写解决方案。不幸的是,即使我使用了我认为正确的算法(正确的答案,太慢而无法接受解决方案),我的实现也太慢了。这是代码:

public class clocks {

private static LinkedList<State> queue = new LinkedList<State>();

private static HashSet<State> set = new HashSet<State>();

private static PrintWriter out;

static long cTime = 0;

private static class State implements Cloneable {
public int[] clocks;
public List<Byte> road;

public State(int[] clocks, List<Byte> road) {
this.clocks = clocks;
this.road = road;
}

private void makeMove(byte move) {
int[] currentMoves = moves[move];
for (int i = 0; i < currentMoves.length; i++) {
clocks[currentMoves[i]] = (clocks[currentMoves[i]] + 1) % 4;
}
road.add(move);
}

@Override
public State clone() {
int[] clocks = new int[this.clocks.length];
System.arraycopy(this.clocks, 0, clocks, 0, this.clocks.length);
List<Byte> road = new ArrayList<Byte>(this.road);
State newState = new State(clocks, road);
return newState;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(clocks);
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
State other = (State) obj;
if (!Arrays.equals(clocks, other.clocks))
return false;
return true;
}
}

private static final int[][] moves = new int[][] {
{0, 1, 3, 4},
{0, 1, 2},
{1, 2, 4, 5},
{0, 3, 6},
{1, 3, 4, 5, 7},
{2, 5, 8},
{3, 4, 6, 7},
{6, 7, 8},
{4, 5, 7, 8}
};

private static boolean isValid(int[] clocks) {
for (int i = 0; i < clocks.length; i++) {
if (clocks[i] != 0) {
return false;
}
}
return true;
}

private static void bfs() {
while (!queue.isEmpty()) {
State currentState = queue.pop();
if (isValid(currentState.clocks)) {
for (int i = 0; i <= currentState.road.size() - 1; i++) {
System.out.print((currentState.road.get(i) + 1));
if (i != currentState.road.size() - 1) {
System.out.print(" ");
}
}
System.out.println("");
break;
}
for (byte i = 0; i < moves.length; i++) {
State newState = currentState.clone();
newState.makeMove(i);
long start = System.currentTimeMillis();
boolean added = set.add(newState);
cTime += System.currentTimeMillis() - start;
if (added) {
queue.add(newState);
}
}
}
}

public static void main(String[] args) throws Exception {
long start = System.currentTimeMillis();
BufferedReader f = new BufferedReader(new FileReader("clocks.in"));
out = new PrintWriter(new BufferedWriter(new FileWriter("clocks.out")));
int clocks[] = new int[9];
for (int i = 0; i < 3; i++) {
StringTokenizer tokenizer = new StringTokenizer(f.readLine());
int k = i*3;
for (int j = 0; j < 3; j++) {
clocks[k + j] = (Integer.valueOf(tokenizer.nextToken()) / 3 ) % 4;
}
}
State state = new State(clocks, new ArrayList<Byte>());
queue.add(state);
if (!isValid(clocks)) {
bfs();
}
System.out.println(System.currentTimeMillis() - start);
System.out.println(cTime);
f.close();
out.close();
}
}

我注意到最耗时的是向集合中添加新元素(90/200 毫秒)和创建新状态(70/200 毫秒)。我想知道是否有可能以更有效的方式实现此解决方案(例如,没有 State 类)。

问题陈述:

Consider nine clocks arranged in a 3x3 array thusly. The goal is to find a minimal sequence of moves to return all the dials to 12 o'clock. Nine different ways to turn the dials on the clocks are supplied via a table below; each way is called a move. Select for each move a number 1 through 9 which will cause the dials of the affected clocks (see next table) to be turned 90 degrees clockwise.

最佳答案

如果我没记错这个问题,那么我遇到了和你一样的问题,因为你写的解决方案实际上并不正确。 :)

这里的关键见解是要认识到按下按钮四次与从未按下按钮是一样的,您的代码不会像现在这样实现。由于有九个按钮,每个按钮有四种状态,因此需要检查 4^9 次操作,这完全在合理范围内。希望无需深入研究太多细节,您可以将其合并到您的 BFS 中或仅使用蛮力算法。

关于java - 如何优化将访问状态存储在 HashSet 中的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18643160/

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