本文目录一览:
java迷宫路径总条数问题
int[][] data是你的迷宫数组,返回值是路径总条数,不需要递归
public int findWayCount(int[][] data) {
int[][] way = new int[data.length][];
for (int m = 0; m data.length; m++) {
way[m] = new int[data[m].length];
for (int n = 0; n data[m].length; n++) {
if (data[m][n] == 0) {
way[m][n] = 0;
} else if (m == 0 n == 0) {
way[m][n] = data[0][0];
} else if (m == 0) {
way[m][n] = way[m][n - 1];
} else if (n == 0) {
way[m][n] = way[m - 1][n];
} else {
way[m][n] = way[m][n - 1] + way[m - 1][n];
}
}
}
JAVA迷宫问题,怎么输出所走路径的坐标
在Way函数的if(Maza[Loci][Locj]==0) 语句里面Maza[Loci][Locj]=2;前面加一句
System.out.println("The Maza route is ("+Loci+","+Locj+")");就打印出程序所走路径的坐标了.
java自动走迷宫线程问题
以一个M×N的长方阵表示迷宫,0和1分别表示迷宫中的通路和障碍。设计一个程序,对任意设定的迷宫,求出一条从入口到出口的通路,或得出没有通路的结论。
(1) 根据二维数组,输出迷宫的图形。
(2) 探索迷宫的四个方向:RIGHT为向右,DOWN向下,LEFT向左,UP向上,输出从入口到出口的行走路径。
求Java关于迷宫的算法(用栈实现)
package com.Albert.LabyringhStack;
public class Point {
int x;
int y;
int direction; //direction指向此点附近的一个点 应该有四个 编号为1 2 3 4
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getDirection() {
return direction;
}
public void setDirection(int direction) {
this.direction = direction;
}
public void addDirection(){
this.direction++;
}
public Point() {
}
public Point(int x, int y) {
super();
this.x = x;
this.y = y;
this.direction = 1;
}
public Point(int x, int y, int direction) {
super();
this.x = x;
this.y = y;
this.direction = direction;
}
}
package com.Albert.LabyringhStack;
import java.util.*;
public class LabyringhStack {
public Point S;
public Point F;
char[][] mazemap;
StackPoint path;
public LabyringhStack() {
}
public LabyringhStack(char[][] ma) { //初始化 存入数组
this.mazemap = new char[ma.length][ma[0].length];
for(int i=0;ima.length;i++){
for(int j=0;jma[0].length;j++){ //mazemap[0]必须有元素 不可为空
this.mazemap[i][j] = ma[i][j];
}
}
S = returnPlace('S');
F = returnPlace('F');
}
public Point returnPlace(char s){ //返回数组中字符的位置
Point point = new Point();
for(int i=0;ithis.mazemap.length;i++){
for(int j=0;jthis.mazemap[0].length;j++){ //mazemap[0]必须有元素 不可为空
if(this.mazemap[i][j]==s)
{ point.setX(i);
point.setY(j);
point.setDirection(1);
}
}
}
return point;
}
public char returnChar(Point point){
if(point.getX()=0point.getY()=0)
return this.mazemap[point.getX()][point.getY()];
else
return '#';
}
public void replacePlace(Point point, char s){ //更改特定位置处的字符
mazemap[point.getX()][point.getY()] = s;
}
public void printPath(){
StackPoint tempPath = new StackPoint();
while(!path.empty()){ //对栈进行反序
tempPath.push(path.pop());
}
while(!tempPath.empty()){
System.out.print("("+tempPath.peek().getX()+","+tempPath.pop().getY()+")");
}
}
public boolean getPath(){ //取得路径的算法 如果有路径就返回真
path = new StackPoint();
S.setDirection(1);
path.push(S);
replacePlace(S, 'X');
while(!path.empty()){
Point nowPoint = path.peek(); //取得当前位置
if(nowPoint.getX()==F.getX()nowPoint.getY()==F.getY()){
//printPath();
return true;
}
Point temp = new Point(); //存放下一个可走的位置
int find = 0; //标志 是否可向下走
while(nowPoint.getDirection()5find==0){
switch(nowPoint.getDirection()){
case 1:temp = new Point(nowPoint.getX(),nowPoint.getY()-1,1); break; //取得当前位置左边的位置
case 2:temp = new Point(nowPoint.getX()+1,nowPoint.getY(),1); break;//取得当前位置下边的位置
case 3:temp = new Point(nowPoint.getX(),nowPoint.getY()+1,1); break;//取得当前位置右边的位置
case 4:temp = new Point(nowPoint.getX()-1,nowPoint.getY(),1); break;//取得当前位置上边的位置
}
nowPoint.addDirection(); //指向下一个需要验证的点
if(returnChar(temp)=='O'||returnChar(temp)=='F') find = 1; //如果能向下走则置为1
}
if(find==1){ //如果可走就进栈
replacePlace(temp, 'X'); //设置成X 防止回走
// printArr();
path.push(temp);
}else{ //如果不可走就退栈
replacePlace(nowPoint, 'O');
path.pop();
}
}
return false;
}
public void printArr(){
for(int i=0;imazemap.length;i++){
for(int j=0;jmazemap[0].length;j++){ //mazemap[0]必须有元素 不可为空
System.out.print(mazemap[i][j]);
}
System.out.println();
}
System.out.println();
}
}
package com.Albert.LabyringhStack;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
char[][] mazemap = {
{'M','M','M','M','M','M','M','M'},
{'M','S','O','O','M','M','M','M'},
{'M','M','M','O','M','M','M','M'},
{'M','M','O','O','O','O','M','M'},
{'M','M','M','M','M','F','M','M'},
{'M','M','M','M','M','M','M','M'},
{'M','M','M','M','M','M','M','M'}
};
LabyringhStack solution = new LabyringhStack(mazemap);
if(solution.getPath()){
System.out.print("迷宫路径如下:");
solution.printPath();
}
else {
System.out.println("没有可走的路");
}
}
}
求用java语言寻找走出迷宫路线的算法
首先给定一个初始坐标,然后构建一个容器保存坐标值,之后进行迭代,横坐标+1,或者纵坐标+1,这个顺寻自己定义(四个方向),经过的“路径”保存在那个容器中,如果遇到死角,以此往回迭代,在容器中将遇到死角的那个坐标删除。最后找到自己定义的那个迷宫出口坐标。