迷宫问题:
实现的思路:
- 使用队列实现,一种广度搜索
- 队列选用循环单链表为基础实现队列ADT
- 前进方向判断
- 遇到死路要回退, 链表的抽象节点类不方便满足需求,新建position节点类(新增prev域指向前驱)
1. 循环单链表为基础的队列
- CircSinglyLinkedList.java
package indi.yyh.datastru.project2.CircleLink;
import indi.yyh.datastru.project2.Node;
public class CircSinglyLinkedList<T> extends Object {
public Node<T> head;
public int length = 0;
public CircSinglyLinkedList() {
this.head = new Node<T>();
this.head.next = this.head;
}
public CircSinglyLinkedList(T[] values) {
this();
this.length = values.length;
Node<T> rear = this.head;
for (int i = 0; i < values.length; i++) {
rear.next = new Node<T>(values[i], null);
rear = rear.next;
}
rear.next = this.head;
}
public boolean isEmpty(){
return this.length== 0;
}
public int insert(int i, T x) {
int count = 0;
Node<T> q = new Node<T>(x, null);
Node<T> p = this.head.next;
while (count != i-1) {
p = p.next;
count++;
}
q.next = p.next;
p.next = q;
this.length++;
return i;
}
public int insert(T x) {
return this.insert( this.length , x);
}
public boolean insertHead(T x) {
Node<T> q = new Node<T>(x, null);
Node<T> p = this.head;
q.next = p.next;
p.next = q;
this.length++;
return true;
}
public T remove(int i){
Node<T> front = this.head;
for(int j = 0; j<i ;j++){
front = front.next;
}
T old = front.next.data;
if(front.next.next!=null){
front.next = front.next.next;
}else{
front.next = this.head;
}
this.length--;
return old;
}
public String toString() {
String str = "(";
for (Node<T> p = this.head.next; p != this.head; p = p.next) {
str += p.data.toString();
if (p.next != this.head) {
str += ",";
}
}
return str + ")";
}
}
package indi.yyh.datastru.project3;
public interface Queue<T> {
public abstract boolean isEmpty();
public abstract boolean add(T x);
public abstract T peek();
public abstract T poll();
}
public class ScQueue<T> implements Queue<T>{
private CircSinglyLinkedList<T> list;
public ScQueue(){
this.list = new CircSinglyLinkedList<T>();
}
2. 使用position节点类记录当前位置
package indi.yyh.datastru.project3.maze;
public class position<T> {
public position<T> prev;
public int x;
public int y;
public position(int x, int y, position<T> prev) {
this.x = x;
this.y = y;
this.prev = prev;
}
public position(){
this(1,1,null);
}
}
3. 判断前进方向, 并实现路径入队出对操作
- findLoad() //代码具体自行优化,老子时间有限,水平不高
public static String findLoad(int x, int y) {
ScQueue path = new ScQueue();
position p = new position();
position pre = p;
path.add(("(" + p.x + "," + p.y + ")"));
while (x != EX || y != EY) {
if (maze[x - 1][y] == 0) {
pre = p;
maze[x][y] = 2;
x -= 1;
p = new position(x, y, pre);
path.add(("(" + p.x + "," + p.y + ")"));
} else if (maze[x][y + 1] == 0) {
pre = p;
maze[x][y] = 2;
y += 1;
p = new position(x, y, pre);
path.add(("(" + p.x + "," + p.y + ")"));
} else if (maze[x][y - 1] == 0) {
pre = p;
maze[x][y] = 2;
y -= 1;
p = new position(x, y, pre);
path.add(("(" + p.x + "," + p.y + ")"));
} else if (maze[x + 1][y] == 0) {
pre = p;
maze[x][y] = 2;
x += 1;
p = new position(x, y, pre);
path.add(("(" + p.x + "," + p.y + ")"));
} else {
maze[x][y] = -1;
p = p.prev;
x = p.x;
y = p.y;
path.poll(path.size() - 1);
}
}
return path.toString();
}
实现效果

本人练习数据结构代码,没有怎么考虑优化,大佬勿喷,萌新仅供参考