我的遞回求解器陷入死胡同,無法轉身,導致堆疊溢位錯誤。我哪里錯了?
solI是一個公共陣列。x2并且y2是公共解決方案坐標。
0-wall
1-open path
2-tried
3-solution
遞回求解器
public static boolean traverse(int x1, int y1) {
boolean done = false;
count ;
if ((count < 100) && (x1 >= 0) && (x1 < solI.length) && (y1 >= 0) && (y1 < solI.length) && (solI[x1][y1] == 1)) {
solI[x1][x1] = 2;
if ((x1 == x2) && (y1 == y2)) {
done = true;
} else {
if (!done) { //up
done = traverse(x1 - 1, y1);
}
if (!done) {//left{
done = traverse(x1, y1 - 1);
}
if (!done) {//down{
done = traverse(x1 1, y1);
}
if (!done) {//right
done = traverse(x1, y1 1);
}
}
if (done) solI[x1][y1] = 3;
}
return done;
}
uj5u.com熱心網友回復:
問題出在代碼的第 5 行。這solI[x1][x1] = 2;應該改為 this solI[x1][y1] = 2;。也忘記了count。會在max count = 100維度超過 的矩陣中引起問題10 * 10。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/445490.html
