我使用暴力递归编写了数独谜题求解器。现在,我想看看解决10个类似类型的谜题需要多长时间。因此,我创建了一个名为“easy”的文件夹,并将10个“easy”谜题放入该文件夹中。当我第一次运行求解器时,可能需要171毫秒,第二次需要37毫秒,第三次运行需要16毫秒。为什么同样的问题解决时间不同?应该是一致的吧?
第二个问题是,它只显示最后一个解决的谜题,尽管我告诉它在加载谜题后重绘屏幕,再在解决后重绘屏幕。如果我只加载一个未解决的谜题,它会显示初始谜题状态。如果我然后调用解决方法,则会在屏幕上绘制最终解决方案。这是我的解决多个谜题的方法。
第二个问题是,它只显示最后一个解决的谜题,尽管我告诉它在加载谜题后重绘屏幕,再在解决后重绘屏幕。如果我只加载一个未解决的谜题,它会显示初始谜题状态。如果我然后调用解决方法,则会在屏幕上绘制最终解决方案。这是我的解决多个谜题的方法。
void LoadFolderAndSolve() throws FileNotFoundException {
String folderName = JOptionPane.showInputDialog("Enter folder name");
long startTime = System.currentTimeMillis();
for (int i = 1; i < 11; i++) {
String fileName = folderName + "/puzzle" + i + ".txt";
ReadPuzzle(filename); // this has a call to repaint to show the initial puzzle
SolvePuzzle(); // this has a call to repaint to show the solution
// If I put something to delay here, puzzle 1-9 is still not shown only 10.
}
long finishTime = System.currentTimeMillis();
long difference = finishTime - startTime;
System.out.println("Time in ms - " + difference);
}