java迷宫算法小老鼠_Java与算法之老鼠再闯迷宫(广度优先算法)

1号完成后，轮到2号行动，也只成功放出一只，即4号

3号放出5号

4号放出6号

5号放出7号

6号放出8号

7号放出9、10号

8号放出11号

9号放出12号

int width = 5; //迷宫宽度

int height = 4; //迷宫高度

int[][] maze = new int[width][height];

maze[2][0] = 1;

maze[1][2] = 1;

maze[2][2] = 1;

maze[4][1] = 1;

mark[0][0] = 1;

public Trace(int x, int y, int father, int step) {

this.x = x;

this.y = y;

this.father = father;

this.step = step;

}

private int x;

private int y;

private int father;

private int step;

public int getX() {

return x;

}

public void setX(int x) {

this.x = x;

}

public int getY() {

return y;

}

public void setY(int y) {

this.y = y;

}

public int getFather() {

return father;

}

public void setFather(int father) {

this.father = father;

}

public int getStep() {

return step;

}

public void setStep(int step) {

this.step = step;

}

@Override

public String toString() {

}

}

import org.apache.commons.lang3.builder.ToStringStyle;

import java.util.ArrayList;

import java.util.List;

/**

* 老鼠走迷宫 BFS算法

* Created by autfish on 2016/9/5.

*/

public class BfsRatMaze {

int min = Integer.MAX_VALUE;

int endX = 4; //目标点横坐标

int endY = 2; //目标点纵坐标

int width = 5; //迷宫宽度

int height = 4; //迷宫高度

int[][] maze;

int[][] mark;

static class Trace {

public Trace(int x, int y, int father, int step) {

this.x = x;

this.y = y;

this.father = father;

this.step = step;

}

private int x;

private int y;

private int father;

private int step;

public int getX() {

return x;

}

public void setX(int x) {

this.x = x;

}

public int getY() {

return y;

}

public void setY(int y) {

this.y = y;

}

public int getFather() {

return father;

}

public void setFather(int father) {

this.father = father;

}

public int getStep() {

return step;

}

public void setStep(int step) {

this.step = step;

}

@Override

public String toString() {

}

}

public void bfs() {

int[][] next = new int[][] { //按右->下->左->上的顺序尝试

{1, 0},

{0, 1},

{-1, 0},

{0, -1}

};

int head = 0, tail = 1;

int startX = 0, startY = 0;

int nextX, nextY;

Listtraces = new ArrayList<>();

mark[startX][startY] = 1;

int flag = 0;

for(int k = 0; k <= 3;="" k++)="">=>

if(nextX < 0="" ||="" nextx="">= width || nextY < 0="" ||="" nexty="">= height) { //超出边界

continue;

}

//没有障碍且没有探索过, 则把当前位置标记为未探索点

if(maze[nextX][nextY] == 0 && mark[nextX][nextY] == 0) {

this.mark[nextX][nextY] = 1;

tail++;

}

if(nextX == endX && nextY == endY) {

flag = 1;

break;

}

}

if(flag == 1)

break;

//一个点的四个方向探索完成, 取编号最小的一个未探索点

}

Trace end = traces.get(tail - 1);

int father = end.getFather();

System.out.println('共' + end.getStep() + '步');

StringBuilder path = new StringBuilder();

path.insert(0, '->[' + end.getX() + ',' + end.getY() + ']');

while(father >= 0) {

Trace prev = traces.get(father);

father = prev.getFather();

if(father > -1)

path.insert(0, '->[' + prev.getX() + ',' + prev.getY() + ']');

else

path.insert(0, '[' + prev.getX() + ',' + prev.getY() + ']');

}

System.out.println(path.toString());

}

public void initMaze() {

this.maze = new int[width][height];

this.mark = new int[width][height];

this.maze[2][0] = 1;

this.maze[1][2] = 1;

this.maze[2][2] = 1;

this.maze[4][1] = 1;

this.mark[0][0] = 1;

//打印迷宫 _表示可通行 *表示障碍 !表示目标

for(int y = 0; y < height;="" y++)="">

for(int x = 0; x < width;="" x++)="">

if(x == endX && y == endY) {

System.out.print('! ');

} else if(this.maze[x][y] == 1) {

System.out.print('* ');

} else {

System.out.print('_ ');

}

}

System.out.println();

}

System.out.println();

}

public static void main(String[] args) {

BfsRatMaze b = new BfsRatMaze();

b.initMaze();

b.bfs();

}

}

_ _ _ _ *

_ * * _ !

_ _ _ _ _

[0,0]->[1,0]->[1,1]->[2,1]->[3,1]->[3,2]->[4,2]

• 0
点赞
• 2
收藏
觉得还不错? 一键收藏
• 0
评论
08-16 587
02-29 577
05-22
08-07 396
09-26
05-08
04-16
10-20

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