博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java迷宫路径,Java中的迷宫路径查找器
阅读量:1537 次
发布时间:2019-04-21

本文共 2439 字,大约阅读时间需要 8 分钟。

我正在尝试使用递归来解决迷宫问题 . 在下面的代码中,MazeCoord是一个程序员创建的类型,用于存储坐标类型位置 . 格式为MazeCoord(int x,int y) . 我的程序现在编译,到达方法的某些部分并忽略另一个,因此在所有情况下都说“找不到路径”,只将起始位置存储在LinkedList mazePath中 . 在search()方法中有一个注释掉的部分,这是我尝试的另一种方式,但我很确定它是错误的而不是这样做的方法 .

任何帮助表示赞赏 .

递归代码:

/ **返回迷宫中的路径 . 第一个元素是起始位置,最后一个元素是退出位置 . 如果没有路径,或者在搜索之前调用此路径,则返回空列表 .

@return迷宫路径* /

public LinkedList getPath() {

return mazePath;

}

/ **如果有迷宫,找到迷宫中的路径 . 客户端可以访问通过getPath方法找到的路径 . @return是否找到了路径 . * /

public boolean search() {

currentLoc = new MazeCoord(startLoc.getRow(), startLoc.getCol());

visitedPath = new boolean[mazeData.length][mazeData[0].length];

mazePath=new LinkedList();

if(hasWallAt(startLoc) || hasWallAt(endLoc)){

return false;

}

else{

mazePath.add(currentLoc);

return appendToSearch(currentLoc.getRow(), currentLoc.getCol());

}

/**

System.out.println("try1");

mazePath.add(new MazeCoord(startLoc.getRow(), startLoc.getCol()));

boolean searchResult = appendToSearch(numRows()-1, numCols()-1);

System.out.println("test: " + searchResult);

System.out.println("test2: row, col --> " + (numRows()-1) + " , " + (numCols()-1));

System.out.println("test3: wallValue:" + hasWallAt(new MazeCoord(numRows()-1,numCols()-1)));

if(searchResult){

System.out.println("try2");

mazePath.add(new MazeCoord(numRows()-1, numCols()-1));

}

return searchResult;

*/

}

/ ** search()方法的辅助函数将执行实际递归以获取通过迷宫的路径@param行currentLoc @param col的行currentLoc @return的列如果路径可用,则为true * /

private boolean appendToSearch(int row, int col) {

//Check if within the maze

if((row - 1 < 0) || (col - 1 < 0) || (row + 1 > numRows()) || (col + 1 > numCols())){

return false;

}

//Check if the position is the exit location

if(row == endLoc.getRow() && col == endLoc.getCol()){

mazePath.add(new MazeCoord(row, col));

return false;

}

//Check for Wall

if(hasWallAt(new MazeCoord(row, col))){

return false;

}

//Check if the position has already been visited

if(visitedPath[row][col]){

return false;

}

//If all pass --> add to visitedPath

visitedPath[row][col]=true;

//Check to the Right

if(appendToSearch(row, col + 1)){

mazePath.add(new MazeCoord(row, col + 1));

return true;

}

//Check Downwards

else if(appendToSearch(row + 1, col)){

mazePath.add(new MazeCoord(row + 1, col));

return true;

}

//Check to the Left

else if(appendToSearch(row, col - 1)){

mazePath.add(new MazeCoord(row, col - 1));

return true;

}

//Check Upwards

else if(appendToSearch(row - 1, col)){

mazePath.add(new MazeCoord(row - 1, col));

return true;

}

return false;

}

转载地址:http://uysdy.baihongyu.com/

你可能感兴趣的文章
linux常用命令
查看>>
mysql主从数据库搭建详细步奏
查看>>
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
查看>>
centos下发布jar包项目并查看日志
查看>>
nodemcu板子控制flash按键和led灯的io口
查看>>
数据库访问性能优化
查看>>
性能优化之数据库和数据源连接池配置
查看>>
高并发系统架构
查看>>
数据分析入门(二)
查看>>
从建表到SQL优化
查看>>
FindInSet性能优化
查看>>
MAC电脑:安装mysql报ERROR 1045 (28000)Access denied
查看>>
Linux Swap空间利用率过高
查看>>
网关、负载均衡、服务注册发现什么关系?
查看>>
桥接模式
查看>>
Servlet的生命周期02
查看>>
EL表达式的基本用法
查看>>
jstl标签库常见用法
查看>>
国际化输出笔记
查看>>
自定义jstl标签用法案例
查看>>