迷宫java代码_java实现迷宫问题

迷宫问题

对于走迷宫,人们提出过很多计算机上的解法。深度优先搜索、广度优先搜索是使用最广的方法。生活中,人们更

愿意使用“紧贴墙壁,靠右行走”的简单规则。

58

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

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

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

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

题号的“解答.txt”中即可。

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 T04 {

static char[][] maze = {

{'#','#','#','#','B','#','#','#','#','#','#','#'},

{'#','#','#','#','.','.','.','.','#','#','#','#'},

{'#','#','#','#','.','#','#','#','#','.','.','#'},

{'#','.','.','.','.','#','#','#','#','#','.','#'},

{'#','.','#','#','#','#','#','.','#','#','.','#'},

{'#','.','#','#','#','#','#','.','#','#','.','#'},

{'#','.','#','#','.','.','.','.','.','.','.','#'},

{'#','.','#','#','.','#','#','#','.','#','.','#'},

{'#','.','.','.','.','#','#','#','.','#','.','#'},

{'#','#','.','#','.','#','#','#','.','#','.','A'},

{'#','#','.','#','#','#','.','.','.','#','#','#'},

{'#','#','#','#','#','#','#','#','#','#','#','#'}

};

// 显示迷宫

public static void show(){

for(int i=0;i

for(int j=0;j

System.out.print(" "+maze[i][j]);

}

System.out.println();

}

}

// 染色

public static Cell colorCell(Set from,Set desc){

Iterator iter = from.iterator();

59

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 set = new HashSet();

set.add(new Cell(9,11,null));

for(;;){

Set set1 = new HashSet();

// 出口 a.from.from.from.....

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();

60

show();

}

}

运行结果:

# # # # B # # # # # # #

# # # # . . . . # # # #

# # # # . # # # # . . #

# . . . . # # # # # . #

# . # # # # # . # # . #

# . # # # # # . # # . #

# . # # . . . . . . . #

# . # # . # # # . # . #

# . . . . # # # . # . #

# # . # . # # # . # . A

# # . # # # . . . # # #

# # # # # # # # # # # #

找到解!

# # # # B # # # # # # #

# # # # + . . . # # # #

# # # # + # # # # ? ? #

# + + + + # # # # # ? #

# + # # # # # ? # # ? #

# + # # # # # ? # # ? #

# + # # + + + + + + + #

# + # # + # # # ? # + #

# + + + + # # # ? # + #

# # ? # ? # # # ? # + +

# # ? # # # ? ? ? # # #

# # # # # # # # # # # #

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值