目录:
1.定义常量 2.定义节点信息 3.定义链式队列 4.定义迷宫类 5.测试代码
1.定义常量
public interface Constant {
int RIGHT
= 0;
int DOWN
= 1;
int LEFT
= 2;
int UP
= 3;
}
2.定义节点类型
private static class MazeNoe{
int val
;
int x
;
int y
;
boolean[] state
;
public MazeNoe(int data
, int i
, int j
) {
this.state
= new boolean[4];
this.val
= data
;
this.x
= i
;
this.y
= j
;
}
}
3.定义链式队列
package maze
;
public class LinkQueue<T>{
private Entry
<T> front
;
private Entry
<T> rear
;
private int count
;
public LinkQueue(){
this.front
= this.rear
= new Entry<>(null
, null
);
}
public void offer(T val
){
Entry
<T> node
= new Entry<>(val
, null
);
this.rear
.next
= node
;
this.rear
= node
;
this.count
++;
}
public T
poll(){
T val
= null
;
if(this.front
.next
!= null
){
val
= this.front
.next
.data
;
this.front
.next
= this.front
.next
.next
;
if(this.front
.next
== null
){
this.rear
= this.front
;
}
this.count
--;
}
return val
;
}
public T
peek(){
T val
= null
;
if(this.front
.next
!= null
){
val
= this.front
.next
.data
;
}
return val
;
}
public boolean isEmpty(){
return this.front
== this.rear
;
}
public int size(){
return this.count
;
}
static class Entry<T>{
T data
;
Entry
<T> next
;
public Entry(T data
, Entry
<T> next
) {
this.data
= data
;
this.next
= next
;
}
}
}
4.定义迷宫类
public class Maze {
private MazeNoe
[][] maze
;
private LinkQueue
<MazeNoe> queue
;
private int row
;
private int col
;
private MazeNoe
[] pathrecord
;
public Maze(int row
, int col
) {
this.row
= row
;
this.col
= col
;
this.maze
= new MazeNoe[row
][col
];
this.queue
= new LinkQueue<>();
this.pathrecord
= new MazeNoe[row
*col
];
}
public void initMazeNode(int data
,int i
,int j
){
this.maze
[i
][j
] = new MazeNoe(data
,i
,j
);
}
public void initMazeNodePathState(){
for (int i
=0;i
<row
;i
++){
for (int j
=0;j
<col
;j
++){
if (this.maze
[i
][j
].val
==1){
continue;
}
if (j
<col
-1 && this.maze
[i
][j
+1].val
==0){
this.maze
[i
][j
].state
[Constant
.RIGHT
] = true;
}
if (i
<row
-1 && this.maze
[i
+1][j
].val
==0){
this.maze
[i
][j
].state
[Constant
.DOWN
] = true;
}
if (j
>0 &&this.maze
[i
][j
-1].val
==0){
this.maze
[i
][j
].state
[Constant
.LEFT
] = true;
}
if (i
>0 && this.maze
[i
-1][j
].val
==0){
this.maze
[i
][j
].state
[Constant
.UP
] = true;
}
}
}
}
public void findMazePath(){
if (maze
[0][0].val
==1){
return;
}
queue
.offer(maze
[0][0]);
while (!queue
.isEmpty()){
MazeNoe top
= queue
.peek();
int x
= top
.x
;
int y
= top
.y
;
if (maze
[x
][y
].state
[Constant
.RIGHT
]) {
maze
[x
][y
].state
[Constant
.RIGHT
] =false;
maze
[x
][y
+1].state
[Constant
.LEFT
] =false;
queue
.offer(maze
[x
][y
+1]);
pathrecord
[x
*col
+y
+1]=maze
[x
][y
];
if (check(x
,y
+1)){
return;
}
}
if (maze
[x
][y
].state
[Constant
.LEFT
]){
maze
[x
][y
].state
[Constant
.LEFT
] = false;
maze
[x
][y
-1].state
[Constant
.RIGHT
] = false;
queue
.offer(maze
[x
][y
-1]);
pathrecord
[x
*col
+y
-1] = maze
[x
][y
];
if (check(x
,y
-1)){
return;
}
}
if (maze
[x
][y
].state
[Constant
.UP
]){
maze
[x
][y
].state
[Constant
.UP
] = false;
maze
[x
-1][y
].state
[Constant
.DOWN
] = false;
queue
.offer(maze
[x
-1][y
]);
pathrecord
[(x
-1)*col
+y
] = maze
[x
][y
];
if (check(x
-1,y
)){
return;
}
}
if (maze
[x
][y
].state
[Constant
.DOWN
]){
maze
[x
][y
].state
[Constant
.DOWN
] = false;
maze
[x
+1][y
].state
[Constant
.UP
] = false;
queue
.offer(maze
[x
+1][y
]);
pathrecord
[(x
+1)*col
+y
] = maze
[x
][y
];
if (check(x
+1,y
)){
return;
}
}
queue
.poll();
}
}
private boolean check(int x
, int y
) {
return x
==this.row
-1 && y
==this.col
-1;
}
public void showMazePath() {
if(pathrecord
[row
*col
-1] == null
){
System
.out
.println("迷宫不存在有效路径");
} else {
System
.out
.println("迷宫路径如下");
int x
= row
-1;
int y
= col
-1;
for(;;){
maze
[x
][y
].val
= '*';
MazeNoe node
= pathrecord
[x
*col
+y
];
if(node
== null
){
break;
}
x
= node
.x
;
y
= node
.y
;
}
for (int i
= 0; i
< row
; i
++) {
for (int j
= 0; j
< col
; j
++) {
if(maze
[i
][j
].val
== '*'){
System
.out
.print('*' + " ");
} else {
System
.out
.print(maze
[i
][j
].val
+ " ");
}
}
System
.out
.println();
}
}
}
5.测试代码
public static void main(String
[] args
) {
Scanner in
= new Scanner(System
.in
);
System
.out
.println("请输入迷宫的行列数:");
int row
,col
,data
;
row
= in
.nextInt();
col
= in
.nextInt();
Maze maze
= new Maze(row
,col
);
System
.out
.println("请输入迷宫的路径");
for (int i
=0;i
<row
;i
++){
for (int j
=0;j
<col
;j
++){
data
= in
.nextInt();
maze
.initMazeNode(data
,i
,j
);
}
}
maze
.initMazeNodePathState();
maze
.findMazePath();
maze
.showMazePath();
}
转载请注明原文地址: https://win8.8miu.com/read-10063.html