华为机试---迷宫问题

题目描述

定义一个二维数组N*M(其中2<=N<=10;2<=M<=10),如5 × 5数组下所示: 

int maze[5][5] = {

        0, 1, 0, 0, 0,

        0, 1, 0, 1, 0,

        0, 0, 0, 0, 0,

        0, 1, 1, 1, 0,

        0, 0, 0, 1, 0,

};

它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。入口点为[0,0],既第一空格是可以走的路。

Input

一个N × M的二维数组,表示一个迷宫。数据保证有唯一解,不考虑有多解的情况,即迷宫只有一条通道。

Output

左上角到右下角的最短路径,格式如样例所示。

Sample Input

0 1 0 0 0

0 1 0 1 0

0 0 0 0 0

0 1 1 1 0

0 0 0 1 0

Sample Output

(0, 0)

(1, 0)

(2, 0)

(2, 1)

(2, 2)

(2, 3)

(2, 4)

(3, 4)

(4, 4)
 

 

 

输入描述:

输入两个整数,分别表示二位数组的行数,列数。再输入相应的数组,其中的1表示墙壁,0表示可以走的路。数据保证有唯一解,不考虑有多解的情况,即迷宫只有一条通道。

输出描述:

左上角到右下角的最短路径,格式如样例所示。

输入例子:
5 5
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

输出例子:
(0,0)
(1,0)
(2,0)
(2,1)
(2,2)
(2,3)
(2,4)
(3,4)

(4,4)

import java.util.Scanner; import java.util.Stack; import java.util.List; import java.util.ArrayList; //保存数组中某个元素的位置信息 class NodeInfo{ int row;//保存行坐标 int col;//保存列坐标 public NodeInfo(int row , int col){ this.row = row; this.col = col; } } public class Main{     public static void main(String[] args){         Scanner scan = new Scanner(System.in);         while(scan.hasNext()){             int N = scan.nextInt();             int M = scan.nextInt();             int[][] maze = new int[N][M];             for(int i = 0 ; i < N ; i++){                 for(int j = 0 ; j < M ; j++){                     maze[i][j] = scan.nextInt();                 }             }             printPath(maze , N , M);         }//endwhile         scan.close();     }     private static void printPath(int[][] maze , int N , int M){     NodeInfo start = new NodeInfo(0 , 0);     NodeInfo end = new NodeInfo(N – 1 , M – 1);         int[][] visit = new int[N][M];     visit[0][0] = 1;     //col=1 row=0 表示向右移动一格, col=0 row=1表示向下移动一格     int[][] step = {{0 , 1} , {1 , 0}};     Stack<NodeInfo> stack = new Stack<NodeInfo>();       stack.push(start);     //判栈是否为空,压栈退栈寻找路径     while(!stack.isEmpty()){     NodeInfo node = stack.peek();     //判断是否为end节点     if(node.row == end.row && node.col == end.col){     break;     }     boolean canPush = false;//是否找到一个可前进的方向     //改变行进方向,尝试向下或向右,先尝试向右移动,后向下移动     for(int i = 0 ; i < 2 ; i++){     NodeInfo temp_node = new NodeInfo(node.row + step[i][0] , node.col + step[i][1]);     int row = temp_node.row;//保存节点的行信息     int col = temp_node.col;     //判断移动后的位置是否符合题意     //1.行列不能越界 2.节点没有访问过 3.当前节点可以走     if(row >= 0 && row < N && col >= 0 && col < M && visit[row][col] == 0 && maze[row][col] == 0){     canPush = true;     visit[row][col] = 1;     stack.push(temp_node);     break;//找到一条能走的一直走下去,不能走回退     }         }//endfor     //执行压栈操作后,即找到了一个可行方向,继续判断下一步     if(canPush){     continue;     }else{                 //如果下一步不满足要求,出栈操作     stack.pop();                  }           }//endwhile     List<String> list = new ArrayList<String>();     //格式化输出,list逆序保存     while(!stack.isEmpty()){     String info = “”;     info = “(” + stack.peek().row + “,” + stack.peek().col + “)”;     list.add(info);     stack.pop();     }//endwhile     int size = list.size();     for(int i = size – 1 ; i >= 0 ; i–){     System.out.println(list.get(i));     }     } }

    原文作者:迷宫问题
    原文地址: https://blog.csdn.net/bigthinker/article/details/51839249
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞