使用2D数组和堆栈在java中构建迷宫
我需要用2D数组和堆栈构建一个迷宫。数组大小是固定的。起点是(0,0)。数组应该从文件中读取,但在本例中,我假设值只是为了说明问题 我似乎找不到合适的算法来遍历2D数组并保存到堆栈的路径。如果我被困在当前一行,这会让我回到上一行。PS:1是墙,0是路径。这个问题需要用户输入一个数组,但为了简单起见,我提供了一个 这里是数组:使用2D数组和堆栈在java中构建迷宫,java,arrays,multidimensional-array,stack,maze,Java,Arrays,Multidimensional Array,Stack,Maze,我需要用2D数组和堆栈构建一个迷宫。数组大小是固定的。起点是(0,0)。数组应该从文件中读取,但在本例中,我假设值只是为了说明问题 我似乎找不到合适的算法来遍历2D数组并保存到堆栈的路径。如果我被困在当前一行,这会让我回到上一行。PS:1是墙,0是路径。这个问题需要用户输入一个数组,但为了简单起见,我提供了一个 这里是数组: 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 1 0 0 0 1 0 0 0 我需要从位置(0,0)开始,出口应该在最后一行。如果我被困住了,我需要
0 1 0 0 0
0 1 0 0 0
0 0 0 0 0
1 1 1 0 0
0 1 0 0 0
我需要从位置(0,0)开始,出口应该在最后一行。如果我被困住了,我需要上去寻找另一条路;那就是跳出堆栈
以下是我的想法:
public class Maze {
Maze currentPos = new Maze();
int position = maze[0][0];
public Maze()
{
}
public Maze(Maze currentPos)
{
this.currentPos = currentPos;
position = maze[0][0];
}
Stack stack = new Stack ();
public static int[][] maze = new int[][] {
{0,1,0,0,0},
{0,1,0,0,0},
{0,0,0,0,0},
{1,1,1,0,0},
{0,1,0,0,0}
};
public boolean UP (int i, int j)
{
if (maze [i-1][j] == 0)
return true;
return false;
}
public boolean DOWN (int i, int j)
{
if (maze [i+1][j] == 0)
return true;
return false;
}
public boolean RIGHT(int i,int j)
{
if (maze [i][j+1] == 0)
return true;
return false;
}
public boolean LEFT(int i,int j)
{
if (maze [i][j-1] == 0)
return true;
return false;
}
public boolean isExit (int i, int j)
{
if (j == 6)
return true;
return false;
}
public void setPosition(int i , int j)
{
position = maze[i][j];
}
public void solve()
{
for (int i=0; i<maze.length; i++)
{
for (int j=0; j<maze.length; j++)
{
while(! currentPos.isExit(i,j));
{
if ( currentPos.DOWN(i,j)) stack.push(i+1,j);
if ( currentPos.LEFT(i,j)) stack.push(i,j-1);
if ( currentPos.RIGHT(i,j)) stack.push(i,i+1);
if ( currentPos.UP(i,j)) stack.push(i-1,j);
}
}
}
}
}
公共类迷宫{
迷宫当前位置=新迷宫();
int位置=迷宫[0][0];
公共迷宫()
{
}
公共迷宫(迷宫位置)
{
this.currentPos=currentPos;
位置=迷宫[0][0];
}
堆栈=新堆栈();
公共静态int[][]迷宫=新int[][]{
{0,1,0,0,0},
{0,1,0,0,0},
{0,0,0,0,0},
{1,1,1,0,0},
{0,1,0,0,0}
};
公共布尔向上(int i,int j)
{
if(迷宫[i-1][j]==0)
返回true;
返回false;
}
公共布尔向下(int i,int j)
{
if(迷宫[i+1][j]==0)
返回true;
返回false;
}
公共布尔右(int i,int j)
{
如果(迷宫[i][j+1]==0)
返回true;
返回false;
}
公共布尔左(int i,int j)
{
if(迷宫[i][j-1]==0)
返回true;
返回false;
}
公共布尔isExit(int i,int j)
{
如果(j==6)
返回true;
返回false;
}
公共无效设置位置(int i,int j)
{
位置=迷宫[i][j];
}
公共空间解决方案()
{
对于(int i=0;i,这里有一些东西可以让您开始:
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class Maze {
//keep reference to start point
int startRow, startCol;
//keep reference to addresses (row, col) that has been checked
List<Integer[]> visited;
//a stack that represents the path (solution)
Stack<Integer[]> path;
public Maze(int startRow, int startCol) {
this.startRow = startRow; //add: check input validity
this.startCol = startCol;
visited = new ArrayList<>();
path = new Stack<>();
}
public static int[][] mazeValues = new int[][] {
{0,1,0,0,0},
{0,0,0,1,0},
{1,1,1,0,0},
{1,1,1,0,1},
{0,0,0,0,0}
};
void solve(){
boolean isSolved = solve(startRow, startCol);
if( isSolved ) {
pathFound();
} else {
noPathFound();
}
}
private boolean solve(int row, int col) {
//check if target found
if(isTargert(row,col)) {
//add target to path
path.push(new Integer[]{row,col});
return true;
}
//check if address is a wall
if(isWall(row,col)) {
return false;
}
//check if visited before
if(isVisited(row, col)) {
return false;
}
//mark as visited
visited.add(new Integer[]{row,col});
//add to path
path.push(new Integer[]{row,col});
//check all neighbors (allows diagonal move)
for (int rowIndex = row-1; rowIndex <= (row+1) ; rowIndex++ ) {
for (int colIndex = col-1; colIndex <= (col+1) ; colIndex++ ) {
if( (rowIndex == row) && (colIndex == col)) {//skip current address
continue;
}
if( ! withInMaze(rowIndex, colIndex)) {
continue;
}
if( solve(rowIndex, colIndex)) {
return true; //solution found
}
}
}
//solution not found after checking all neighbors
path.pop(); //remove last from stack;
return false;
}
//check if address is a target
private boolean isTargert(int row, int col) {
//target set to last row / col. Change taget as needed
return (row == (mazeValues.length-1))&& (col == (mazeValues[0].length -1)) ;
}
//check if address is a wall
private boolean isWall(int row, int col) {
return mazeValues[row][col] == 1;
}
private boolean isVisited(int row, int col) {
for (Integer[] address : visited ) {
if((address[0]==row) && (address[1]==col)) {
return true;
}
}
return false;
}
//return true if rowIndex, colIndex are with in mazeValues
private boolean withInMaze(int rowIndex, int colIndex) {
return (rowIndex < mazeValues.length)&& (rowIndex >= 0)
&&(colIndex < mazeValues[0].length) && (colIndex >=0);
}
private void noPathFound() {
System.out.println("No path found............");
}
private void pathFound() {
System.out.println("Path found");
for (Integer[] address : path) {
int row = address[0]; int col = address[1];
System.out.println("Address: "+ row +"-"+ col
+" value: "+ mazeValues[row][col]);
}
}
public static void main(String[] args) {
Maze maze = new Maze(0,0);
maze.solve();
}
}
import java.util.ArrayList;
导入java.util.List;
导入java.util.Stack;
公共类迷宫{
//参考起点
int startRow,startCol;
//保留对已检查的地址(行、列)的引用
访问名单;
//表示路径(解决方案)的堆栈
堆栈路径;
公共迷宫(int startRow、int startCol){
this.startRow=startRow;//添加:检查输入有效性
this.startCol=startCol;
已访问=新建ArrayList();
路径=新堆栈();
}
公共静态int[]mazeValues=新int[]{
{0,1,0,0,0},
{0,0,0,1,0},
{1,1,1,0,0},
{1,1,1,0,1},
{0,0,0,0,0}
};
void solve(){
布尔值IsResolved=solve(startRow,startCol);
如果(未解决){
路径发现();
}否则{
noPathFound();
}
}
私有布尔解算(int行,int列){
//检查是否找到目标
如果(isTargert(行、列)){
//将目标添加到路径
push(新整数[]{row,col});
返回true;
}
//检查地址是否为墙
if(isWall(行、列)){
返回false;
}
//检查之前是否访问过
如果(已访问(行、列)){
返回false;
}
//标记为已访问
add(新整数[]{row,col});
//添加到路径
push(新整数[]{row,col});
//检查所有邻居(允许对角移动)
对于(int-rowIndex=row-1;rowIndex=0);
}
私有void noPathFound(){
System.out.println(“未找到路径……);
}
私有void路径发现(){
System.out.println(“找到的路径”);
for(整数[]地址:路径){
int行=地址[0];int列=地址[1];
System.out.println(“地址:“+行+”-“+列
+“值:“+mazeValues[行][col]);
}
}
公共静态void main(字符串[]args){
迷宫=新迷宫(0,0);
maze.solve();
}
}
对于一般的迷宫路径查找算法,我建议从开始,我需要一个能够解决迷宫问题的算法。这一个是不够的。我需要知道,如果我所在的行中被1阻塞,何时返回。如果你在真实的迷宫中,你会怎么做?“我需要一个算法…”这不是一个问题,而是一个故事。你的问题是什么?