天地无极,乾坤剑法

导航

 

题目描述

定义一个二维数组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表示可以走的路。数据保证有唯一解,不考虑有多解的情况,即迷宫只有一条通道。

输出描述:

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

示例1

输入

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)

代码如下:

 1 package com.yzh.hehe;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 import java.util.Scanner;
 6 
 7 public class MiGong {
 8 
 9     public static void main(String[] args) {
10         // TODO Auto-generated method stub
11         Scanner scanner=new Scanner(System.in);
12         while (scanner.hasNext()) {
13             int x=scanner.nextInt();
14             int y=scanner.nextInt();
15             int[][] arr=new int[x][y];
16             for (int i = 0; i < x; i++) {
17                 for (int j = 0; j < y; j++) {
18                     arr[i][j]=scanner.nextInt();
19                 }
20             }
21             List<Point> list = miGong(arr); 
22             for(Point temp:list){
23                 System.out.println("("+temp.x+","+temp.y+")");
24             }
25         } 
26         scanner.close();
27     }
28     /** 
29      *  用一个list作为栈使用,每找到一个可通的点就加入到栈中,从栈顶取出一个点,依次从右下左上(对应1234)四个方向
30      * 判断下一个点是否可通(判断是否是1(墙)和考虑边界情况),可通就加入栈中,重复上面的操作,直到找到最右下角的点。否则的话换一个方向判断,
31      * 如果四个方向不通就从栈中删除这个点(回退一个点),再重复上面的操作。
32      */
33      private static List<Point> miGong(int[][]arr) {
34             int xBound=arr.length-1;
35             int yBound=arr[0].length-1;
36             List<Point>list=new ArrayList<Point>();
37             list.add(new Point(0, 0, 1));
38             Point temppoPoint;
39              
40             while (true) {
41                 temppoPoint=list.get(list.size()-1);
42                 if (temppoPoint.direction==5) {
43                     list.remove(list.size()-1);
44                     continue;
45                 }
46                 
47                 if (temppoPoint.direction==1){
48                     //temppoPoint.y==yBound 放在前面能避免考虑边界上操作时数组越界 
49                     if (temppoPoint.y==yBound||arr[temppoPoint.x][temppoPoint.y+1]==1) {
50                          temppoPoint.direction++;
51                          continue;
52                     } else {
53                         list.add(new Point(temppoPoint.x, temppoPoint.y+1, 1));
54                     }
55                 }else if (temppoPoint.direction==2) {
56                     if (temppoPoint.x==xBound||arr[temppoPoint.x+1][temppoPoint.y]==1) {
57                         temppoPoint.direction++;
58                         continue;
59                     } else {
60                         list.add(new Point(temppoPoint.x+1, temppoPoint.y, 1));
61                     }
62                 }else if (temppoPoint.direction==3) {
63                     if (temppoPoint.y==0||arr[temppoPoint.x][temppoPoint.y-1]==1) {
64                         temppoPoint.direction++;
65                         continue;
66                     } else {
67                         list.add(new Point(temppoPoint.x, temppoPoint.y-1, 1));
68                     }
69                 }else{
70                     if (temppoPoint.x==0||arr[temppoPoint.x-1][temppoPoint.y]==1) {
71                         temppoPoint.direction++;
72                         continue;
73                     } else {
74                         list.add(new Point(temppoPoint.x-1, temppoPoint.y, 1));
75                     }
76                 }
77                   
78       
79                 if (list.get(list.size()-1).x==xBound&&list.get(list.size()-1).y==yBound) {
80                     return list;
81                 }
82                  
83             }
84         }
85      
86 }
87 class Point{
88     int x;
89     int y;
90     int direction;
91     Point(int x,int y,int direction){
92         this.x=x;
93         this.y=y;
94         this.direction=direction;
95     }
96 }

 

posted on 2018-03-21 15:12  天地无极,乾坤剑法  阅读(341)  评论(0)    收藏  举报