迷宫走法

来源:互联网 发布:linux 限制某ip访问 编辑:程序博客网 时间:2024/05/01 15:23

迷宫走法

迷宫问题

对于走迷宫,人们提出过很多计算机上的解法。深度优先搜索、广度优先搜索是使用最广的方法。生活中,人们更愿意使用“紧贴墙壁,靠右行走”的简单规则。

下面的代码则采用了另一种不同的解法。它把走迷宫的过程比做“染色过程”。

假设入口点被染为红色,它的颜色会“传染”给与它相邻的可走的单元。

这个过程不断进行下去,如果最终出口点被染色,则迷宫有解。

仔细分析代码中的逻辑,填充缺少的部分。把填空的答案(仅填空处的答案,不包括题面)存入考生文件夹下对应题号的“解答.txt”中即可。

package marslin;import java.util.HashSet;import java.util.Iterator;import java.util.Set;class cell {// 单元格int row; // 哪行int col; // 哪列cell from; // 开始点public cell(int row, int col, cell from) {this.row = row;this.col = col;this.from = from;}}public class 迷宫走法29 {static char[][] maze = {{ '#', '#', '#', '#', 'B', '#', '#', '#', '#', '#', '#', '#' },{ '#', '#', '#', '#', '.', '.', '.', '.', '#', '#', '#', '#' },{ '#', '#', '#', '#', '.', '#', '#', '#', '#', '.', '.', '#' },{ '#', '.', '.', '.', '.', '#', '#', '#', '#', '#', '.', '#' },{ '#', '.', '#', '#', '#', '#', '#', '.', '#', '#', '.', '#' },{ '#', '.', '#', '#', '#', '#', '#', '.', '#', '#', '.', '#' },{ '#', '.', '#', '#', '.', '.', '.', '.', '.', '.', '.', '#' },{ '#', '.', '#', '#', '.', '#', '#', '#', '.', '#', '.', '#' },{ '#', '.', '.', '.', '.', '#', '#', '#', '.', '#', '.', '#' },{ '#', '#', '.', '#', '.', '#', '#', '#', '.', '#', '.', 'A' },{ '#', '#', '.', '#', '#', '#', '.', '.', '.', '#', '#', '#' },{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' } };// 显示迷宫public static void show() {for (int i = 0; i < maze.length; i++) {for (int j = 0; j < maze[i].length; j++) {System.out.print(" " + maze[i][j]);}System.out.println();}}// 染色public static cell colorCell(Set<cell> from, Set<cell> desc) {Iterator<cell> iter = from.iterator();while (iter.hasNext()) {cell a = iter.next();cell c[] = new cell[4];c[0] = new cell(a.row - 1, a.col, a); // 向上走c[1] = new cell(a.row + 1, a.col, a); // 向下走c[2] = new cell(a.row, a.col - 1, a); // 向左走c[3] = new cell(a.row, a.col + 1, a); // 向右走for (int i = 0; i < 4; i++) {if (c[i].row < 0 || c[i].row >= maze.length)continue;if (c[i].col < 0 || c[i].col >= maze[0].length)continue;char x = maze[c[i].row][c[i].col]; // 取得单元格对应字符if (x == 'B')return a;if (x == '.') {maze[c[i].row][c[i].col] = '?'; // 染色desc.add(c[i]);}}}return null;}//public static void resolve() {Set<cell> set = new HashSet<cell>();set.add(new cell(9, 11, null));for (;;) {Set<cell> set1 = new HashSet<cell>();// 出口 a.from.from.from.....<-(set.get(0).from)==null<-入口cell a = colorCell(set, set1);if (a != null) { // 找到解System.out.println("找到解!");while (a != null) { // 当前a里包含a.from 一直往前推maze[a.row][a.col] = '*'; // 染色路径a = a.from;}break;}if (set1.isEmpty()) { // 遍历所以一直到没有路走,这时 set1为空System.out.println("无解!");break;}set = set1; // 向里边}}public static void main(String[] args) {show();resolve();show();}}


0 0