迷宫问题
给出一个矩阵:
假定矩阵中元素为1时,表示此处有障碍物,无法通过;
元素为0时,可以通过。
现在假设一只小老鼠被放置在起点(1,1)处,而出口是(8,10),现在要记录小老鼠从起点到出口所走过的路径。
解决方案:
迷宫问题是堆栈应用的典型例子,我们可以利用堆栈记录小老鼠走过的每一步(把走过的路径标识成2,表示小老鼠已经走过)——对应于堆栈的入栈;如果走到一个死胡同的时候,就需要回退一步——对应于堆栈的出栈。
算法思想
1 是否已经达到出口处,是,结束循环,否则执行步骤2
2 向上走一步,可以通过的时候,将其记录到栈中,否则执行步骤3
3 向右走一步,可以通过的时候,将其记录到栈中,否则执行步骤4
4 向下走一步,可以通过的时候,将其记录到栈中,否则执行步骤5
5 向左走一步,可以通过的时候,将其记录到栈中,否则执行步骤6
6 回退一步,并且进行出栈一次,执行步骤1
程序
以链表形式存储堆栈
public class LinkList {
/**
*
* @author ****
*创建链表数据结构
*/
class Node{
Node nextNode;
int column;
int row;
public Node(){
}
public Node(int row,int column){
this.row= row;
this.column=column;
this.nextNode = null;
}
}
//头节点
Node headNode;
private int index ;
public LinkList() {
// TODO Auto-generated constructor stub
//创建一个带头节点的链表
//-1表示头节点中没有存储数据
headNode = new Node(-1, -1);
index=0;
}
public void insert(int row,int column){
Node currentNode = new Node(row, column);
if(index==0){
//插入到头节点之后
headNode.nextNode =currentNode;
index++;
}else{
//寻找链表中的最后一个节点
Node preNode = indexNode(index);
preNode.nextNode = currentNode;
index++;
}
}
public void deleteNode(){
//删除末尾节点
//找到倒数第二个节点
Node node= indexNode(index-1);
node.nextNode=null;
index--;
}
public Node indexNode(int index){
int count = 0;
//第一个节点;
Node currentNode = headNode;
while(count<index){
currentNode = currentNode.nextNode;
count++;
}
return currentNode;
}
public int getIndex(){
return index;
}
}
记录小老鼠走过的路径public class Maze {
/**
* 定义出口坐标(8,10)
*/
public static int ExitRow = 8;
public static int ExitColumn = 10;
/**
* 定义当前坐标
* 初始时为(1,1)
*/
public static int currentRow = 1;
public static int currentColumn = 1;
/**
* 把走过的方格放入到栈中
*/
public static LinkList hasExperienced = new LinkList();
//利用二维数组模拟迷宫10*12
public static int[][] MAZE = {
{1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,1,1,1,0,1,1,1,1},
{1,1,1,0,1,0,1,0,0,1,1,1},
{1,1,1,0,1,0,0,0,1,1,1,1},
{1,1,1,0,0,0,1,0,1,1,1,1},
{1,1,1,0,1,1,1,0,0,0,1,1},
{1,1,1,0,1,1,1,0,1,1,1,1},
{1,1,1,0,0,0,1,0,0,0,1,1},
{1,1,1,1,1,1,1,1,1,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1},
};
public static void main(String[] args) {
System.out.println("打印迷宫");
//迷宫的行和列
int ROW = MAZE.length;
int COLUMN = MAZE[0].length;
for(int i=0;i<ROW;i++){
for(int j=0;j<COLUMN;j++){
System.out.print(MAZE[i][j]+" ");
}
System.out.println();
}
//先把起点放入到栈中
hasExperienced.insert(currentRow,currentColumn);
//2表示该方格已经走过
MAZE[currentRow][currentColumn] = 2;
int n=0;
while(currentRow!=ExitRow||currentColumn!=ExitColumn){
//开始下一步
if(MAZE[currentRow-1][currentColumn]==0){
//可以往上走
currentRow--;
MAZE[currentRow][currentColumn] = 2;
hasExperienced.insert(currentRow,currentColumn);
}else if (MAZE[currentRow][currentColumn+1]==0) {
//right
currentColumn++;
MAZE[currentRow][currentColumn] = 2;
hasExperienced.insert(currentRow,currentColumn);
}else if (MAZE[currentRow+1][currentColumn]==0) {
//down
currentRow++;
MAZE[currentRow][currentColumn] = 2;
hasExperienced.insert(currentRow,currentColumn);
}else if(MAZE[currentRow][currentColumn-1]==0) {
//left
currentColumn--;
MAZE[currentRow][currentColumn] = 2;
hasExperienced.insert(currentRow,currentColumn);
}else {
hasExperienced.deleteNode();
currentRow = hasExperienced.indexNode(hasExperienced.getIndex()).row;
currentColumn= hasExperienced.indexNode(hasExperienced.getIndex()).column;
}
}
System.out.println("老鼠走过的路径");
//迷宫的行和列
for(int i=0;i<ROW;i++){
for(int j=0;j<COLUMN;j++){
System.out.print(MAZE[i][j]+" ");
}
System.out.println();
}
}
}
程序结果