求java实现矩阵图上任意两点的最短路径源码基本要求 以矩阵表示地图,{1,1,1,1,1,1,1,1,1,1,1 } {1,0,1,0,1,0,0,0,0,0,1 } {1,0,1,0,0,0,1,0,1,1,1 } {1,0,0,0,1,0,1,0,0,0,1 } {1,0,1,1,0,0,1,0,0,1,1 } 0代表可以通过,1代表不
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/24 00:13:14
求java实现矩阵图上任意两点的最短路径源码基本要求 以矩阵表示地图,{1,1,1,1,1,1,1,1,1,1,1 } {1,0,1,0,1,0,0,0,0,0,1 } {1,0,1,0,0,0,1,0,1,1,1 } {1,0,0,0,1,0,1,0,0,0,1 } {1,0,1,1,0,0,1,0,0,1,1 } 0代表可以通过,1代表不
求java实现矩阵图上任意两点的最短路径源码
基本要求
以矩阵表示地图,
{1,1,1,1,1,1,1,1,1,1,1 }
{1,0,1,0,1,0,0,0,0,0,1 }
{1,0,1,0,0,0,1,0,1,1,1 }
{1,0,0,0,1,0,1,0,0,0,1 }
{1,0,1,1,0,0,1,0,0,1,1 } 0代表可以通过,1代表不可通过
{1,0,1,0,1,1,0,1,0,0,1 }
{1,0,0,0,0,0,0,0,1,0,1 }
{1,0,1,0,1,0,1,0,1,0,1 }
{1,0,0,1,0,0,1,0,1,0,1 }
{1,1,1,1,1,1,1,1,1,1,1 }
可以任意设定起点和终点,可以任意设定地图,输出路径和步数.
比如我要找从右下角的那个1到矩阵第三行第4列的那个元素的最短路径,我想要的是java源代码,最好能附上一些解释.想了好久,没想出来,
求java实现矩阵图上任意两点的最短路径源码基本要求 以矩阵表示地图,{1,1,1,1,1,1,1,1,1,1,1 } {1,0,1,0,1,0,0,0,0,0,1 } {1,0,1,0,0,0,1,0,1,1,1 } {1,0,0,0,1,0,1,0,0,0,1 } {1,0,1,1,0,0,1,0,0,1,1 } 0代表可以通过,1代表不
我用的是递归调用方法,有个小问题就是在打印步数的时候是返向的,原因是就是程序不断的调用自己,到最后判断基值位准退出调用.这才开始从栈里取出方法进行执行的原因.
代码欣赏:
public static int step = 1;
public static StringBuffer printStep = new StringBuffer();
public static int[][] maze ={{1,1,1,1,1,1,1,1,1,1,1},
{1,0,1,0,1,0,0,0,0,0,1 },
{1,0,1,0,0,0,1,0,1,1,1 },
{1,0,0,0,1,0,1,0,0,0,1 },
{1,0,1,1,0,0,1,0,0,1,1 },// 0代表可以通过,1代表不可通过
{1,0,1,0,1,1,0,1,0,0,1 },
{1,0,0,0,0,0,0,0,1,0,1 },
{1,0,1,0,1,0,1,0,1,0,1 },
{1,0,0,1,0,0,1,0,1,0,1 },
{1,1,1,1,1,1,1,1,1,1,1 } };
public static void main(String[] args) {
int i, j; //循环记数变量
Sample.way(1, 1);//二维数组起始值从下标1,1开始
System.out.println("起点从坐标 x = 1, y = 1开始");
System.out.println("终点坐标是 x = 8, y = 9结束");
System.out.println("这是迷宫图表");
System.out.println(" 0 1 2 3 4 5 6 7 8 9 10");
System.out.println(" +---+---+---+---+---+---+---+---+---+---+---+---+---+");
for(i = 0; i < 10; i++){
System.out.print(" " + i + "‖");
for(j = 0; j < 11; j++)
System.out.print("-" + maze[i][j] + "-‖");
System.out.println("");
System.out.println(" +---+---+---+---+---+---+---+---+---+---+---+---+---+");
}
//打印显示步数
System.out.print(printStep.toString());
}
public static boolean way(int x, int y){
if(maze[8][9] == 2)//代表递归终止条件(也就是当走出出口时标记为 2)
return true;
else{
if(maze[y][x] == 0){
maze[y][x] = 2;
/*
* 下面if判断条件代表当前坐标为基点,
* 根据判断对当前位置进行递归调用:如:
* 往上、往右上、往右、往右下、往下、
* 往左下、往左、往左上的坐标是否可走,
* 判断是否可走的返回条件是:
* 2代表可通过、1代表不能通过、3表示已经走过,但是未能走通.
*/
if(way(x, y - 1)){
printStep.append("第 " + step + " 步的所走的位置是 x = " + x + " y = " + y + "\n");
step++;
return true;
}else if(way(x + 1, y - 1)){
printStep.append("第 " + step + " 步的所走的位置是 x = " + x + " y = " + y + "\n");
step++;
return true;
}else if(way(x + 1 , y)){
printStep.append("第 " + step + " 步的所走的位置是 x = " + x + " y = " + y + "\n");
step++;
return true;
}else if(way(x + 1, y + 1)){
printStep.append("第 " + step + " 步的所走的位置是 x = " + x + " y = " + y + "\n");
step++;
return true;
}else if(way(x, y + 1)){
printStep.append("第 " + step + " 步的所走的位置是 x = " + x + " y = " + y + "\n");
step++;
return true;
}else if(way(x - 1, y + 1)){
printStep.append("第 " + step + " 步的所走的位置是 x = " + x + " y = " + y + "\n");
step++;
return true;
}else if(way(x - 1, y)){
printStep.append("第 " + step + " 步的所走的位置是 x = " + x + " y = " + y + "\n");
step++;
return true;
}else if(way(x - 1, y - 1)){
printStep.append("第 " + step + " 步的所走的位置是 x = " + x + " y = " + y + "\n");
step++;
return true;
}else{
maze[y][x] = 3;
return false;
}
}else
return false;
}
}
复制代码前需要楼主自己创建个 类
Sample.way(1, 1);这句代码是我的类的静态调用,改下XXXXX.way(1, 1);
XXXXX代表你创建的类.
下面是这个程序运行后的截图