迷宮走法

迷宮走法

迷宮問題

對於走迷宮,人們提出過很多計算機上的解法。深度優先搜索、廣度優先搜索是使用最廣的方法。生活中,人們更願意使用“緊貼牆壁,靠右行走”的簡單規則。

下面的代碼則採用了另一種不同的解法。它把走迷宮的過程比做“染色過程”。

假設入口點被染爲紅色,它的顏色會“傳染”給與它相鄰的可走的單元。

這個過程不斷進行下去,如果最終出口點被染色,則迷宮有解。

仔細分析代碼中的邏輯,填充缺少的部分。把填空的答案(僅填空處的答案,不包括題面)存入考生文件夾下對應題號的“解答.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();
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章