4
我用蠻力遞歸編寫了一個Sudoku拼圖求解器。現在,我想知道需要多長時間才能解決10個相似類型的謎題。所以,我製作了一個名爲easy的文件夾,並在文件夾中放置了10個「簡單」拼圖。第一次運行求解器時,可能需要171毫秒,第二次需要37毫秒,第三次運行需要16毫秒。爲什麼不同時間再次解決完全相同的問題?時間不一致嗎?數獨時間不規律
第二個問題是隻顯示最後一個難題,即使我告訴它在加載拼圖之後重新繪製屏幕並在解決之後再次重新繪製屏幕。如果我只加載一個謎題而不解決它,它將顯示最初的謎題狀態。如果我然後調用Solve方法,最終的解決方案將在屏幕上繪製。這是我解決多個謎題的方法。
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);
}
你是什麼意思的「沖洗」的數據? – user1429254
*你是什麼意思的「重繪」? (我問第一個:P) – alfasin
我找到了一些更多的搜索後的答案。看起來在Java中,當你得到一個重繪方法時,儘快調用它重繪。如果重新連續發生,它只是等待最後一次電話。爲了解決這個問題,如果我將repaint()更改爲paintImmediately(0,0,maxX,maxY);它可以在屏幕上繪製所有內容。謝謝你的幫助。 – user1429254