链式队列实现迷宫寻径
来源:互联网 发布:手机信号干扰软件 编辑:程序博客网 时间:2024/05/17 08:11
测试类
public class TestQueue1 {
public static void main(String[] args) {
int[][] map={
{1,1,1,1,1,1,1,1,1,1},
{1,0,1,0,1,0,1,1,1,1},
{1,0,0,1,0,1,0,1,1,1},
{1,1,1,1,0,1,0,0,1,1},
{1,0,1,0,1,1,1,0,1,1},
{1,1,0,1,1,1,0,0,0,1},
{1,0,1,1,0,0,1,1,0,1},
{1,1,1,1,1,1,1,1,1,1}
};
int row=map.length,col=map[0].length;
System.out.println("迷宫矩阵:");
for(int i=1;i<row-1;i++){
for(int j=1;j<col-1;j++){
System.out.print(map[i][j]+" ");
}
System.out.println();
}
Migong4 mi=new Migong4(map);
if(mi.findpath()){//寻找路径,存在路径时返回1
System.out.println("迷宫有路,走迷宫的一条路径为:");
//输出迷宫的路径
Migong4.Point1[] points=mi.getpath();
for(int o=0;o<points.length;o++){
System.out.print("("+points[o].x+","+points[o].y+")");
}
}else System.out.println("迷宫无路!");
}
}
队列接口
public interface IQueue<E> {//定义队列接口
boolean enqueue(E item); //入队列操作
E dequeue(); //出队列操作
E peek(); //取对头元素
int size(); //求队列的长度
boolean isEmpty(); //判断队列是否为空
boolean isFull(); //判断队列是否为满
}
链式数据结构
public class QueueNode<E> {//链式结构
private E data; // 数据域
private QueueNode<E> next; // 引用域
//构造函数
public QueueNode(){}
public QueueNode(E data) {
this.data = data;
}
public QueueNode(E data, QueueNode<E> next) {
this.data = data;
this.next = next;
}
//数据域get属性
public E getData() {
return data;
}
//数据域set属性
public void setData(E data) {
this.data = data;
}
//引用域get属性
public QueueNode<E> getNext() {
return next;
}
//引用域get属性
public void setNext(QueueNode<E> next) {
this.next = next;
}
}
单链表链实现队列数据结构
public class LinkQueue<E> implements IQueue<E> {//实现队列
private QueueNode<E> front; // 队列头指示器
private QueueNode<E> rear; // 队列尾指示器
private int maxsize; // 队列的容量,假如为0,不限容量
private int size; // 队列数据元素个数
// 初始化链队列
public LinkQueue() {
front = rear = null;
size = 0;
maxsize = 0;
}
// 初始化限容量的链队列
public LinkQueue(int maxsize) {
super();
this.maxsize = maxsize;
}
// 入队列操作
public boolean enqueue(E item) {
QueueNode<E> newnode = new QueueNode<E>(item);
if (!isFull()) {
if (isEmpty()) {
front = newnode;
rear = newnode;
} else {
rear.setNext(newnode);
rear = newnode;
}
++size;
return true;
} else
return false;
}
// 出队列操作
public E dequeue() {
if (isEmpty())
return null;
QueueNode<E> node = front;
front = front.getNext();
if (front == null) {
rear = null;
}
--size;
return node.getData();
}
// 取对头元素
public E peek() {
if (!isEmpty()) {
return front.getData();
} else
return null;
}
// 求队列的长度
public int size() {
// TODO Auto-generated method stub
return size;
}
// 判断队列是否为空
public boolean isEmpty() {
if ((front == rear) && (size == 0)) {
return true;
} else {
return false;
}
}
// 判断队列是否为满
public boolean isFull() {
if (maxsize != 0 && size == maxsize) {
return true;
} else {
return false;
}
}
}
主方法
public class Migong4 {//链式队列存储结构实现迷宫
int[][] maze;
int row,col;
LinkQueue< Point1> sta;
Point1[] move={new Point1(0,1),new Point1(1,1),//八个方向
new Point1(1,0),new Point1(1,-1),new Point1(0,-1),
new Point1(-1,1),new Point1(-1,0),new Point1(-1,1)};
public Migong4(int[][] map){//构造方法
row=map.length+2;
col=map[0].length+2;
sta= new LinkQueue<Point1>();//创建栈 使用链式栈,也可以使用顺序栈
maze=new int[row][col];
for(int X=1;X<row-1;X++){
for(int Y=1;Y<col-1;Y++){
maze[X][Y]=map[X-1][Y-1];
}
}
}
public boolean findpath(){//探寻路径
row=maze.length;
col=maze[0].length;
int x,y,d,i,j;
Point1 temp=null;
temp=new Point1(1,1,-1);
sta.enqueue(temp);
while(!sta.isEmpty()){
temp=sta.dequeue();
x=temp.x;y=temp.y;d=temp.d+1;
while(d<8){//广度优先搜索
i=x+move[d].x; j=y+move[d].y;
if(maze[i][j]==1){
temp=new Point1(x,y,d);
sta.enqueue(temp);
x=i;y=j;maze[x][y]=-1;
if(x==row-2 && y==col-2){
temp=new Point1(x,y,-1);
sta.enqueue(temp);
return true;//迷宫可达
}
else d=0;//向下继续便利
}
else d++; //
}
}
return false;
}
public Point1[] getpath(){
Point1[] points = new Point1[sta.size()];
for(int k=points.length-1;k>=0;k--){
points[k]=sta.dequeue();
}
return points;
}
public class Point1{
public int x,y,d;
public Point1(int x,int y){
this.x=x;
this.y=y;
}
public Point1(int x,int y,int d){
this.x=x;
this.y=y;
this.d=d;
}
}
}
- 链式队列实现迷宫寻径
- 链式栈实现迷宫寻径
- 链式队列的实现
- 链式队列的实现
- 链式队列的实现
- 链式队列的实现。
- 链式队列的实现
- 链式队列的实现
- 链式队列的实现
- 队列的链式实现
- C实现链式队列
- 链式队列的实现
- 链式队列简单实现
- 链式队列实现
- 链式队列的实现
- 链式队列的实现
- 链式队列的实现
- 链式队列实现
- 静态与动态RNN函数比较
- Application.StartupPath
- 企业中常用的RAID数据盘简介
- webpack插件-html-webpack-plugin
- INSERT INTO SELECT 实例
- 链式队列实现迷宫寻径
- 沉浸式透明渐变
- sdnu1521手写堆排
- C语言小游戏:猜数字
- IST改进算法之Two-Step Iterative Shrinkage/Thresholding(TwIST)
- 2017/11/27学习笔记 Mysql基础
- 怎么把PHP中复选框选中的多个值写入到数据库中
- Couldn't save uncommitted changes,Tried to save uncommitted changes in stash before update,but faile
- linux C 进程结束 exit与_exit函数