POJ 3984 迷宫问题

Description

定义一个二维数组:

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表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

Input

一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

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 import java.util.Scanner;
 2 import java.util.Stack;
 3 
 4 public class Main {
 5     
 6     private class Point{
 7         int x=0;
 8         int y=0;
 9         
10         public Point(int x,int y){
11             this.x=x;
12             this.y=y;
13         }
14         
15         public boolean equals(Point p){
16             return(x==p.x)&&(y==p.y);
17         }
18         @Override
19         //Override(重写)是子类与父类的一种多态性体现。 
20         //Override允许子类改变父类的一些行为。 
21         public String toString(){
22             return "("+x+","+y+")";
23         }
24     }
25     
26     private int[][] maze=null;//迷宫图
27     private Stack<Point>stack=new Stack<Point>();//路径栈
28     
29     public Main(int[][] maze){
30         this.maze=maze;
31     }
32     
33     public void go(){
34         Point out=new Point(maze.length-1,maze[0].length-1);//出口
35         Point in=new Point(0,0);//入口
36         Point curNode=in;//当前点为入口(当前点)
37         Point nextNode=null;//下一个访问点(目标点)
38         
39         while(!curNode.equals(out)){
40             nextNode=new Point(curNode.x,curNode.y);//设置目标点为当前点,便于偏移。
41             if((curNode.x+1)<maze.length&&maze[curNode.x+1][curNode.y]==0){
42                 nextNode.x++;//下路
43             }else if((curNode.y+1)<maze[0].length&&maze[curNode.x][curNode.y+1]==0){
44                 nextNode.y++;//右路
45             }else if((curNode.x-1)>=0&&maze[curNode.x-1][curNode.y]==0){
46                 nextNode.x--;//上路
47             }else if((curNode.y-1)>=0&&maze[curNode.x][curNode.y-1]==0){
48                 nextNode.y--;//左路
49             }else{//无路
50                 maze[curNode.x][curNode.y]=3;//标记为死路
51                 if(stack.isEmpty()){//判断栈是否为空
52                     System.out.println("Non solutioin");
53                     return;
54                 }
55                 curNode=stack.pop();//弹出上一次的点
56                 continue;//继续循环
57             }
58             
59             //有路
60             stack.push(curNode);//当前的压入栈
61             maze[curNode.x][curNode.y]=2;//标记为已走
62             curNode=nextNode;//移动当前点
63         }
64         
65         if(nextNode.equals(out)){
66             stack.push(nextNode);//将出口点添加到当前路径中
67             maze[nextNode.x][nextNode.y]=2;//标记为已走
68         }
69         
70         for(int i=0;i<stack.size();i++){
71             System.out.println(stack.elementAt(i));
72         }
73     }
74 
75     public static void main(String[] args){
76         Scanner sc=new Scanner(System.in);
77         int[][] maze=new int[5][5];
78         for(int i=0;i<5;i++)
79             for(int j=0;j<5;j++)
80                 maze[i][j]=sc.nextInt();
81         
82         new Main(maze).go();
83     }
84 }
View Code

 

posted @ 2014-01-23 09:28  JL-JackyLee  阅读(222)  评论(0编辑  收藏  举报