**15.36 (模拟:自回避随机漫步)
编写一个模拟程序,显示出现尽头点路径的可能性随着格子数量的增加而变大。程序模拟大小从10到80的网格。对每种大小的网格,模拟自回避随机漫步10000 次,然后显示出现尽头点路径的概率,如下面的示例输出所示
代码示例 :编程练习题15_36SelfAvoidanceRandomWalk.java
package chapter_15;
import java.util.ArrayList;
public class 编程练习题15_36SelfAvoidanceRandomWalk {
private static final int MAX_TRIES = 10000;
public static void main(String[] args) {
for (int gridSize = 10; gridSize <= 80; gridSize++) {
int endPointCount = 0;
for (int j = 0; j < MAX_TRIES; j++) {
endPointCount += drawLine(gridSize);
}
double probability = (double)endPointCount / MAX_TRIES;
System.out.printf("For a lattice of size %d, the probability of dead-end paths is %.2f%%\n",gridSize, probability*100);
}
}
public static int drawLine(int gridSize) {
boolean[][] point = new boolean[gridSize][gridSize];
int startX = gridSize / 2;
int startY = gridSize / 2;
int currentX = startX;
int currentY = startY;
point[startX][startY] = true; // 标记起始点为已访问
while (true) {
// 检查当前位置四周是否可行
boolean up = currentY > 0 && !point[currentX][currentY - 1]; // 向上移动是否可行
boolean down = currentY < gridSize - 1 && !point[currentX][currentY + 1]; // 向下移动是否可行
boolean left = currentX > 0 && !point[currentX - 1][currentY]; // 向左移动是否可行
boolean right = currentX < gridSize - 1 && !point[currentX + 1][currentY]; // 向右移动是否可行
// 如果四周都不可行,则为死路
if (!up && !down && !left && !right) {
return 1; // Dead-end path
}
// 随机选择一个可行的方向
int randomDirection;
do {
randomDirection = (int) (Math.random() * 4);
} while ((randomDirection == 0 && !up) || (randomDirection == 1 && !down) || (randomDirection == 2 && !left) || (randomDirection == 3 && !right));
// 根据随机数更新当前位置
if (randomDirection == 0) { // 上
currentY--;
} else if (randomDirection == 1) { // 下
currentY++;
} else if (randomDirection == 2) { // 左
currentX--;
} else if (randomDirection == 3) { // 右
currentX++;
}
// 检查新位置是否超出边界
if (currentX <= 0 || currentY <= 0 || currentX >= gridSize || currentY >= gridSize) {
// 超出边界,但不视为死路
return 0;
}
// 标记新位置为已访问
point[currentX][currentY] = true;
}
}
}
输出结果
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
For a lattice of size 10, the probability of dead-end paths is 29.72%
For a lattice of size 11, the probability of dead-end paths is 28.93%
For a lattice of size 12, the probability of dead-end paths is 35.81%
For a lattice of size 13, the probability of dead-end paths is 36.24%
For a lattice of size 14, the probability of dead-end paths is 42.44%
For a lattice of size 15, the probability of dead-end paths is 42.47%
For a lattice of size 16, the probability of dead-end paths is 49.29%
For a lattice of size 17, the probability of dead-end paths is 48.93%
For a lattice of size 18, the probability of dead-end paths is 53.59%
For a lattice of size 19, the probability of dead-end paths is 54.03%
For a lattice of size 20, the probability of dead-end paths is 60.08%
For a lattice of size 21, the probability of dead-end paths is 58.90%
For a lattice of size 22, the probability of dead-end paths is 63.90%
For a lattice of size 23, the probability of dead-end paths is 64.21%
For a lattice of size 24, the probability of dead-end paths is 67.82%
For a lattice of size 25, the probability of dead-end paths is 67.88%
For a lattice of size 26, the probability of dead-end paths is 72.37%
For a lattice of size 27, the probability of dead-end paths is 71.37%
For a lattice of size 28, the probability of dead-end paths is 75.71%
For a lattice of size 29, the probability of dead-end paths is 75.39%
For a lattice of size 30, the probability of dead-end paths is 78.44%
For a lattice of size 31, the probability of dead-end paths is 78.39%
For a lattice of size 32, the probability of dead-end paths is 81.72%
For a lattice of size 33, the probability of dead-end paths is 80.60%
For a lattice of size 34, the probability of dead-end paths is 82.76%
For a lattice of size 35, the probability of dead-end paths is 82.78%
For a lattice of size 36, the probability of dead-end paths is 84.22%
For a lattice of size 37, the probability of dead-end paths is 85.05%
For a lattice of size 38, the probability of dead-end paths is 86.68%
For a lattice of size 39, the probability of dead-end paths is 86.10%
For a lattice of size 40, the probability of dead-end paths is 88.50%
For a lattice of size 41, the probability of dead-end paths is 88.11%
For a lattice of size 42, the probability of dead-end paths is 89.65%
For a lattice of size 43, the probability of dead-end paths is 89.55%
For a lattice of size 44, the probability of dead-end paths is 90.37%
For a lattice of size 45, the probability of dead-end paths is 90.50%
For a lattice of size 46, the probability of dead-end paths is 91.45%
For a lattice of size 47, the probability of dead-end paths is 91.99%
For a lattice of size 48, the probability of dead-end paths is 92.88%
For a lattice of size 49, the probability of dead-end paths is 93.05%
For a lattice of size 50, the probability of dead-end paths is 93.94%
For a lattice of size 51, the probability of dead-end paths is 93.98%
For a lattice of size 52, the probability of dead-end paths is 94.64%
For a lattice of size 53, the probability of dead-end paths is 94.79%
For a lattice of size 54, the probability of dead-end paths is 95.46%
For a lattice of size 55, the probability of dead-end paths is 95.27%
For a lattice of size 56, the probability of dead-end paths is 95.86%
For a lattice of size 57, the probability of dead-end paths is 96.02%
For a lattice of size 58, the probability of dead-end paths is 96.09%
For a lattice of size 59, the probability of dead-end paths is 96.53%
For a lattice of size 60, the probability of dead-end paths is 96.69%
For a lattice of size 61, the probability of dead-end paths is 96.96%
For a lattice of size 62, the probability of dead-end paths is 97.15%
For a lattice of size 63, the probability of dead-end paths is 97.03%
For a lattice of size 64, the probability of dead-end paths is 97.69%
For a lattice of size 65, the probability of dead-end paths is 97.46%
For a lattice of size 66, the probability of dead-end paths is 97.94%
For a lattice of size 67, the probability of dead-end paths is 98.14%
For a lattice of size 68, the probability of dead-end paths is 98.20%
For a lattice of size 69, the probability of dead-end paths is 98.08%
For a lattice of size 70, the probability of dead-end paths is 98.39%
For a lattice of size 71, the probability of dead-end paths is 98.36%
For a lattice of size 72, the probability of dead-end paths is 98.70%
For a lattice of size 73, the probability of dead-end paths is 98.48%
For a lattice of size 74, the probability of dead-end paths is 98.63%
For a lattice of size 75, the probability of dead-end paths is 98.77%
For a lattice of size 76, the probability of dead-end paths is 98.95%
For a lattice of size 77, the probability of dead-end paths is 98.92%
For a lattice of size 78, the probability of dead-end paths is 99.00%
For a lattice of size 79, the probability of dead-end paths is 99.02%
For a lattice of size 80, the probability of dead-end paths is 99.24%